Install Virtualenv for Python2.7 on Debian 10

debianpippythonvirtualenv

I want to use python2.7 with virtualenv. I recently upgraded from debian 8 to debian 10. I originally had python2.7 and pip installed on debian 8, but maybe something happened during the installation and now I don't have pip. But I do still have python2.7 installed:

$ python --version
Python 2.7.16

So I just installed pip like so:

$ cd /tmp
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py

And now I can see that I have pip installed:

$ which pip
/home/me/.local/bin/pip
$ pip --version
pip 20.0.2 from /home/me/.local/lib/python2.7/site-packages/pip (python 2.7)

Firstly, is this where pip should be installed? Under my home directory? I am the only user on this PC, but I'm not sure if pip should be in /usr/share/ or somewhere more public for it to work properly? Should I have used sudo python /tmp/get-pip.py to install pip? I don't plan to run python as root, but apt always requires root for installations, so maybe installing pip should have too? The documentation did not specify.

Anyway, next I tried to update pip to the latest version and install virtualenv:

$ pip install -U pip
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already up-to-date: pip in ./.local/lib/python2.7/site-packages (20.0.2)

$ pip install virtualenv
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: virtualenv in ./.local/lib/python2.7/site-packages (15.1.0)

All seems good. But when I try and check which version of virtualenv I have, it fails:

$ virtualenv --version
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from virtualenv.__main__ import run_with_catch
ImportError: No module named __main__

And if I try and use virtualenv it always throws up these errors.

So overall, my question is to how correctly install python2.7, pip and virtualenv on debian 10. I don't mind uninstalling everything and starting again if that is what it takes.


As instructed by Stephen Kitt in the answer below, I have tried uninstalling the versions of pip and virtualenv that I previously installed with get-pip.py, however this gives some new errors. I will explain exactly what I have done…

First uninstall virtualenv:

$ pip uninstall virtualenv
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Found existing installation: virtualenv 15.1.0
Uninstalling virtualenv-15.1.0:
  Would remove:
    /home/me/.local/bin/virtualenv
    /home/me/.local/lib/python2.7/site-packages/virtualenv-15.1.0.dist-info/*
    /home/me/.local/lib/python2.7/site-packages/virtualenv.py
    /home/me/.local/lib/python2.7/site-packages/virtualenv_support/*
Proceed (y/n)? y
  Successfully uninstalled virtualenv-15.1.0

Seems fine. Then uninstall pip:

$ python -m pip uninstall pip
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Found existing installation: pip 20.0.2
Uninstalling pip-20.0.2:
  Would remove:
    /home/me/.local/bin/pip
    /home/me/.local/bin/pip2
    /home/me/.local/bin/pip2.7
    /home/me/.local/lib/python2.7/site-packages/pip-20.0.2.dist-info/*
    /home/me/.local/lib/python2.7/site-packages/pip/*
Proceed (y/n)? y
  Successfully uninstalled pip-20.0.2

$ pip --version
bash: /home/me/.local/bin/pip: No such file or directory
$ ls -a ~/.local/bin
.  ..  chardetect  easy_install  easy_install-2.7  flake8  pew  pipenv  pycodestyle  pyflakes  virtualenv-clone

That also seems fine. I'm not sure how bash knows that pip should be /home/me/.local/bin/pip since that file does not exist. Maybe bash has a cache? Anyway, next install pip and virtualenv from the debian 10 repo:

$ sudo apt install python-pip virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  python3-distutils python3-lib2to3 python3-virtualenv
The following NEW packages will be installed:
  python-pip python3-distutils python3-lib2to3 python3-virtualenv virtualenv

But when I try and check what version of virtualenv I now have, it fails:

$ virtualenv --version
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from virtualenv.__main__ import run_with_catch
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/__init__.py", line 3, in <module>
    from .run import cli_run
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/run/__init__.py", line 6, in <module>
    from virtualenv.run.app_data import AppDataAction
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/run/app_data.py", line 8, in <module>
    from virtualenv.util.lock import ReentrantFileLock
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/lock.py", line 11, in <module>
    from virtualenv.util.path import Path
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/path/__init__.py", line 3, in <module>
    from ._pathlib import Path
  File "/usr/local/lib/python2.7/dist-packages/virtualenv/util/path/_pathlib/__init__.py", line 42, in <module>
    from pathlib2 import Path
ImportError: No module named pathlib2

Best Answer

To avoid messing things up outside the virtualenvs, I recommend using the packaged versions:

sudo apt install python-pip virtualenv

(along with python3-pip for Python 3 support, if appropriate).

You’ll probably need to remove the versions of pip and virtualenv installed in your home directory, and any others on your PATH outside /usr/bin.

When setting up your virtualenvs, you can specify Python 2.7:

virtualenv -p /usr/bin/python2.7 ...

and virtualenv will do the right thing.

Related Question