Why is my Virtualenv not working

A virtual environment for Python

weidner / computer / software / python /

Virtual environments have something of their own. I can set it up however I want and don't have to worry about the rest of the system.

Not only can I set up my environment differently from the system environment, sometimes I have to. Usually when I need software for a project that requires newer or older libraries than those available in the system. Then I can try to go through the dependency hell and get the software to work with the current system environment. Or I set up a virtual environment that fulfills all of the dependencies.

So a nice thing. There are also different types of virtual environments. There are

  • virtual machines that provide a complete computer and operating system,
  • Containers that only contain the most necessary software in a process group and
  • virtual environments that only provide the required libraries for a programming language.

For Python is virtualenv such an environment. The paths for the libraries to be used are adjusted via environment variables so that the virtual environment is independent of the Python libraries of the operating system.

Of course, this requires more disk space, since a number of libraries are now available multiple times in the. But this space is considerably cheaper than the working time that goes into adapting new software to the system or - even worse - adapting the system to new software.


With that enough of the preface, let's try the matter out.

First of all, I need the program virtualenvwith which I can set up the individual environments.

At Fedora I can find this in the package python3-virtualenvthat I install with:

I'm already done with the installation. The software package contains the program virtualenv (possibly with appended version number), with which I can set up virtual environments for Python.


Now I have the software for the virtual environments, how do I use it?

Create a virtual environment

First of all - and I only have to do this once per project - I create a virtual environment.

I move to the project directory and call virtuelenv with the name of the directory in which the libraries of the virtual environment are to be stored.

I usually mention this directory venv for a good reason that becomes clear as you read on.

Activate the virtual environment

Every time I want to work with the virtual environment in my property, I have to activate it. But only once before I start working.

Since I don't want to type so much, I build an alias for Bash, my preferred shell.

This alias is not that much shorter than the actual command. But because it is only one word, I save a lot with the Command Line Completion compared to the command with the directory path.

And so that the alias works with every project with a virtual environment, I will name the associated directory venv.

Deactivate the virtual environment

When I no longer need the virtual environment, I can deactivate it with the following command.

In most cases it is not harmful for me to work with this Python environment in other directories. But in this way I am formally ending my work on this project, and besides, I don't get confused if I then work on another project with my own Python environment.

install a software

If I have activated the virtual environment, as described above, all Python libraries that I also install end up in it.

In order to have the same environment available on another computer later, I record my current status in a text file:

The file python3-requirements.txt I store it in the repository with my project and am thus one step closer to reproducible builds. The directory venv / With the Python modules, I don't necessarily need to drop the project.

With I can then set up another virtual environment with identical software after I have set up the environment as above:

Sphinx documentation system

Most often I use virtualenvwhen I use documentation and texts sphinx write.

When setting up, I not only create the virtual environment, but also install Sphinx myself in this environment.

After the installation I record the current status of the packages and start the project with:

Everything else goes as described above.

Additional information

There is more detailed information on, for example, the Hitchhiker's Guide to Python.

Posted 2017-05-13