WHY DID I STOP USING THE COMMAND LINE TO ACTIVATE MY VIRTUAL ENVIRONMENT?

Raghuvansh Tahlan
Analytics Vidhya
Published in
5 min readAug 17, 2020

Learn how to create Virtual Environment using venv and activate using executables.

Photo by Kevin Ku on Unsplash

What is a Virtual Environment?

Virtual Environments help create, manage and isolate environments for the project, each one using same or different versions of the executables.

Why do we need one?

Consider the following scenario where you have two projects: Project A and Project B, both of which have a dependency on the same library, Project C. The problem becomes apparent when we require different versions of Project C. Maybe Project A needs v1.0.0, while Project B requires the newer v2.0.0. This is a real problem for Python since it can’t differentiate between versions in the same directory.

Where do we need one?

With growing Python-compatible web frameworks like Django, Flask and increasing cloud services like AWS, Google Cloud, Heroku, linode, etc. along with a multitude of companies taking their businesses online and each project needing its own libraries and versions, it becomes important to isolate each project.

In this guide we look forward to creating a Virtual Environment in a project folder.

Consider a folder/directory My_Project in which we want to create a virtual environment named Test

1. Check if Python has been installed correctly.

Run Command Prompt (CMD) or Windows PowerShell window and navigate to the My_Project directory and execute python followed by exit() If you get the same output as in the image, then everything is working fine.

If it says python is not recognized as an internal or external command, operable program or batch file then you have to add the directory in which python.exe exists to the Environment variables.

2. Execute pip install virtualenv to install the virtual environment.

3. Execute python -m venv Test where Test is the name of the virtual environment you want to create.

If the commands have been successfully executed till now, a folder named Test should be created inside My_Project directory.

Inside the directory Test, you should see a similar file structure.

4. To activate the environment, execute .\Test\Scripts\activate or .\Test\Scripts\activate.bat

NOTE: The word “ (Test) ” before the directory path indicates that you have successfully activated your virtual environment.

5. To deactivate the environment, execute .\Test\Scripts\deactivate.bat

TIPS and TRICKS

Consider a project you are working on: if it’s a small project you won’t mind executing the commands to activate the environment, if it’s an extensive project spanning months executing each command every time you work on the project doesn’t make much sense. So, here’s what you can do:

Create a Batch(.bat) file containing the command to activate the environment.

Create a new text document (.txt ) in the My_Project directory, write the absolute path of activate.bat in the file and save the file as activate1.bat in the project directory.

Now, start the Command prompt/PowerShell window in the project directory by right-clicking on the project directory while pressing Shift key. OR start a Command prompt/PowerShell window anywhere and navigating it to the project folder. Now execute activate1.bat And Voila! Your virtual environment is activated.

Here C:\Users\raghu\Desktop\My_Project\Test\Scripts\activate.bat is the absolute path for my file.

But for some lazy people (me included)- it’s still a two-step process. Can it be shortened further, hell yeah!

Create a Batch(.bat) file which does both the tasks — start a Command prompt/PowerShell window and activate the virtual environment.

Now edit the Text(.txt) document created in the earlier step to

start cmd /k “%~dp0Test\Scripts\activate.bat”

Now this command may seem vague to some readers. I copy it from a former project and change the word ‘Test’ to the name of the virtual environment, that would suffice. But let’s understand what it says part by part.

1. start cmd is to start a Command prompt window.

2. /k is to keep the Command prompt window up and running after execution of the command.

3. Now everything between the quotes is the actual command which activates the virtual environment %~dp0Test\Scripts\activate.bat

4. %~dp0 refers to the path of the directory where you are executing the executable. That path will be substituted in this place i.e. C:\Users\raghu\Desktop\My_Project\ which is the project directory.

5. Test is the name of the virtual environment.

The final command after substitution which will be executed is :C:\Users\raghu\Desktop\My_Project\Test\Scripts\activate.bat which is the absolute path for the activate.bat file which is same as the used in the first method.

Save the Text document as activate2.bat in the project directory and execute it.

NOTE: You can activate the ‘TEST’ virtual environment using the ‘activate1.bat’ from any directory, but to use ‘activate2.bat’ it has to be in your project directory.

I am just a beginner in using Batch files and this was just a trick which I have learnt over time and use. So, I am looking forward to hear from experienced people. Readers can also reach me out on LinkedIn.

--

--

Raghuvansh Tahlan
Analytics Vidhya

Passionate about Data Science. Stock Market and Sports Analytics is what keeps me going. Writer at Analytics Vidhya Publication. https://github.com/rvt123