Customize your Python environment with pyenv, virtualenvwrapper, tox and pip-compile

These tools will simplify setup and automate routine operations. They will save the developer from many difficulties that prevent you from focusing on problem solving and comfortable writing code.

There are many ways to customize your Python environment. This article is about one of them. But this, of course, is not the only solution.

Python is a general-purpose programming language that is often recommended for beginners. Over the twenty years of its existence, several books on Python have been written. Although often referred to as simple, setting up Python development tools is far from an easy task.

Setting up the Python environment is quite complicated: xkcd

Use pyenv for version control

In my opinion, this is the best Python version manager. However, it is worth noting that pyenv runs on Linux, Mac OS X, and WSL2: that is, three “UNIX-like” environments.

Installing pyenv itself can sometimes be difficult. But can help special installer pyenv that uses curl | bash for bootstrap.

If you are using a Mac (or another system on which you are running Homebrew), you can read the instructions for installing and using pyenv here.

After installing and configuring pyenv, you can use pyenv global to install the default version of Python. You can choose your “favorite” version. Usually this is the latest stable version, but it’s not accurate -)

Use virtualenvwrapper to set up a virtual environment

For Python, you can create virtual environments, that is, put each project in an isolated environment. This can be done using virtualenvwrapper. Then it becomes possible to switch between virtual environments at any time.

You can read more about setting up a virtual environment. here.

If you need to repeatedly create and use the same virtual environment, it makes sense to automate the process. Today my choice is tox.

Use tox to automate

Tox is a great tool for automating your tests. In every Python environment, I create a tox.ini file. No matter what system I use for continuous integration, everything will work. And I can run it locally using virtualenvwrapper:

$ workon runner
$ tox

The thing is, I am testing my code on several versions of Python and several versions of library dependencies. This means that tox will work in multiple environments. Some of them will have actual dependencies. In some – frozen. I could also generate them locally using pip-compile.

Recently, I am considering nox as a replacement for tox. In this article I will not explain the reasons, but you should pay attention to it.

Use pip-compile to manage dependencies

Each time Python loads its dependencies at the code execution stage. Understanding and controlling which version of the dependencies is used will help to avoid unexpected crashes while the program is running. This means that we should think about dependency management tools.

For each new project, I include the file, which (as a rule) contains only one character:


Yes, everything is correct here. A single dot string. In the setup.pyfile file for dependencies, I write Twisted> = 17.5 instead of Twisted == 18.1, which makes it difficult to upgrade to new versions of the library.

“.” means “current directory” in which the corresponding file is used as a source of dependency information.

This means that when using pip-compile> requirements.txt, a file with frozen dependencies will be created. You can use this dependency file either in the virtual environment created by virtualenvwrapper or in tox.ini.

Sometimes the requirements-dev.txt file generated from (contents:.[dev]) or requirements-test.txt generated from (contents:.[test])

Alternatively pip-compile can use dephell. The dephell tool has many interesting things, for example, using asynchronous HTTP requests to load dependencies.


I consider Python a powerful and beautiful language. Nevertheless, in order to work productively, I use a certain set of tools that have proven their effectiveness, at least for me. So use these four tools – pyenv, virtualenvwrapper, tox and pip-compile – and you will be happy =)

As an advertisement

Meet me! For the first time in Russia – epic servers!
Powerful servers based on the latest AMD EPYC processors. CPU frequency up to 3.4 GHz. Rates – from 10 rubles per day!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *