简体   繁体   English

如何解决“无法从'virtualenv'导入名称'main'”?

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

When I try running virtualenv I get the following error:当我尝试运行 virtualenv 时,出现以下错误:

[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.当我上次将 Virtualenv 用于项目时,它正在工作,所以我猜测更新会导致它崩溃。 I have tried reinstalling virtualenv and pip.我尝试重新安装 virtualenv 和 pip。

The closest post I could find was this one: virtualenv: cannot import name 'main'我能找到的最接近的帖子是: virtualenv: cannot import name 'main'

I tried following this post so I ran the following in the python interpreter:我尝试关注这篇文章,所以我在 python 解释器中运行了以下内容:

import virtualenv
virtualenv.__file__

Which returned: '/home/jelly/.local/lib/python3.8/site-packages/virtualenv/ init .py'返回:'/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.但是,没有文件 /usr/local/bin/virtualenv.py 并且 .local 目录中没有 virtualenv.py ,因此该帖子中的解决方案对我不起作用。

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.更新:我在 /usr/bin/ 中找到了 virtualenv.py,它似乎是导致问题的原因,但我不确定如何更新它以使用当前版本。 I moved it then tried reinstalling virtualenv but that did not generate a new virtualenv.py so still not sure what's going on.我移动了它,然后尝试重新安装 virtualenv,但这并没有生成新的 virtualenv.py,所以仍然不确定发生了什么。

This happened to me when I installed the new Ubuntu 20.04 LTS.这发生在我安装新的 Ubuntu 20.04 LTS 时。 I renamed the existing virtualenv file to something else, and it started working again.我将现有的 virtualenv 文件重命名为其他文件,然后它又开始工作了。 Not exactly sure why, but it was advice from this answer: https://stackoverflow.com/a/32859811/2477292不完全确定为什么,但这是来自这个答案的建议: 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:)我添加了一个指向 /home/jelly/.local/bin/virtualenv 的符号链接 /usr/bin/virtualenv ,它现在似乎可以工作了:)

The error can be resolved by modifying the pip file该错误可以通过修改pip文件来解决

Check the location of the file:检查文件的位置:

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

Go to that location /usr/bin/pip and open a terminal. Go 到该位置 /usr/bin/pip 并打开一个终端。

Enter: $ sudo nano pip输入:$ 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.然后 Ctrl + O 写入更改并退出。

Hope this will do!!希望这会做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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