Automate Conda commands

Divi
5 min readApr 27, 2021

Automate conda environment setup using scripting!

Miniconda

No longer is need to open command line every time you start to work on your python project. My experience has brought you automation of

Keep Calm and Double Click!

Have you ever had same feeling of frustration (maybe that is stronger expression) or perhaps little irritation when you daily come to office, sit on chair and spend almost 2 precious minutes to load your conda virtual environment to work in?

You have to look no further because I present out of my frustration and need — Scripting !

Bash Scripting

I made a small and simple script that I double click to load my conda environment and run Jupyter lab.

Batch Script | super is environment name

Create new .bat file known as Batch File (or .bat file) in Windows or Shell Script (.sh file) in Mac. For simplicity, I will only write commands to (a.) activate conda environment and (b.) open jupyter lab

Step 1: Commands as following are self explanatory!

call conda activate environment_name

call jupyter lab

Step 2: Save as .bat/.sh file

Now save these three command in text file and save it as Run.bat and place it in same directory wherever you project folder is. You can place it in a directory above the project to expose the root folder.

If I double click the .bat file as above, jupyter lab will open within exactly same directory structure you see above.

Now you can easily enjoy your work without typing extra 2 lines every day. Small automation but it makes me happy.

Additional

You can add other commands based on your need. For example, if you want to install some pip packages (probably you wont do this every time) or perhaps update all packages. Just add the command before running the Jupyter lab and you are set!

You can alternatively place the .bat anywhere you want but you may want to change directory before jupyter lab command and you are good to go.

call CD “your/project/path”

I will let you explore more and based on your requirement- you can automate almost anything.

For Anaconda users:

You can perform following to get it working with Miniconda.

Step 1.) Right click on Anaconda Prompt and Select Properties. (Now if you are going through Start button in Windows; You would need to go the file location which would look like this):

C:\Users\user_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)

Step 2.) The Properties window would have Target field under Shortcut tab. Copy the text in that Target field and paste it into blank notepad. Target field would look like:

%windir%\System32\cmd.exe “/K” C:\Users\user_name\Anaconda3\Scripts\activate.bat C:\Users\user_name\Anaconda3

Copy and Paste it into notepad. Edit it to

%windir%\System32\cmd.exe “/K” C:\Users\user_name\Anaconda3\Scripts\my_activation.bat C:\Users\user_name\Anaconda3

Save the file as my_setup_file_.bat — save it as .bat instead of .txt, anywhere you would like.

Step 3.) Now, go to

C:\Users\user_name\Anaconda3\Scripts

You will find activate.bat, create a copy of that file and renamed it at your convenience. Rename to my_activation.bat to match file name in previous step. Now we are going to edit this my_activation.bat file to customise to our requirements — environment we want to activate, launch Jupyter lab etc.

Edits to my_activation.bat will be as following:

...

@CALL “%~dp0..\condabin\conda.bat” activate super

@GOTO :End

)

@CALL “%~dp0..\condabin\conda.bat” activate %super

@CALL jupyter lab

Fun part

my desktop shortcut

Now, you have created your bat file which you can create shortcut off and send it to your desktop. Every morning when you boot your computer, now you can just double click the short cut to launch Jupyter lab in your desired directory with your desired Conda environment. Saves time and your delicate fingers for typing those commands every morning…

UPDATE:

I just found the even more easier way to activate your conda environment and open Jupyter lab.

Step 1. Go to C:\Users\user_name\Anaconda3\Scripts

Step 2. Create a duplicate of activate.bat and rename it, lets say, activate_julab.bat

Step 3. Open activate_julab.bat and append following line at end:

@CALL jupyter lab

a command to run jupyter lab….

Step 4. To make life easier, lets run activate_julab.bat using another .bat file that will act as

I have in my C:\ drive, a new bat file (named Run_My_Env.bat) and the contents of that bat file are two lines as following:

CD “path to your project or folder in which jupyter lab will open

%windir%\System32\cmd.exe “/K”

C:\Users\user_name\Anaconda3\Scripts\activate_julab.bat My_Env

Explanation

First line changes directory to a new path. It could be the path where you working project resides. Second line run activate_julab.bat in cmd with argument My_Env passed. (My_Env: name of your conda environment)

Step 5. (Optional) Make shortcut of your new bat file Run_My_Env.bat, maybe send it to your desktop so that every day you turn up to work and open your computer, there is a shortcut icon waiting for you to be clicked and you will be running jupyter lab in your desired conda environment in just one click. :)

Feel free to change icons of shortcut to make it easily differentiable from other environment activators.

Happy Engineering!

If you liked or have not liked this piece, make sure to share your appreciation and/or criticism through claps and comments.

Feel free to say hi on LinkedIn !

~ Divi

--

--