I\'m having a little trouble with virtualenv on Mac OS X Yosemite. After I couldn\'t run virtualenv at all first, I installed Python 3 via brew
(previously I in
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
).
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)
On Linux Mint 20, I had to switch default Python interpreter to python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Then remove existing virtualenv
and reinstall via pip
and python3
:
rm ~/.local/bin/virtualenv
apt remove python3-virtualenv
sudo pip install virtualenv
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