15

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 installed it via the package on python.org). I linked this installation of python3, updated pip and ran pip3 install virtualenv. When I try to run virtualenv (e.g. $ virtualenv --python=python3 ../virtualenv), I get the following error message.

Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in <module>
    from virtualenv import main
  File "/usr/local/bin/virtualenv.py", line 7, in <module>
    from virtualenv import main
ImportError: cannot import name 'main'

Can anybody help me with this?

5 Answers5

18

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
Friedrich Große
  • 2,383
  • 19
  • 19
  • This is not the right way to go about it. User may not want to install virtualenv in the system folders and contain everything in home folder – Sreeram Boyapati Sep 16 '20 at 08:21
10

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).

hansmosh
  • 491
  • 5
  • 13
  • 1
    That other file is probably left over from some other attempt at installing virtualenv or from another version of python. If you open it up, you'll probably see that it's the same file as the file that you're executing. It might be tied to a different version of python, which you would see in the shebang at the beginning of the script. – hansmosh Sep 30 '15 at 16:32
8

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)

Semo
  • 783
  • 2
  • 17
  • 38
  • 1
    Solved it for me. Not that using bash you may need to restart your bash session or run `hash -r` as cache would point to old `virtualenv` path (see https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables) – Pierre B. Nov 30 '20 at 10:32
  • 1
    Great. Thank you. – GhitaB Jun 07 '21 at 08:03
  • Why that weird (and wrong) command? You probably want `$USER` instead of `USER`, but it's more consistent to pass the root path the same in both instances (source and target): `mv $HOME/.local/bin/virtualenv $HOME/.local/bin/virtualenv_OLD` – Chris Jun 27 '21 at 19:20
  • 1
    worked for me. in my case I had to install specific python /pip versions. After removing from /usr/local/bin , it picked up virtualenv from /usr/bin which linked to the right dist-packages folder – r11 Apr 13 '23 at 17:58
2

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.

raahlb
  • 195
  • 2
  • 10
1

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
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178