ANR's Blog
Published on

Python: working with virtual environments

Authors

So, what are virtual environments and why should we use them?

We use a lot of packages in Python and by default all projects in your system will use the same directory to store and retrieve packages

Do you seee the problem here?

all your packages will need to use the same version of the package which is a bad thing because certain version of the packages might have bugs that break some projects and some other project might need some other version of the package this will be chaotic when you have a lot of projects in your system and this is where virtual environments come in

Virtual environment create a folder in your project directory and all the the packages related to that project will be stored there this allows each project to have it's own packages independent of other projects. In addition to this each virtual Environment will have it's own python installation with a specific version of python.

Choosing a virtual environment tool

There are many pacakages out there that help in creating and managing virtual environments currently virtualenv and venv are the most popular ones. Since python 3.3 venv is included in the standard python library while you have to install virtualenv via pip

What is the difference between venv and virtualenv?

As the virtualenv docs put it The venv module does not offer all features of this library, to name just a few more prominent:

  • is slower (by not having the app-data seed method),

  • is not as extendable,

  • cannot create virtual environments for arbitrarily installed python versions (and automatically discover these),

  • is not upgrade-able via pip,

  • does not have as rich programmatic API (describe virtual environments without creating them).

which one to use? That is totally upto you if you don't want another dependency you can go with venv, I personally like using venv but I recommend you to try both and choose what suits you best and satisfies all your requirements.

If you decide to go with virtualenv you can install it through pip with the following command

python -m pip install virtualenv

you can check other installation methods from here

venv comes pre-packaged with your python installation

Create a virtual environment

If you are using venv in your project folder run these commands to create a virtual environment

# if using venv
# on windows
python -m venv venv

# on other platforms
python3 -m venv venv

If you decide to use virtualenv use these commands instead

# if using virtualenv
virtualenv venv

In the above command the last venv is the name of the folder in which venv files are stored you can change it whatever you want I like to name it venv.

Now activate the virtualenv using the command

# on windows
venv\Scripts\activate

# on other platforms
. venv/bin/activate

(This script is written for the bash shell. If you use the csh or fish shells, there are alternate activate.csh and activate.fish scripts you should use instead.)

If you changed the folder name while creating the virtual environment please use the name you gave in the above command

after running that command your virtual environment will be activated and you can verify this by looking at the left side of your command prompt if you are inside a virtual environment you will see a (venv) infront of your normal prompt look at the image below

command prompt with virtual environment disabled and enabled

To exit the virtual environment just type deactivate and press enter you will exit the virtual environment.

getting all your project requirements

one of the advantages of using pip is that you can use pip freeze to get all your project requirements in the requirements.txt file.

You can get all your project requirements in the requirements.txt file by running the following command when your virtual environment is active

pip freeze > requirements.txt

The requirements.txt can then be committed to version control and shipped as part of an application. So that users can install all the necessary packages with install -r

installing packages listed in requirements.txt

The following command will install the packages according to the configuration file requirements.txt

pip install -r requirements.txt

Run the above command when your virtual environment is active to install all the packages listed in requirements.txt inside your virtual environment.