Activating Python Virtual Environment with Custom Batch Script
Once you create your virtual environment for python using the virtualenv package as of my previous blog you will find that it is difficult for us to activate it each time by navigating into the virtualenv’s destination and then into Scripts and finally open command prompt and enter activate.
That is tedious, time consuming and requires effort to do it. So if your are a lazy person like me, Congrats, I have come up with a “One Line Solution” for our problem.
I choose a Lazy person to do a hard job, Because a Lazy person will find an easy way to do it. -Bill Gates
First of all let us understand How Stuff Works
When you create a virtual environment this is what happens
The virtualenv package uses the installed python, which is in the directory path called ‘base prefix’ and creates a new executable python from that directory into the path where we created the virtual environment.
This simply means we are having a Base Python and a virtual python environment which are of same version but any changes made to the virtual environment won't reflect back to the base python. This is quite useful when we need different versions of the same package without any difficulties.
When we finally activate our environment, note the path we are working on
<path-to-venv>/<ven-name>/Scripts>
Notice that /Scripts is constant for all virtualenv’s and the “path-to-venv” will also be a fixed one, If we create our virtualenv in same folder, Now the only thing that is a variable is the environment name.
Batch Script
If you are a programmer, then batch scripting is easy to understand. For all non-programmers let me explain you.
In Windows if you create a file with an extension “.cmd” windows recognized the file name as a alias, and the contents of the file as commands which will be executed.
Now that we have understood what batch script is and how to use it. Let’s hack it for our purpose.
Activating Script
creating a custom script named activate that takes a single input parameter, i.e the name of the virtual environment that we need to activate.
Let me demonstrate the script for you. We have a virtual environment called “test” which is in the Workspace directory where I usually keep my virtual environments.
If we were to activate it by usual method it looks like this
but with the script it now looks like this
once the command gets executed our environment gets activated.
Here’s the script
The path to my venv is simply hard coded with a parameter. You see that “%1” in the path. That is the whole key thing here. Whenever we run the command “activate” with a space and then the venv’s name. It gets replaced here making it the actual full path.
Where do we put this?
The simplest place to put it is under the “System32" folder even though it is a dangerous place to workon. So how to get there?
Step 1: Right click on the windows icon in the bottom left corner of your screen.
Step 2: Click on “Windows PowerShell(admin)”. That should open a PowerShell window at the System32 folder.
Step 3: Open VS Code or any other text editor from that PowerShell window. Note: Elevated privilege required.
Step 4: Create the file activate.cmd and type in the script.
Note: modify the root directory based on your computer’s path.
Step 5: Save the script and then enjoy activating your environment with this new method.