Saturday, October 30, 2021

[SOLVED] Create virtualenv with specific, older, setuptools version

Issue

I am required to support a project with a legacy version of python (3.5) and I'm struggling to create a virtualenv because latest setuptools (>51) no longer supports python==3.5. It throws syntax errors during installation of packages as it tries to run the setuptools.setup function which contains "future" syntax. I'm trying to figure out a way to create a virtualenv with an older version of setuptools as its default.

My current workaround is to create the virtualenv normally, then downgrade setuptools. This works for now, but could break at any time if other setuptools modules change syntax such that pip install setuptools==old can't run.

virtualenv -p python3 venv
source venv/bin/activate
pip install --upgrade setuptools==50.3.2 # This line could break in future
pip install my_project

I can see the virtualenv --extra-search-dir option (https://virtualenv.pypa.io/en/legacy/userguide.html#the-extra-search-dir-option) but that defaults to the latest version it can find since "virtualenv will look for wheels in the specified directories, but will use pip’s standard algorithm for selecting the wheel to install", so will ignore the old setuptools version I try to point it at.


Solution

The comment by @hoefling to use python -m venv seems to be the best solution, I can get this package from apt and that should always give me a version compatible with the base python version. I can then upgrade to a pinned version of setuptools/pip after building the venv.

apt update
apt install python3-venv python3-pip
python3 -m venv venv
source venv/bin/activate
pip install --upgrade setuptools==50.3.2 pip==20.3.3


Answered By - David258