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 chown
ed 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?
In my case, I was using
mkvirtualenv
, but didn't tell it I was going to be using python3. I got this error:It worked after specifying python3:
I didn't create my virtualenv using sudo. So Sebastian's answer didn't apply to me. My project is called
utils
. I checkedutils
directory and saw this:As you can see,
utils.egg-info
is owned byroot
notmacuser
. That is why it was giving mepermission denied
error. I also had to remove/Users/macuser/.virtualenvs/armoury/lib/python2.7/site-packages/utils.egg-link
as it was created byroot
as well. I didpip install -e .
again after removing those, and it worked.virtualenv
permission problems might occur when you create thevirtualenv
assudo
and then operate withoutsudo
in thevirtualenv
.As found out in your question's comment, the solution here is to create the
virtualenv
withoutsudo
to be able to work (esp. write) in it withoutsudo
.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: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. rundeactivate
)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.
Solution:
If you created the virtualenv as root, run the following command:
This will probably fix your problem.
Cheers