python 使用 virtualenv 导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1909025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:19:12  来源:igfitidea点击:

Import error with virtualenv

pythonvirtualenv

提问by espenhogbakk

I have a problem with virtualenv. I use it regulary, I use it on my development machine and on several servers. But on this last server I tried to use i got a problem.

我对 virtualenv 有问题。我经常使用它,我在我的开发机器和几台服务器上使用它。但是在我尝试使用的最后一个服务器上,我遇到了问题。

I created a virtualenv with the --no-site-packages argument, and then I installed some python modules inside the virtualenv. I can confirm that the modules is located inside the virtualenvs site-packages and everything seems to be fine.

我使用 --no-site-packages 参数创建了一个 virtualenv,然后在 virtualenv 中安装了一些 python 模块。我可以确认这些模块位于 virtualenvs 站点包内,并且一切似乎都很好。

But when i try to do:source virtualenv/bin/activateand then import one of the module python import modulenamei get an import error that says that the module doesnt exist. How is it that this is happending? It seems like it never activates even thoug that it says it do.

但是当我尝试这样做:source virtualenv/bin/activate然后导入其中一个模块时,python import modulename我收到一个导入错误,指出该模块不存在。这是怎么回事?即使它说它会激活,它似乎也永远不会激活。

Anybody have a clue on how to fix this?

有人知道如何解决这个问题吗?

回答by Carl Meyer

Is there a bash alias active on this machine for "python", by any chance? That will take priority over the PATH-modifications made by activate, and could cause the wrong python binary to be used.

这台机器上是否有一个 bash 别名为“python”而活动?这将优先于激活所做的 PATH 修改,并可能导致使用错误的 python 二进制文件。

Try running virtualenv/bin/python directly (no need to activate) and see if you can import your module.

尝试直接运行 virtualenv/bin/python (无需激活),看看是否可以导入您的模块。

If this fixes it, you just need to get rid of your python bash alias.

如果这解决了它,你只需要摆脱你的 python bash 别名。

回答by codeape

After activating the virtual env, try:

激活虚拟环境后,尝试:

$ python
>>> import sys
>>> sys.executable
...

... and see if you are running the expected executable.

...并查看您是否正在运行预期的可执行文件。

Also check:

还要检查:

>>> sys.path
[...]

回答by Jeff Peck

IIRC, the activate script just puts your virtual env first on your path, so when you type "python" it finds the one in your virtual env first. If the activate script fails, you can always edit your path manually. Also - go here and search for "activate": http://pylonsbook.com/en/1.1/installing-pylons.html#setting-up-a-virtual-python-environment. This will tell you why the activate command can fail.

IIRC,激活脚本只是把你的虚拟环境放在你的路径上,所以当你输入“python”时,它首先在你的虚拟环境中找到那个。如果激活脚本失败,您始终可以手动编辑路径。另外 - 去这里搜索“激活”:http: //pylonsbook.com/en/1.1/installing-pylons.html#setting-up-a-virtual-python-environment。这将告诉您为什么 activate 命令会失败。