Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't override require-virtualenv config option via CLI #5528

Closed
justinmayer opened this issue Jun 25, 2018 · 16 comments
Closed

Can't override require-virtualenv config option via CLI #5528

justinmayer opened this issue Jun 25, 2018 · 16 comments
Labels
auto-locked Outdated issues that have been locked by automation C: configuration Configuration management and loading good first issue A good item for first time contributors to work on type: docs Documentation related

Comments

@justinmayer
Copy link

Environment

  • pip version: 10.0.1
  • Python version: 2.7.14
  • OS: macOS 10.13.5

Pip bundled along with Homebrew Python, subsequently upgraded to 10.0.1

Description
I always want pip install to require an activated virtual environment, unless I override that default on a per-invocation basis. In Pip 9.x it was possible to override the require-virtualenv default via the PIP_REQUIRE_VIRTUALENV environment variable, but Pip 10 does not respect that environment variable nor seem to provide any way to override require-virtualenv on a per-invocation basis.

Expected behavior
I expected Pip to respect the PIP_REQUIRE_VIRTUALENV environment variable and/or provide some other way to override the require-virtualenv setting.

How to Reproduce
With a Pip configuration file including:

[install]
require-virtualenv = true

... override via environment variable and install the desired package to global site-packages:

env PIP_REQUIRE_VIRTUALENV="" pip install some-package-I-want-in-global-site-packages

Output
The above command behaved as expected up until Pip 10. When I attempt this on Pip 10, however, the PIP_REQUIRE_VIRTUALENV environment variable does not appear to be honored. Instead, an error is returned:

Could not find an activated virtualenv (required).

Additional Notes
If there is a flag or some other way to override the require-virtualenv setting on a per-invocation basis, I cannot find it and would appreciate a pointer to its existence. Otherwise, I would prefer and humbly request that the ability to do so be restored in some manner or other.

@moshez
Copy link

moshez commented Jul 14, 2018

I can confirm this is a regression:

moshez@mcgyver:~/src/pybay-2018-web-a-to-z$ env PIP_REQUIRE_VIRTUALENV='' pip install --upgrade pip
Collecting pip
  Downloading http://localhost:3141/root/pypi/+f/717/cdffb2833be84/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 43.0MB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-10.0.1
moshez@mcgyver:~/src/pybay-2018-web-a-to-z$ env PIP_REQUIRE_VIRTUALENV='' pip install --upgrade pip
Could not find an activated virtualenv (required).

@moshez
Copy link

moshez commented Jul 14, 2018

moshez@mcgyver:~/src/pip$ env PIP_REQUIRE_VIRTUALENV='0' PYTHONPATH=src python -m pip install twisted
Looking in indexes: http://localhost:3141/root/pypi/+simple/

Apparently using =0 is the right behavior. I don't know if we want to keep treating an empty string as 0.

@moshez
Copy link

moshez commented Jul 14, 2018

@justinmayer to your question -- just use =0 and it should be ok!

@moshez
Copy link

moshez commented Jul 14, 2018

@moshez
Copy link

moshez commented Jul 14, 2018

And just to finalize the saga -- when it is not empty, it uses strtobool.

"True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises ValueError if val is anything else."

On invalid values, it will traceback:

$ env PIP_REQUIRE_VIRTUALENV='blah' PYTHONPATH=src pytho
n -m pip install twisted                                                       
Traceback (most recent call last):                                             
  File "/home/moshez/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 193, in
 _run_module_as_main                                                           
...
    values = self.get_default_values()
  File "/home/moshez/src/pip/src/pip/_internal/baseparser.py", line 230, in get_default_values
    defaults = self._update_defaults(self.defaults.copy())  # ours             
  File "/home/moshez/src/pip/src/pip/_internal/baseparser.py", line 195, in _update_defaults
    val = strtobool(val)
  File "/home/moshez/.pyenv/versions/3.6.4/lib/python3.6/distutils/util.py", line 317, in strtobool
    raise ValueError("invalid truth value %r" % (val,))                        
ValueError: invalid truth value 'blah'

@pradyunsg
Copy link
Member

I don't know if we want to keep treating an empty string as 0.

No. I don't think setting PIP_REQUIRE_VIRTUALENV="" was ever a good way to do this. Boolean options are a little weird to deal with and how-to-use-them should probably get documented in the documentation.


pip clearly needs to print a better error message when the environment variable is not set correctly -- @moshez could you check if there's an existing issue for it otherwise file a new issue for this?

@pradyunsg pradyunsg added C: configuration Configuration management and loading type: support User Support labels Jul 16, 2018
@moshez
Copy link

moshez commented Jul 18, 2018

Hi @pradyunsg, I opened #5616 -- I did some searching and did not see an existing issue. Will you manage the state of this ticket and #5616 ? I don't know if this one is supposed to be closed or something.

@pradyunsg
Copy link
Member

I opened #5616

Thanks! ^>^

Will you manage the state of this ticket and #5616?

Yeah; I'll keep this open to track a documentation change -- we should document how boolean options behave when used via environment variables. The other issue would serve to track changing to print a better error message when we get "blah" values. :P

@pradyunsg pradyunsg added type: docs Documentation related good first issue A good item for first time contributors to work on and removed type: support User Support labels Jul 18, 2018
@pradyunsg
Copy link
Member

This issue is a good starting point for anyone who wants to help out with pip's development -- it's simple and the process of fixing this should be a good introduction to pip's development workflow. See the discussion above to understand what the desired fix is.

Feel free to mention me (write @pradyunsg in the comment) if you need help with something. :)

@SummerGram
Copy link

SummerGram commented Aug 27, 2018

@pradyunsg
Is it closed?

@pradyunsg
Copy link
Member

This ticket is open @SummerGram. Would you like to work on this?

@SummerGram
Copy link

@pradyunsg but I can't find PIP_REQUIRE_VIRTUALENV this variable

@lakshmanaram
Copy link
Contributor

Is this issue still relevant to the current codebase?

@pradyunsg
Copy link
Member

@lakshmanaram Yes.

Apologies for the delay in responding. I've been swamped by GitHub notifications since I was AWOL for a couple of weeks since the sprints.

pip's configuration code automatically translates option for CLI into environment variables when checking. I'm a little short on time to find the exact links to lines of currently -- the relevant code is in pip._internal.configuration, pip._internal.cli.cmdoptions and users of options.require_virtualenv.

@Divya063
Copy link

@pradyunsg I would like to work on this issue.

@pradyunsg
Copy link
Member

Looks like this got fixed at some point. From https://pip.pypa.io/en/stable/user_guide/#environment-variables:

Environment variables set to be empty string will not be treated as false. Please use no, false or 0 instead.


@Divya063 Hi! Would you be willing to try solving a different issue? Perhaps #7804?

@lock lock bot added the auto-locked Outdated issues that have been locked by automation label Apr 2, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation C: configuration Configuration management and loading good first issue A good item for first time contributors to work on type: docs Documentation related
Projects
None yet
Development

No branches or pull requests

6 participants