在虚拟环境中,"sudo pip"位于链接到全局python pip [英] Inside virtual env, "sudo pip" links to the global python pip

查看:183
本文介绍了在虚拟环境中,"sudo pip"位于链接到全局python pip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用

(venv) vagrant@vagrant-ubuntu-trusty-64:~$ pip install <package_name>

我收到权限错误:

error: could not create '/home/vagrant/venv/lib/python2.7/site-packages/<package_name>': Permission denied

当我使用sudo安装时:

When I use sudo to install:

(venv) vagrant@vagrant-ubuntu-trusty-64:~$ sudo pip install <package_name>

安装成功,但是该软件包未安装在venv内部,而是安装在全局python目录中.

the install is successful, but the package is not installed inside venv, but instead inside the global python directory.

通过使用sudo并指定pip的路径,我可以在venv中成功安装该软件包:

I can successfully install the package inside venv by using sudo and specifying the path to pip:

(venv) vagrant@vagrant-ubuntu-trusty-64:~$ sudo /home/vagrant/venv/bin/pip install <package_name>

这是相当令人费解的.那么如何停止sudo pip链接到全局python pip?

This is quite convoluted though. So how can I stop sudo pip linking to the global python pip?

谢谢

推荐答案

根本问题是sudo在执行命令时默认情况下不会继承用户的环境.这就是您想要的-相信我.

The root problem is that sudo does not by default inherit the user's environment as it executes the command. This is what you want - trust me on this.

在您的情况下,您的pip要么被引导至无法写入的venv,要么在sudo下被引导至您不希望其存在的root的环境.

In your case, your pip is either guided to the venv that it can't write to or - under sudo - to root's environment where you don't want it to be.

您发布的解决方案实际上是有效的:如果您使用sudo,请确保准确告诉它要做什么,如何做以及向谁做!前面提到的所有这些都可以由用户的环境变量控制,因此请务必谨慎.

The solution you posted is actually valid: If you use sudo, be sure to tell it exactly what to do, how to do it and whom to do it to! All of the aforementioned can be controlled by the user's environment variables so caution is key.

您还可以使用sudo -E,它确实继承了呼叫用户的环境,因此应该保留您的venv.不过,请务必阅读sudo的手册页或对可能遇到的所有麻烦进行一些搜索.

You may also use sudo -E, which does inherit the calling user's environment and should therefore preserve your venv. Be sure to read sudo's man-page or do some googling about all the possible trouble you could get in, though.

这篇关于在虚拟环境中,"sudo pip"位于链接到全局python pip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆