[python] How to avoid "Permission denied" when using pip with virtualenv

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

(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

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

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

This question is related to python virtualenv pip

The answer is


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


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 )


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

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/

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.


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


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

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to virtualenv

How to setup virtual environment for Python in VS Code? Conda version pip install -r requirements.txt --target ./lib What is the purpose of "pip install --user ..."? What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? ImportError: No module named 'encodings' how to specify new environment location for conda create Use virtualenv with Python with Visual Studio Code in Ubuntu Is it ok having both Anacondas 2.7 and 3.5 installed in the same time? How to uninstall a package installed with pip install --user Unable to install boto3

Examples related to pip

How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? What is the meaning of "Failed building wheel for X" in pip install? Could not install packages due to an EnvironmentError: [Errno 13] How do I install Python packages in Google's Colab? Conda version pip install -r requirements.txt --target ./lib pip: no module named _internal AttributeError: Module Pip has no attribute 'main' Error after upgrading pip: cannot import name 'main'