Python在虚拟环境中找不到包 [英] Python can't find packages in Virtual Environment
问题描述
我正在尝试为一个项目设置我的环境,但 python 无法找到我使用 pip 安装的模块.
I'm trying to setup my environment for a project but python isn't able to find the modules I've installed with pip.
我做了以下事情:
mkdir helloTwitter
cd helloTwitter
virtualenv myenv
Installing setuptools, pip, wheel...done.
source myenv/bin/activate
pip install tweepy
Collecting tweepy
Using cached tweepy-3.5.0-py2.py3-none-any.whl
Collecting six>=1.7.3 (from tweepy)
Using cached six-1.10.0-py2.py3-none-any.whl
Collecting requests>=2.4.3 (from tweepy)
Using cached requests-2.11.1-py2.py3-none-any.whl
Collecting requests-oauthlib>=0.4.1 (from tweepy)
Using cached requests_oauthlib-0.6.2-py2.py3-none-any.whl
Collecting oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
Installing collected packages: six, requests, oauthlib, requests-oauthlib, tweepy
Successfully installed oauthlib-2.0.0 requests-2.11.1 requests-oauthlib-0.6.2 six-1.10.0 tweepy-3.5.0
当我尝试导入模块时,它说找不到.
When I try to import the module it says it cannot be found.
$PATH 中的第一个条目是 helloTwitter/myenv/bin
所有包都显示在环境站点包目录中.我似乎使用了正确的 python 和 pip.哪个 python 输出 helloTwitter/myenv/bin/python
哪个 pip 输出 helloTwitter/myenv/bin/pip
The first entry in $PATH is helloTwitter/myenv/bin
All the packages are showing up in the environments site-packages directory.
I seem to be using the right python and pip.
Which python outputs helloTwitter/myenv/bin/python
Which pip outputs helloTwitter/myenv/bin/pip
对我哪里出错有什么建议吗?
Any advice on where I'm going wrong?
推荐答案
看起来您正在手动设置 $PATH
以指向您的虚拟环境.myenv/bin/activate
脚本的全部意义在于为您解决这个问题.
It looks like you're manually setting your $PATH
to point to your virtual environment. The whole point of the myenv/bin/activate
script is to take care of this for you.
激活虚拟环境后,您使用 pip 安装的任何包都将放置在相关的 venv site-packages
目录中(在您的情况下为 myenv/lib/python2.7/site-packages
).当您在虚拟环境中工作时(假设默认行为),诸如 pip --user
之类的东西是不必要的.一切都是自动的.
Once you have activated your virtual environment, any package you install using pip will be placed in the relevant venv site-packages
directory (in your case, myenv/lib/python2.7/site-packages
). Things like pip --user
are unnecessary when you are working in a virtual environment (assuming default behaviour). It's all automatic.
运行activate
后,您可以使用find -iname tweepy
检查您正在使用的python 二进制文件.
After running activate
, you can check the python binary you are using with find -iname tweepy
.
别名也会导致问题.which
是一个外部命令,不会总是选择这些.type -a python
将清除这些.
Aliases can cause issues too. which
is an external command, and won't always pick these up. A type -a python
will flush these out.
可以通过直接运行 helloTwitter/myenv/bin/python -c 'import tweepy'
来完成快速测试.如果这与您当前运行的 Python 不同(即不引发导入异常),那么这就是您的问题.
A quick test can be done by running helloTwitter/myenv/bin/python -c 'import tweepy'
directly. If this behaves differently to however you are currently running python (i.e. doesn't throw an import exception), then this is your problem.
希望有所帮助.
这篇关于Python在虚拟环境中找不到包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!