对Python安装、模块安装和interp感到困惑吗

2024-09-26 23:23:11 发布

您现在位置:Python中文网/ 问答频道 /正文

所以昨天我不得不创建一个virtualenv,以便能够安装由于osx El Capitan的新SIP而无法安装的Python模块。我以为我做的每件事都对,但今天我得出了不同的结论。我希望我能说清楚。你知道吗

我的python自定义安装在myname/learnp/imdb\u模块,这是我用virtualenv创建它的地方。编辑:我后来把它移到myname/learnp/ayr2/imdb\u模块。你知道吗

但是,当我尝试运行解释器时,它似乎总是默认为库中的Python或类似的东西。我发现这一点是因为我在这个定制python环境中安装的某个模块无法导入,当我检查我拥有的模块时,它与我预期的不一样。你知道吗

此外,我想在定制virtualenv上安装的所有其他模块似乎都安装在主python env上,而我一直没有在定制env上安装这些模块。你知道吗

对不起,我现在很困惑。你知道吗

  • 我知道如何创建虚拟环境
  • 我知道如何激活它(它出现在终端线路的右侧)
  • 我不知道如何安装模块到我的虚拟环境
  • 我不知道如何让解释器从虚拟env运行,这样我就可以执行python操作,而这些操作只能通过使用自定义env模块来实现

任何建议都非常感谢!你知道吗

更新:

跟随Will Hogan's answer进行故障排除,我认为发生了一些奇怪的事情,引用我对他的回答的评论:

HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.

And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin


Tags: 模块thetoenvbinvirtualenvisusr
1条回答
网友
1楼 · 发布于 2024-09-26 23:23:11

在创建virtualenv时,您应该看到它正在安装setuptools和pip:

$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.

在确保virtualenv被激活后,您应该看到您的提示更改:

$ . ./testvenv/bin/activate
(testvenv)$ 

现在您可以确认python和pip的路径,它们应该在virtualenv中:

(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip

如果您没有看到python和pip位置在virtualenv的目录下,那么virtualenv还没有被激活。你知道吗

我还要确保,如果您直接执行.py文件(而不是使用“python foo.py”),shebang行使用:

#!/usr/bin/env python

甚至是到virtualenv的python的完整路径,例如:

#!/tmp/testvenv/bin/python

与之相反,比如说:

#!/usr/bin/python

第一个将在当前环境中搜索,这将由virtualenv激活设置。第二个明确指向virtualenv的“python”。你知道吗

相关问题 更多 >

    热门问题