DEV Community

Cover image for Get Virtual! with Python Virtual Environments
Eden Jose
Eden Jose

Posted on

Get Virtual! with Python Virtual Environments

These are used to isolate environments and resolve dependency conflict resolution.

This ensure projects have isolated siloes where they live and have their own dependency tree that don't interfere with one another.

Another thing, feel free to jump to sections that you're only interested in!


Python Dependencies

You will often use 3rd-party libraries - non-default libraries which doesn't come along with Python.
You'll have to import and install them before they can be used.

  • normally installed using pip or easy_install
  • libraries are pulled from the pupi.org or Python Package Index which as an enormous index of libraries

image


Installing Libraries

To install a library, as example, django

pip install django
Enter fullscreen mode Exit fullscreen mode

To install a specific version of django,

pip install django==2.2.12
Enter fullscreen mode Exit fullscreen mode

To check the version of Django installed, you can run any of the commands below:

django --version
python -m django --version
Enter fullscreen mode Exit fullscreen mode

You can also view the versions of all installed packages, including Django,

pip freeze
python -m pip freeze
Enter fullscreen mode Exit fullscreen mode

To save all of these data (versions of each modules) to be reused or processed later, you can forward them to a file.

pip freeze > module-versions.txt
Enter fullscreen mode Exit fullscreen mode

To upgrade to a newer version

pip install --upgrade django
Enter fullscreen mode Exit fullscreen mode

To uninstall it, don't delete the folder, instead

pip uninstall django
Enter fullscreen mode Exit fullscreen mode

Another example: installing the "site" library

import site
print(site.getsitepackages())
Enter fullscreen mode Exit fullscreen mode

Using a requirements.txt

Another way to install multiple third parties with just a single command is to put them into a single requirements.txt file and use pip to to do a bulk-install.

(Note that it's recommended to install modules for a project in a virtual environment. Read below to learn more about this.)

To install from a requirements file

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Using env

Let's take django as our example again. We recently installed django version 2.2.12. If we are to install a new version, v3.0, then it will override and uninstall the v2.2.12.

Some projects need specific versions of a library and this is where virtual environments come into play. Virtual environments exist to isolate projects and their dependencies from one another.

To create a virtual environment, we can use venv

python -m venv my-project-1
Enter fullscreen mode Exit fullscreen mode

After you run this, a folder for the virtual environment will be created.

$ ll
total 0
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 my-project-1/

$ ll my-project-1/
total 5
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Include/
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Lib/
-rw-r--r-- 1 Eden Jose 197610 121 Sep 14 12:16 pyvenv.cfg
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Scripts/
Enter fullscreen mode Exit fullscreen mode

To activate a virtual environment, run the "activate" script inside the Scripts folder of the project. As an example,

$ source ./my-project-1/Scripts/activate
$
(my-project-1) 
Eden Jose@EdenJose MINGW64 ~/Desktop/Git/5-Virtual-Envs
Enter fullscreen mode Exit fullscreen mode

Notice that the name of the virtual environment can now be seen at the prompt. To exit out of the virtual environment, simply run

deactivate
Enter fullscreen mode Exit fullscreen mode

Using venv

Another way to create virtual environments is through virtual env.
Note that virtual env doesn't ship alongside your Python installation. To install virtual env,

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

This take a similar step to create a new environment and activate it,

virtualenv my-project-2

# you can also use
python -m virtualenv my-project-2
Enter fullscreen mode Exit fullscreen mode

You can create the virtual environment with a different Python installation by using the "-p" flag

# Check the python installations you currently have
$ where python
C:\Users\Eden Jose\AppData\Local\Programs\Python\Python39\python.exe
C:\Users\Eden Jose\AppData\Local\Programs\Python\Python38\python.exe

# You can choose from this two and create a virtual env with that version
python -m virtualenv -p
Enter fullscreen mode Exit fullscreen mode

Using virtualenvwrapper

This is what I am using in labs and even at work. Treat this section as standalone, and can be setup even without the venv or env

This is another virtualenv library which wraps up some useful management functionality for virtualenv. One feature of this is it manages a single location of all your projects.

Unlike virtualenv and env where the project folder is created on your current working directory, virtualenvwrapper creates a folder is the user's home directory.

virtualenvwrapper maintains this folder where all your environment folders are created by default. You can setup your own directory where all the virtual environments folder will be created by creating the variable WORKON_HOME.

I had some problems when I was trying this one. As a solution, I just uninstalled any existing virtualenvwrapper installed on my system and do a fresh install. This

To do a fresh install,

joseeden@EdenJose:~$ sudo pip uninstall virtualenvwrapper
joseeden@EdenJose:~$ sudo pip install virtualenvwrapper
Enter fullscreen mode Exit fullscreen mode

Next, append this to your .bashrc file


# Change the WORKON_HOME path to your directory where you want all your virtual environment folders created
export WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'
export VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3'
source /usr/local/bin/virtualenvwrapper.sh
Enter fullscreen mode Exit fullscreen mode

To create a virtual environment

mkvirtualenv <name>

# Sample
joseeden@EdenJose:~$ mkvirtualenv project-a
created virtual environment CPython3.8.10.final.0-64 in 21319ms...
(project-a) joseeden@EdenJose:~$

# Note that to create another virtual envionment, exit out of the previous virtual environment by running "deactivate"

joseeden@EdenJose:~$ mkvirtualenv project-b
created virtual environment CPython3.8.10.final.0-64 in 21319ms...
(project-b) joseeden@EdenJose:~$
Enter fullscreen mode Exit fullscreen mode

To see all your virtual environments,

joseeden@EdenJose:~$ workon
project-a
project-b
Enter fullscreen mode Exit fullscreen mode

To switch between virtual environments, you can simply run

joseeden@EdenJose:~$ workon project-b
(project-b) joseeden@EdenJose:~$
Enter fullscreen mode Exit fullscreen mode

To exit out of a virtual environment

(project-b) joseeden@EdenJose:~$ deactivate
joseeden@EdenJose:~$
Enter fullscreen mode Exit fullscreen mode

ERRORS

mkvirtualenv: command not found

If you get an error "command not found", this might mean virtualenvwrapper was not properly installed.

You can simply re-do the installation.
I had issues also when trying the virtualenvwrapper on Git Bash in VSCode so I decided to run the commands below in WSL.

sudo pip uninstall virtualenv -y 
sudo pip uninstall virtualenvwrapper -y
sudo pip install virtualenv
sudo pip install virtualenvwrapper
echo "WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'" >> ~/.bashrc
echo "source `which virtualenvwrapper.sh`" >> ~/.bashrc
. ~/.bashrc

Enter fullscreen mode Exit fullscreen mode

virtualenvwrapper Command '' not found, but can be installed with ...

When you create a virtual environment using the mkvirtualenv command, you might see this error message

joseeden@EdenJose:/mnt/c/Users/Eden Jose$ mkvirtualenv project-a

Command '' not found, but can be installed with:

sudo apt install mailutils-mh  # version 1:3.7-2.1, or
sudo apt install meshio-tools  # version 4.0.4-1
sudo apt install mmh           # version 0.4-2
sudo apt install nmh           # version 1.7.1-6
sudo apt install termtris      # version 1.3-1
Enter fullscreen mode Exit fullscreen mode

To resolve this, add the lins below (in this order) to your .bashrc file

export VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3'
source /usr/local/bin/virtualenvwrapper.sh

## Note that the path depends on where your Python is installed. To check
which python
which python3
Enter fullscreen mode Exit fullscreen mode

References


Oldest comments (0)