如何避免“权限拒绝”时使用虚拟的 pip 与虚拟

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

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

以下列方式终止:

Error: could not create’/home/test/viralenvs/TestVirtualEnv/lib/python3.3/site-package/markdown2.py’: 权限被拒绝

我不能使用 sudo,因为它将在全局范围内安装软件包,而不是在虚拟环境中。I chowned site-packages; ls只显示与 easy_installpipsetuptools相关的目录,与 Markdown 无关。

如何使用 pip在虚拟环境中部署包而不会遇到与权限相关的错误?

144276 次浏览

virtualenv permission problems might occur when you create the virtualenv as sudo and then operate without sudo in the virtualenv.

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

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

-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-info is owned by root not macuser. That is why it was giving me permission denied error. I also had to remove /Users/macuser/.virtualenvs/armoury/lib/python2.7/site-packages/utils.egg-link as it was created by root as well. I did pip install -e . again after removing those, and it worked.

Solution:

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

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

This will probably fix your problem.

Cheers

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

mkvirtualenv hug
pip3 install hug -U


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

It worked after specifying python3:

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

You did not activate the virtual environment before using pip.

Try it with:

$(your venv path) . bin/activate

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

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.

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

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

If you created virtual environment using root then use this command

sudo su

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

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

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 python is symlinked to, i.e. run:

ls -l venv/bin/python

and make sure it points to the appropriate Python binary. For most systems this will be /usr/bin/python or /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 )

I was getting permission denied when trying to activate my virtual environment. I landed on this page trying to find solutions so perhaps this could also help others who are facing similar issues

source your_env_name_goes_here/bin/activate

I was using the wrong command (without the source), to activate my environment. If you're on zsh that's the correct command to use. If not, python docs has a table of the commands to use depending on your platform and shell (windows or mac, bash or powershell etc)

On centos 7 this worked:

first, create it

virtualenv --python=/usr/local/bin/python3.8 fastapi

then to activate

source fastapi/bin/activate