question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pip ignores the site.ENABLE_USER_SITE value if user is set explicitly (asymetric with venv behaviour)

See original GitHub issue

Environment

  • pip version: 20.2.0 & master
  • Python version: all
  • OS: all

Description

When we have a venv or virtualenv, and site.ENABLE_USER_SITE is False, and try to pip install --user and error is raised (InstallationError). If we do the same with a non-virtual environment, the ENABLE_USER_SITE value is ignored.

$ python -m venv /tmp/venv
$ source /tmp/venv/bin/activate
(venv) $ PYTHONNOUSERSITE=1 pip install --user typing-extensions
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

But with a base environment (i.e. not virtual):

(venv) $ deactivate
$ PYTHONNOUSERSITE=1 pip -v install --user typing-extensions
Using pip 20.2.2 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
User install by explicit request
...
Collecting typing-extensions
  Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Installing collected packages: typing-extensions
Successfully installed typing-extensions-3.7.4.3

Expected behavior

I expect the site.ENABLE_USER_SITE to always be honoured

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
uranusjrcommented, Aug 24, 2020

I think this depends on your interpretation to “disabled”. Does it mean that no Python tools can/should put stuffs inside the site, or simply that Python won’t load it into sys.path for import? pip’s current behaviour basically means that it’s installing into a location that’s neither discoverable nor importable (by default), i.e. implying the second interpretation.

IMO the linked Superuser question is fundamentally misguided. The only way to really prevent users from pip install-ing stuff into their user directories is to disable their filesystem write permission, since pip can always install to any arbitrary locations by specifying --prefix and --target. The --user option is not any different. The reason pip explicitly errors out inside a virtual environment is for UX reasons (a pip inside venv is not supposed to be able to modify the non-venv interpreter), and the same UX problem does not exist for site.ENABLE_USER_SITE = False since the installed packages are not visible anyway.

(To be explicit: I’m not saying your interpretation is wrong, just that pip’s behaviour is also not unreasonable.)

1reaction
uranusjrcommented, Aug 30, 2020

This whole thing came up because I found it unusual to be able to disable user site-packages (from the sys.path), and yet still be able to install stuff into it with the --user flag. If there isn’t any motivation to normalise what I would consider to be asymmetric behaviour, I’m completely OK with closing the issue down as a wontfix.

Honestly I doubt the initial implementation actually put much thought in comparing approaches, and simply went with the most simplistic one (the current behaviour). I would welcome a PR containing a change of behaviour, with accompanying documentation explaining the thought process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pip default behavior conflicts with virtualenv? - Stack Overflow
When I got to virtualenv flask command, I received this error message: Can not perform a '--user' install. User site-packages are not visible...
Read more >
venv — Creation of virtual environments — Python 3.11.1 ...
When a virtual environment has been activated, the VIRTUAL_ENV environment variable is set to the path of the environment. Since explicitly activating a...
Read more >
How can I block pip and python user packages
If this is set, Python won't add the user site-packages directory to sys.path. So even though I was surprised by the behaviour, I...
Read more >
Pipenv: A Guide to the New Python Packaging Tool
Otherwise, Pipenv will use whatever default virtualenv finds. Sidenote: If you require a more specific version of Python, you can provide a --python...
Read more >
Advanced Usage of Pipenv - Python Packaging Authority
Pipenv will expand environment variables (if defined) in your Pipfile. ... To reuse Conda–installed Python packages, use the --site-packages flag: $ pipenv ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found