在 Windows 中激活 conda 环境时如何更改目录 [英] How to change directory when activating conda environment in Windows

查看:146
本文介绍了在 Windows 中激活 conda 环境时如何更改目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于本问答(由 Nehal J Wani 回答)似乎可以创建并修改某个配置文件,以便在激活 conda 环境时自动更改目录.有人知道如何在 Windows 中执行此操作和/或可以将以下代码转换为 Windows 友好的步骤吗?

Based off of this Q&A (answered by Nehal J Wani) it seems that it's possible to create and then modify a certain config file in order automatically change directories when activating a conda environment. Does someone know how to do this in Windows and/or can just translate the below code to Windows-friendly steps?

(root) [watchmen@manhattan ~]# mkdir /tmp/myproject
(root) [watchmen@manhattan ~]# conda create -yn myproject python=3.6
(root) [watchmen@manhattan ~]# source activate myproject
(myproject) [watchmen@manhattan ~]# mkdir -p $CONDA_PREFIX/etc/conda/activate.d
(myproject) [watchmen@manhattan ~]# cat <<EOF > $CONDA_PREFIX/etc/conda/activate.d/gotodirs.sh
> #!/bin/bash
> pushd /tmp/myproject
> EOF
(myproject) [watchmen@manhattan ~]# chmod +x $CONDA_PREFIX/etc/conda/activate.d/gotodirs.sh
(myproject) [watchmen@manhattan ~]# pwd
/root
(myproject) [watchmen@manhattan ~]# source deactivate
[watchmen@manhattan ~]# source /conda/bin/activate myproject
/tmp/myproject ~
(myproject) [watchmen@manhattan myproject]# pwd
/tmp/myproject

推荐答案

我意识到这个问题很老,但我遇到了同样的问题并找到了解决方案.

I realise this question is old but I ran into the same problem and found a solution.

第 1 步:通过运行找到您的环境目录

Step 1: Find out your environment's directory by running

conda env list

在 conda Powershell 中.

in the conda Powershell.

第 2 步:使用以下命令切换到环境目录:

Step 2: Change to the environment's directory by using the command:

cd [PATH]

或通过导航到 Windows 资源管理器中的目录.(您可以从步骤 1 的输出中复制路径.)

or by navigating to the directory in the windows explorer. (You can copy the Path from the output of Step 1. )

第 3 步:在那里创建以下目录通过使用命令:

Step 3: In there create the following directory by using the command:

mkdir .\etc\conda\activate.d

或者通过创建一个新目录etc",在那里创建一个新目录conda",然后在 conda 中创建最终目录activate.d".激活环境时,将运行此目录中的所有脚本.

or by making a new directory "etc", in there a new directory "conda" and then in conda the final directory "activate.d". All scripts in this directory will be run when activating the environment.

第 4 步:创建脚本(在 activate.d 目录内)通过使用命令

Step 4: Create the script (inside of the activate.d directory) by using the command

cd .\etc\conda\activate.d
new-item . -name set_working_directory.bat -type "file" -value "cd [PATH]"

其中 [PATH] 是您所需工作目录的路径或者通过创建一个新文件.bat"并在里面写cd [PATH].因此,如果您希望 conda 切换到目录 C:\user\Documents,例如,您可以编写 cd C:\user\Documents 并保存文件.

where [PATH] is the path to your desired working directory or by crating a new file ".bat" and inside writing cd [PATH]. So if you want conda to switch to the directory C:\user\Documents for example, you would write cd C:\user\Documents and save the file.

现在,当您运行 conda activate [your_env] 时,脚本会自动更改您的目录.

Now, when you run conda activate [your_env] the script changes your directory automatically.

这篇关于在 Windows 中激活 conda 环境时如何更改目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆