Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change directory when activating conda environment in 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
like image 615
Liam Hanninen Avatar asked Oct 08 '18 02:10

Liam Hanninen


Video Answer


1 Answers

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

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

conda env list

in the conda Powershell.

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

cd [PATH]

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

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

mkdir .\etc\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.

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]"

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.

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

like image 155
Emu Avatar answered Oct 01 '22 07:10

Emu