Unable to uninstall virtualenv from debian 11

My debian OS has been through a few upgrades and is now on 11 (Bullseye). I initially started out with python2 and now I no longer want it – I just want python3. I installed virtualenv a long while back when I was still working with python2.

I have done a full dist-upgrade:

$ sudo apt-get update
Hit:1 http://deb.debian.org/debian bullseye InRelease
Get:2 https://deb.nodesource.com/node_14.x bullseye InRelease [4,586 B]
Get:3 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Hit:4 http://deb.debian.org/debian bullseye-updates InRelease
Fetched 53.0 kB in 1s (49.9 kB/s)
Reading package lists... Done
$ sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  libts-dev
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

And when I check if virtualenv is installed it says yes:

$ which virtualenv
/usr/local/bin/virtualenv

I try uninstalling it but pip tells me it is not installed:

$ pip uninstall virtualenv
WARNING: Skipping virtualenv as it is not installed
$ sudo pip uninstall virtualenv
WARNING: Skipping virtualenv as it is not installed.
$ pip3 uninstall virtualenv
WARNING: Skipping virtualenv as it is not installed
$ sudo pip3 uninstall virtualenv
WARNING: Skipping virtualenv as it is not installed.

I also tried uninstalling with the package manager in case it was installed that way

$ sudo apt-get remove virtualenv --purge
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'virtualenv' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

But after all this I can still see that it is installed:

$ which virtualenv
/usr/local/bin/virtualenv

The reason I want to remove it is that it has an error when i try running it. And I’m thinking fully removing it and installing again will fix this:

$ virtualenv .
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in <module>
    from virtualenv import main
ModuleNotFoundError: No module named 'virtualenv'

Should I just delete /usr/local/bin/virtualenv? That seems like a bad idea. How can I remove virtualenv using a package manager?

Update 1 – try installing virtualenv using pip3

The installation is successful but virtualenv still fails to run without errors:

$ pip3 install virtualenv
Defaulting to user installation because normal site-packages is not writeable
Collecting virtualenv
  Using cached virtualenv-20.25.1-py3-none-any.whl.metadata (4.4 kB)
Requirement already satisfied: distlib<1,>=0.3.7 in ./.local/lib/python3.9/site-packages (from virtualenv) (0.3.8)
Requirement already satisfied: filelock<4,>=3.12.2 in ./.local/lib/python3.9/site-packages (from virtualenv) (3.13.3)
Requirement already satisfied: platformdirs<5,>=3.9.1 in ./.local/lib/python3.9/site-packages (from virtualenv) (4.2.0)
Using cached virtualenv-20.25.1-py3-none-any.whl (3.8 MB)
Installing collected packages: virtualenv
  WARNING: The script virtualenv is installed in '/home/me/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed virtualenv-20.25.1
$ which virtualenv
/usr/local/bin/virtualenv
$ virtualenv --version
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in <module>
    from virtualenv import main
ImportError: cannot import name 'main' from 'virtualenv' (/home/me/.local/lib/python3.9/site-packages/virtualenv/__init__.py)
$ virtualenv .
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in <module>
    from virtualenv import main
ImportError: cannot import name 'main' from 'virtualenv' (/home/me/.local/lib/python3.9/site-packages/virtualenv/__init__.py)

And now virtualenv exists in 2 places on the filesystem:

$ which virtualenv
/usr/local/bin/virtualenv
$ sudo find /usr/ -iname "virtualenv" | grep bin
/usr/local/bin/virtualenv
$ sudo find /home/me/.local/ -iname "virtualenv" | grep bin
/home/me/.local/bin/virtualenv
$ ls -l /home/me/.local/bin/virtualenv 
-rwxr-xr-x 1 me me 238 Apr  7 13:06 /home/me/.local/bin/virtualenv
Asked By: mulllhausen

||

The system package manager doesn’t manage /usr/local.

Given that pip can’t uninstall virtualenv for you, your best option now is

sudo rm /usr/local/bin/virtualenv

There may be other remnants elsewhere in /usr/local, but they won’t cause any harm. You can look for them later.

Note that in Debian 11, virtualenv is available in the python3-virtualenv package.

Answered By: Stephen Kitt
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.