1

I am trying to install packages into a virtual environment on Ubuntu 20.04. I have sudo access but have read it is bad practice to pip install with sudo, so I am trying to install into the venv.

I have activated the venv: (flask-prod-venv) iamurray@sjc45d1cetap001:/var/www/flask-prod$, but when I run pip3 install flask, I get the following:

Collecting flask
  Using cached Flask-2.1.2-py3-none-any.whl (95 kB)
Collecting importlib-metadata>=3.6.0
  Using cached importlib_metadata-4.11.4-py3-none-any.whl (18 kB)
Collecting click>=8.0
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting Jinja2>=3.0
  Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting Werkzeug>=2.0
  Using cached Werkzeug-2.1.2-py3-none-any.whl (224 kB)
Collecting itsdangerous>=2.0
  Using cached itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting zipp>=0.5
  Using cached zipp-3.8.0-py3-none-any.whl (5.4 kB)
Collecting MarkupSafe>=2.0
  Using cached MarkupSafe-2.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: zipp, Werkzeug, MarkupSafe, itsdangerous, click, Jinja2, importlib-metadata, flask

ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/var/www/flask-prod/flask-prod-venv/lib/python3.8/site-packages/zipp.py'
Check the permissions.

Then I go to check the permissions of this zipp.py file that appears to be the issue, but I can't find it:

(flask-prod-venv) iamurray@sjc45d1cetap001:/var/www/flask-prod/flask-prod-venv/lib/python3.8/site-packages$ ls -a
.   _distutils_hack           Flask  pip-22.1.2.dist-info   pkg_resources  setuptools-62.3.4.dist-info   _virtualenv.pth  wheel                   wheel-0.37.1.virtualenv
..  distutils-precedence.pth  pip    pip-22.1.2.virtualenv  setuptools     setuptools-62.3.4.virtualenv  _virtualenv.py   wheel-0.37.1.dist-info

Trying to just change permissions produces an error (that makes sense based on the absence of zipp.py in the above output)

(flask-prod-venv) iamurray@sjc45d1cetap001:/var/www/flask-prod/flask-prod-venv/lib/python3.8/site-packages$ chmod 777 zipp.py
chmod: cannot access 'zipp.py': No such file or directory

Could someone please help me install packages into this virtual environment? I am trying to set up an apache server with WSGI. Thank you!

Ian Murray
  • 65
  • 6
  • 2
    /var/ is owned by root so installing anything into it will need sudo, which must have been used to create '/var/www/flask-prod/flask-prod-venv/lib/python3.8/site-packages'. It would be better if you could base the project somewhere in your home dir. Or you could take ownership of the 'site-packages' dir with chown (not sure if that's safe in a system dir), or just use sudo to install... Also pip is probably trying to install zipp.py first, so that's why it fails on that module, probably nothing to do with permissions on the file itself. – Sean C Jun 21 '22 at 01:39
  • @SeanC Thank you for the detailed reply - I've changed the location to my /home directory, and created a fresh venv there. I'm still getting the permissions error, with zipp.py being the culprit. Do you know a workaround for the zipp.py issue? I'm googling it, but if you know a way that would be appreciated. Thank you! – Ian Murray Jun 21 '22 at 16:57
  • 1
    Sorry, no - I just installed flask into a venv in my home dir using pipenv and all dependencies including zipp installed fine - "Flask==2.1.2 - click [required: >=8.0, installed: 8.1.3] - importlib-metadata [required: >=3.6.0, installed: 4.11.4] - zipp [required: >=0.5, installed: 3.8.0] <--------------- - itsdangerous [required: >=2.0, installed: 2.1.2] - Jinja2 [required: >=3.0, installed: 3.1.2] - MarkupSafe [required: >=2.0, installed: 2.1.1] - Werkzeug [required: >=2.0, installed: 2.1.2] " . Other than that I'm lost :/ – Sean C Jun 21 '22 at 17:12
  • 1
    just athought - clear your pip cache (don't ask me how!) then try reinstalling flask again - perhaps pip is using a **cached** version of zipp which may have changed permissions? – Sean C Jun 21 '22 at 17:14
  • I've found a (workaround?) solution, where I temporarily elevate my permissions (using sudo su), and then perform the pip install. It puts everything into my venv, and doesn't produce the error warning about root package conflicts. More details linked here: https://stackoverflow.com/a/69385507/18174191 – Ian Murray Jun 21 '22 at 17:17
  • 1
    Cool - clear pip cache: https://pip.pypa.io/en/stable/cli/pip_cache/ . Also is it worth lookig at pipenv to handle the virt env and dependency stuff? Works great for me and no sudo needed. – Sean C Jun 21 '22 at 17:20
  • 1
    pipenv: https://realpython.com/pipenv-guide/ – Sean C Jun 21 '22 at 17:21
  • 1
    Also ```pip install xyz --user``` doesn't need sudo and installs in your home dir for **you only** so uninstalling virtualenv and then reinstalling ```python3 -m pip install --user virtualenv ``` should remove any need for sudo anywhere – Sean C Jun 21 '22 at 17:25

0 Answers0