简体   繁体   中英

How can I solve “cannot import name 'main' from 'virtualenv'”?

When I try running virtualenv I get the following error:

[jelly@laptop Getskilled]$ virtualenv venv
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 6, in <module>
    from virtualenv import main
ImportError: cannot import name 'main' from 'virtualenv' (/home/jelly/.local/lib/python3.8/site-packages/virtualenv/__init__.py)

Virtualenv was working when I last used it for a project so I am guessing that an update cause it to break. I have tried reinstalling virtualenv and pip.

The closest post I could find was this one: virtualenv: cannot import name 'main'

I tried following this post so I ran the following in the python interpreter:

import virtualenv
virtualenv.__file__

Which returned: '/home/jelly/.local/lib/python3.8/site-packages/virtualenv/ init .py'

However, there was no file /usr/local/bin/virtualenv.py and there is no virtualenv.py in the.local directory so that solution in that post won't work for me.

What can I try next?

Update: I found virtualenv.py in /usr/bin/ and it seems like it is causing the problem but I'm not sure how to update it to work with the current version. I moved it then tried reinstalling virtualenv but that did not generate a new virtualenv.py so still not sure what's going on.

This happened to me when I installed the new Ubuntu 20.04 LTS. I renamed the existing virtualenv file to something else, and it started working again. Not exactly sure why, but it was advice from this answer: https://stackoverflow.com/a/32859811/2477292

sudo mv /usr/local/bin/virtualenv /usr/local/bin/xvirtualenv

I added a symbolic link /usr/bin/virtualenv pointing to /home/jelly/.local/bin/virtualenv and it seems to be working now:)

The error can be resolved by modifying the pip file

Check the location of the file:

$ which pip
path -> /usr/bin/pip

Go to that location /usr/bin/pip and open a terminal.

Enter: $ sudo nano pip

You can see:

from pip import main
if __name__ == '__main__':
     sys.exit(main())

Change to:

import sys
from pip import __main__
if __name__ == '__main__':
     sys.exit(__main__._main())

then Ctrl + O write the changes and exit.

Hope this will do!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM