-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
Environment variable to set alternate location for pycache tree #77680
Comments
We would like to set an environment variable that would cause Python to read and write
Proposal: a |
Per vstinner Python prefers to not have underscores in environment variable names, for historical reasons. So I'm using Other open questions:
Will push an initial version for review that has no CLI flag nor |
FWIW, I've had issues with environment variables in that they affect every version of Python running on a system and seem to defy isolation. So, if one application needs the environment variable set, it will affect every application, even if it wants to keeps its contents private and not leak outside of a virtual environment. Can your needs be met with just CLI flag rather than a system-wide environment variable? |
Environment variable seems to make a bit more sense for this, since it's not per-invocation; there's no point writing bytecode cache to a particular location unless the next invocation reads the cache from there. Our use case includes a webserver process that embeds Python; I'm not sure if we could pass a CLI arg to it or not. Python has lots of precedent for similar environment variables (e.g. I asked Brett Cannon in the sprints if I should add a CLI flag in addition to the env var; he suggested it wasn't worth it. I'm not opposed to adding the CLI flag, but I think removing the env var option would be a mistake. |
Environment variables aren't system-wide, they are per-process (though they can be inherited by child processes). |
Yes, that is how they work. It is not how they are used. Environment variables are commonly set in shell start-up scripts such as .bashrc and the results then affect every python application that gets run in any shell session. |
On May 15, 2018, at 22:58, Carl Meyer <report@bugs.python.org> wrote:
I think you pretty much have to have an environment variable, as there are just too many places where you’re going to invoke Python without the ability to set the command line. We have precedence for having both a switch and environment variable, and I think that makes sense here. |
Regarding environment variables, note that they get used in two *very* different ways:
Anywhere that I used this setting, I'd want it to be passed along to child processes, so an environment variable would be a lot more useful than a command line option. If we did add an option, then a named -X option would probably make the most sense. Regarding the state caching: having this be read once at startup would help avoid a lot of potential for weird state inconsistencies where some modules were loaded from one cache directory, while later modules were loaded from a different one. |
On May 17, 2018, at 08:14, Nick Coghlan <report@bugs.python.org> wrote:
+1 |
Can we have a named -X option that also takes a parameter? I don't see any existing examples of that. This option needs to take the path where bytecode should be written. Are there strong use-cases for having a CLI arg for this? I don't mind doing the implementation work if there are, but right now I'm struggling to think of any case where it would be better to run |
Honestly, I don't think there's a strong argument for a CLI option. I'm perfectly happy with just an environment variable. |
I believe the main argument for -X options is the fact that cmd on Windows doesn't offer a nice way of setting environment variables as part of the command invocation (hence "-X utf8", for example). As far as setting values for X options goes, $ python3 -X arg=value -c "import sys; print(sys._xoptions)"
{'arg': 'value'} If no value is given for the arg, then it's just set to the boolean True. The _xoptions entry shouldn't be the public API though - it's just a way of shuttling settings from the command line through to CPython-specific initialisation code. |
Cool, thanks for the pointer on -X. PR is updated with |
Merged as PYTHONPYCACHEPREFIX=path, -X pycache_prefix=path and sys.pycache_prefix :) I'll also update PEP-304 with a note saying a variant of the idea was eventually accepted for Python 3.8. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: