Python 将pip与virtualenv一起使用时如何避免“权限被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19471972/
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-08-19 13:49:12  来源:igfitidea点击:

How to avoid "Permission denied" when using pip with virtualenv

pythonvirtualenvpip

提问by Arseni Mourzenko

I attempt to deploy a Python package with pipin a virtual environment on an Ubuntu machine, but encounter a permission-related issue. For example:

我尝试pip在 Ubuntu 机器上的虚拟环境中部署 Python 包,但遇到与权限相关的问题。例如:

(TestVirtualEnv)test@testServer:~$ pip install markdown2

terminates by:

终止于:

error: could not create '/home/test/virtualenvs/TestVirtualEnv/lib/python3.3/site-packages/markdown2.py': Permission denied

错误:无法创建“/home/test/virtualenvs/TestVirtualEnv/lib/python3.3/site-packages/markdown2.py”:权限被拒绝

I can't sudo, since it will install the package globally, and not within the virtual environment. I chowned site-packages; lsshows only directories related to easy_install, pipand setuptools, and nothing related to Markdown.

我不能sudo,因为它将全局安装包,而不是在虚拟环境中。我chown编辑site-packages; ls只显示目录有关easy_installpipsetuptools,并没有相关的降价。

How to deploy a package in a virtual environment with pipwithout encountering permission-related errors?

如何在pip不遇到与权限相关的错误的情况下在虚拟环境中部署包?

采纳答案by sebastian_oe

virtualenvpermission problems might occur when you create the virtualenvas sudoand then operate without sudoin the virtualenv.

virtualenv当您创建virtualenvassudo然后在没有sudovirtualenv.

As found out in your question's comment, the solution here is to createthe virtualenvwithout sudoto be able to work(esp. write) in it without sudo.

至于你的问题的评论发现,这里的解决方案是建立virtualenvsudo能够工作(尤其是写)在它没有sudo

回答by AliBZ

I didn't create my virtualenv using sudo. So Sebastian's answer didn't apply to me. My project is called utils. I checked utilsdirectory and saw this:

我没有使用 sudo 创建我的 virtualenv。所以塞巴斯蒂安的回答不适用于我。我的项目名为utils. 我检查了utils目录,看到了这个:

-rw-r--r--   1 macuser  staff   983  6 Jan 15:17 README.md
drwxr-xr-x   6 root     staff   204  6 Jan 14:36 utils.egg-info
-rw-r--r--   1 macuser  staff    31  6 Jan 15:09 requirements.txt

As you can see, utils.egg-infois owned by rootnot macuser. That is why it was giving me permission deniederror. I also had to remove /Users/macuser/.virtualenvs/armoury/lib/python2.7/site-packages/utils.egg-linkas it was created by rootas well. I did pip install -e .again after removing those, and it worked.

如您所见,utils.egg-inforootnot拥有macuser。这就是为什么它给我permission denied错误。我也必须删除,/Users/macuser/.virtualenvs/armoury/lib/python2.7/site-packages/utils.egg-link因为它也是由创建的rootpip install -e .删除这些后我又做了一次,它奏效了。

回答by Vingtoft

Solution:

解决方案:

If you created the virtualenv as root, run the following command:

如果您以 root 身份创建了 virtualenv,请运行以下命令:

sudo chown -R your_username:your_username path/to/virtuaelenv/

This will probably fix your problem.

这可能会解决您的问题。

Cheers

干杯

回答by nealmcb

In my case, I was using mkvirtualenv, but didn't tell it I was going to be using python3. I got this error:

就我而言,我使用的是mkvirtualenv,但没有告诉它我将使用 python3。我收到此错误:

mkvirtualenv hug
pip3 install hug -U

....
error: could not create '/usr/lib/python3.4/site-packages': Permission denied

It worked after specifying python3:

它在指定 python3 后工作:

mkvirtualenv --python=/usr/bin/python3 hug
pip3 install hug -U

回答by jmrueda

You did not activate the virtual environmentbefore using pip.

您在使用 pip 之前没有激活虚拟环境

Try it with:

试试看:

$(your venv path) . bin/activate

And then use pip -r requirements.txt on your main folder

然后在您的主文件夹上使用 pip -r requirements.txt

回答by Vkreddy Komatireddy

While creating virtualenv if you use sudo the directory is created with root privileges.So when you try to install a package with non-sudo user you won't have permission to install into it. So always create virtualenv without sudo and install without sudo.

如果您使用 sudo 创建 virtualenv,则该目录是使用 root 权限创建的。因此,当您尝试使用非 sudo 用户安装软件包时,您将无权安装到其中。所以总是在没有 sudo 的情况下创建 virtualenv 并在没有 sudo 的情况下安装。

You can also copy packages installed on global python to virtualenv.

您还可以将安装在全局 python 上的包复制到 virtualenv。

cp -r /lib/python/site-packages/* virtualenv/lib/python/site-packages/

回答by Dinesh Chandra Kumawat

If you created virtual environment using root then use this command

如果您使用 root 创建了虚拟环境,则使用此命令

sudo su

it will give you the root access and then activate your virtual environment using this

它将为您提供 root 访问权限,然后使用它激活您的虚拟环境

source /root/.env/ENV_NAME/bin/activate

回答by Sam Mason

I've also had this happen (by accident) after creating a new venv while inside an existing virtual environment. an easy way to diagnose this would be to see where the pythonis symlinked to, i.e. run:

在现有虚拟环境中创建新的 venv 后,我也发生了这种情况(偶然)。诊断此问题的一种简单方法是查看python符号链接到的位置,即运行:

ls -l venv/bin/python

and make sure it points to the appropriate Python binary. For most systems this will be /usr/bin/pythonor /usr/bin/python3. while if it points to an existing virtual environment it'll be something like /home/youruser/somedir/bin/python. if it's the latter than I'd suggest recreating the venv while making sure that you aren't "inside" any existing virtualenv (i.e. run deactivate)

并确保它指向适当的 Python 二进制文件。对于大多数系统,这将是/usr/bin/python/usr/bin/python3。而如果它指向现有的虚拟环境,它将类似于/home/youruser/somedir/bin/python. 如果是后者,我建议重新创建 venv,同时确保您不在任何现有的 virtualenv 中(即运行deactivate