This page looks best with JavaScript enabled

Virtual Environments in Python

 ·  🎃 kr0m

Having virtual environments in Python can be very interesting to isolate projects from each other. For example, we can have a different version of a certain library for each project. Additionally, it provides greater security when updating the operating system since the virtual environment libraries will not depend on those of the operating system.

We install support for virtual environments in Python:

emerge -av dev-python/virtualenv

We create a virtual environment called ENV:

virtualenv ENV

New python executable in /home/kr0m/ENV/bin/python2.7
Also creating executable in /home/kr0m/ENV/bin/python
Installing setuptools, pip, wheel...done.

In Python3:

python3 -m venv ENV

We load the virtualEnv:

RX4 ✺ ~> source ENV/bin/activate

(ENV) RX4 ✺ ~>

Now we can install the libraries we need through the virtualEnv pip, and they will be completely isolated from our OS.

We can check the installed libraries using pip both outside and inside the virtual environment:

RX4 ✺ ~> pip list |grep tahoe

(ENV) RX4 ✺ ~> pip list |grep tahoe
tahoe-lafs 1.12.1 

To exit the virtual environment:

(ENV) RX4 ✺ ~> deactivate

RX4 ✺ ~>

If we want a script to run directly within our virtual environment, we must indicate the interpreter:

#! /path/to/ENV/bin/python
If you liked the article, you can treat me to a RedBull here