首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >虚拟环境下的pip需要sudo

虚拟环境下的pip需要sudo
EN

Stack Overflow用户
提问于 2015-06-22 18:22:03
回答 1查看 2.9K关注 0票数 0

我在OSX上,通过virtualfish设置了一个名为ds-mapreduce-env的python virtualfish。在我的项目目录中激活了venv之后,它看起来选择了正确的pippython

代码语言:javascript
复制
~/d/ds-mapreduce (venv) master ➜ which pip
/Users/cody/.virtualenvs/ds-mapreduce-env/bin/pip
~/d/ds-mapreduce (venv) master ➜ which python
/Users/cody/.virtualenvs/ds-mapreduce-env/bin/python

但是当我运行pip install时,它并没有像预期的那样安装到虚拟环境中。然而,当我使用sudo时,它确实是这样的。

代码语言:javascript
复制
~/d/ds-mapreduce (venv) master ➜ pip install geonamescache
Requirement already satisfied (use --upgrade to upgrade): geonamescache in /usr/local/lib/python2.7/site-packages

~/d/ds-mapreduce (venv) master ➜ sudo pip install geonamescache
The directory '/Users/cody/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/cody/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting geonamescache
Installing collected packages: geonamescache
Successfully installed geonamescache-0.18

我猜这是某种权限问题,但我想不出来。我用brew install python安装了brew install python,用pip install virtualfish安装了virtualfish。我试着拆掉和重新安装都没有效果。

我的解决方案

我的问题是我的PYTHONPATH。我把/usr/local/lib/python2.7/site-packages加到了我的PYTHONPATH上。移除它解决了问题!

EN

回答 1

Stack Overflow用户

发布于 2015-06-23 19:27:26

第一步是卸载全局安装的包:

代码语言:javascript
复制
pip uninstall -y geonamescache

通过对/usr/local/lib/python2.7/site-packages/的检查,确认它已被完全拆除。如果没有,手动移除你在那里找到的任何相关碎屑。

下一步是更好地将虚拟环境的site-packages与全局site-packages隔离开来。将以下内容添加到~/.config/fish/config.fish

代码语言:javascript
复制
set -x PIP_REQUIRE_VIRTUALENV "true"

如果您打开了一个新的终端会话,那么如果您运行的pip install geonamescache没有激活的虚拟环境--这正是我们想要的--那么您现在应该得到一个错误。通过vf new ds-mapreduce-env创建和激活一个新的虚拟环境,然后再次尝试相同的pip install geonamescache调用,并且包应该安装到虚拟环境中。

由于这将阻止您将任何东西安装到全局site-packages中,因此您可能希望设置一个别名,该别名允许您覆盖此隔离并执行全局pip操作。将以下函数添加为~/.config/fish/functions/gpip.fish

代码语言:javascript
复制
function gpip -d "Manage globally-installed Python packages"
    env PIP_REQUIRE_VIRTUALENV="" pip $argv
end

这样,您就可以(例如)通过以下方式升级您的全局包:

代码语言:javascript
复制
gpip install --upgrade setuptools pip wheel virtualenv

以上大部分内容已经通过Tacklebox + 铲球 (查找pip插件)来解决,这些项目的目的是方便地向fish环境添加增强功能。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30987118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档