Python 2 virtual environment for your legacy applications

Audun Nes
2 min readSep 30, 2020

--

The Python logo is a trademark of the Python Software Foundation. More info here: https://www.python.org/community/logos/

Python 2 reached its End of Life (EOL) on the 31st of December 2019, and since then 3rd party Python packages has only needed to focus on Python 3 compability. Which is perfectly fine for new development, or for migration of smaller codebases from Python 2 to Python 3. For many of your legacy applications, it might not be neither time nor budget to go through such a migration. For fixing defects you will still need to be able to spin up a development environment based on Python 2, and install its dependencies in a virtual environment. Python virtualenv will by default also try to install setuptools package, which has not has support for Python 2 since version 44.x (as of time of writing the latest Python 2 compatible version is https://pypi.org/project/setuptools/44.1.1/

My colleague Samit Kumar Patel shared the following tip with me that uses virtualenvwrapper to create a Python 2 compliant virtual environment on Linux without the setuptools package:

mkvirtualenv --python=/bin/python --no-setuptools --no-site-packages --system-site-packages myenv

You don’t need to activate the virtual environment afterwards. The mkvirtualenv command will also take care of that for you.

If you don’t know exactly where your python executable is located, and if you don’t have virtualenvwrapper installed, a similar command using virtualenv directly on Linux would be:

virtualenv --python=$(which python) --no-setuptools --no-site-packages --system-site-packages ~/.virtualenv/myenv
source ~/.virtualenv/myenv/bin/activate

Furthermore, if your computer has both Python 2 and Python 3 installed, then you can change the which command to look for the python2 executable instead.

virtualenv --python=$(which python2) --no-setuptools --no-site-packages --system-site-packages ~/.virtualenv/myenv
source ~/.virtualenv/myenv/bin/activate

Have fun, and stay frosty!

--

--

Audun Nes

Lead Cloud Engineer/Site Reliability Engineer from Copenhagen, Denmark. GitHub: https://github.com/avnes