In this article, we will be covering the topic of Python Virtualenv (Virtual Environment). What is a virtual environment?
A virtual environment is a tool that creates individual python environments to separate projects with different dependencies or software versions.
Let’s take the following scenario.
A Programmer wants to work on multiple projects with different versions of Python at the same time. How is this achieved?
By creating virtual environments, they can work on multiple projects with their own versions of Python, PIP, and a separate location for external dependencies, all within the same computer.
How To Create A Virtual Environment?
About the Module – virtualenv
The virtualenv module allows the creation of separate Python environments. Virtualenv creates folders consisting of all the required executables to use external dependencies a project requires.
Installing the Module
The module is NOT a part of the Python Standard Library. Install it manually via PIP using the following command.
Command for Windows and Linux/Mac
pip install virtualenv
As of 2022, the latest version is virtualenv 20.16.3
Creating A Virtual Environment
Create a virtual environment using the below command:
Upon execution, the directory
your_project_name is created. In this directory, all the required executables to use external dependencies for a project is present.
To specify the version of the Python interpreter for the virtual environment, run the following command.
virtualenv -p /usr/bin/ [enviornment name]Creating a enviornment with Python 3.9
virtualenv -p /usr/bin/python3.9 my_Enviornment3.9Creating a enviornment with Python 2.7
virtualenv -p /usr/bin/python2.7 my_Enviornment2.7
Note that different virtual environments can have different versions of Python.
In order to work on a specific environment, you must activate it using the following command.
If you have multiple environments, be sure to activate the appropriate one
source [enviornment name]/bin/activate
How To Find the Location Of Your Virtual Environment
The activated virtual environment’s location can be found using the “
which” keyword. Let’s look at the following example
$ which [your environment's name]Sample Output /Users/Adimn/Documents/myEnvironment
The first matched location is displayed by default. To display all available matches, pass the
-a argument to show all.
$ which -a [your environment's name]Sample Output /Users/Admin/Documents/venv/myEnvironment /Users/Admin/myProjects/venv/myEnvironment
How to Change the Path of the Virtual Environment
The virtualenv module provides an option to change the path of your virtual environment. The
relocatable command achieves this.
All virtual environments are located on a specific path. A virtual environment cannot be manually relocated or transferred to another system. To change the path of a virtual environment, use the following command:
$ virtualenv --relocatable [Your Environnment Name]
Make sure to run the command outside your environment directory. This will force the files created by setup tools or distribution to use relative paths. If you install a new package into a relocatable environment, run –relocatable again.
Where are the Virtual Environments Created?
The virtualenv module creates a file within the project directory. The folder is called
venv, but the name can be changed accordingly. The venv file consists of Python PIP .exe files.
External dependencies of an activated virtual environment are installed within the venv file. Any other modules installed after activation are installed within the particular project’s venv folder.
What is Python virtualenv .env File?
With the help of a .env file, we can use environment variables for local development. A .env file separates environment variables from similar projects that utilize them.
A .env file consists of key-value elements of the necessary environment variables for the program. The file is usually included within the virtualenv directory.
To read environment variables from the
.env file, the python-dotenv module is used. Upon installation, you can read env files using the following function:
from dotenv import load_dotenv load_dotenv(path/example/.env')
load_dotenv() will look for env files and load the variables. Therefore, all variables from the env file are now accessible globally in your program.
How To Setup Virtual Environments in VS Code
In Visual Studio Code, it is simple to create a Virtual Environment. Let’s look at the following step to create Python Virtualenv Location.
Create a Python Terminal within VS Code by pressing Ctrl+Shift+P
In the terminal, type the following command:
python -m venv venv
The following dialog will pop up:
Click yes to create the virtual environment. Install necessary external modules using PIP.
What is Python Virtualenv’s Directory Structure?
Python virtual environment has a simple directory and file structure. It consists of a “bin” sub-directory that contains a Python interpreter and Python modules installed in the environment. By calling the interpreter in the bin sub-directory, Python uses the installed packages in the virtualenv.
Upon creating an environment, we can call it via Python using the path to the bin sub-directory in the venv folder. For example,
How To Run Separate PIP Config Files in Virtualenv
We can run config files based on what virtualenv is being worked on. Let’s see how that is achieved. Within the command line, run the following command based on the environment:
~/.pip/pip.conf Run: ~/.samplenv/environment1/pip.conf ~/.samplenv/environment2/pip.conf ... and so on.
NOTE: each config will be inherited from: ~/.pip/pip.conf
The environment will overwrite each config file.
How to Activate the virtualenv and Run a Python Script
It is possible to automate the process of activating a virtualenv and running Python scripts with the help of Batch files.
In the following example, we are activating the virtual environment and running the required Python Script.
Sample.bat: @echo off cmd /k "cd /d C:\Users\Admin\Desktop\venv\Scripts & activate & cd /d C:\Users\Admin\Desktop\helloworld & python helloworld.py doSomething"
This activates the virtual environment and runs the script “helloworld.py”
FAQs on Python Virtualenv Location
You can see virtual environments using virtualenv. Python virtualenv location can also be found within the project directory.
The virtualenv module will store the necessary executables within your project directory under a file called venv. All .exe related to the virtual environment must be created within the project folder.
In this article, we have discussed a vital feature in Python programming called virtual environments. Virtual environments allow programmers to work on multiple projects with different dependencies within the same machine simultaneously. The Python virtualenv module enables the creation of such virtual environments. We’ve reviewed various techniques to fetch Python virtualenv location.