virtualenv: cannot import name 'main'
Similarly to some others here, I had multiple installations of virtualenv. Not sure where the extra one came from, but I had these two:
/usr/local/bin/virtualenv
/usr/bin/virtualenv
One is from apt install of virtualenv, the other from pip install of virtualenv.
This happened when upgrading to Ubuntu 20.04.
I received this error after upgrading Ubuntu 18.04 LTS to 20.04 LTS. So there were two problems all at once. First the python version was still running 2.x and doing a simple update or try to uninstall (apt-get remove virtualenv
) of virtualenv did not help at all. But I found a solution. First let 20.04 LTS 'know' the times of using old python is over:
sudo apt-get install python-is-python3
Then test it and open a console to get the version string with python -V
; by now it should be showing something like Python 3.8.5. Fine.
Next step is to solve the virtualenv
problem. I tried to find out, which executable was run with which virtualenv
and it showed: $HOME/.local/bin/virtualenv
. Hmmkay, somehow the system wasn't using the /usr/bin/virtualenv
executable. I thought maybe I let the directory become invisible (a.k.a. renaming) and maybe the system will go on a hunt for an alternative virtualenv
running:
mv $HOME/.local/bin/virtualenv /home/USER/.local/bin/virtualenv_OLD
Then I simply changed into a playground-directory and ran virtualenv donaldknuth
and behold - it worked. To be sure I ran another which virtualenv
and the system returned a /usr/bin/virtualenv
. Last check to do was activating the new virtual environment:
source $HOME/playground/donaldknuth/bin/activate
The terminal changed and it worked fine. Solution
EDIT:
Based on Pierre B.'s suggestion you may have to restart your Shell. The command hash -d virtualenv
will delete the stored location of virtualenv
from the shell's cache and determine the correct path right now. (Sources: https://www.computerhope.com/unix/bash/hash.htm, https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables)
After my upgrade to Fedora 32 I had the same issue which lead me to this question:
ImportError: cannot import name 'main' from 'virtualenv'
In my case I actually seemed to have both /usr/local/bin/virtualenv
as well as $HOME/.local/lib/python3.8/site-packages/virtualenv/__init__.py
.
Removing the user virtualenv version and reinstalling it into the system with root fixed the issue:
pip uninstall virtualenv
sudo pip install virtualenv
Your virtualenv executable /usr/local/bin/virtualenv
is importing the virtualenv package /usr/local/bin/virtualenv.py
. My guess is that package is not the one the executable should really be importing. The reason it is choosing that one is because it is in the same directory.
First, check where the real virtualenv package is. In the python3 terminal:
>>> import virtualenv
>>> virtualenv.__file__
If it is not /usr/local/bin/virtualenv.py
, then the simplest way to get /usr/local/bin/virtualenv
to import it instead of /usr/local/bin/virtualenv.py
is to delete /usr/local/bin/virtualenv.py
(or so you can easily undo this if it doesn't work, simply rename virtualenv.py
to something else like xvirtualenvx.py
).