[Python-ideas] Python Environment Initialization Scripts

James William Pye x at jwp.io
Sun Jul 29 11:44:55 CEST 2012


On Jul 28, 2012, at 11:57 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> The short answer is that, no, a coherent proposal for application
> specific preinitialisation hasn't been put forward as a PEP.

=)

> The slightly longer answer is that there are related problems in terms of
> initialising debuggers, warnings, logging, faulthandler, coverage,
> profiling, etc, during development where it would be incredibly
> convenient to be able to execute a short snippet of Python code prior
> to execution.

Yeah, that's been painful.

> See http://bugs.python.org/issue14803 (I'm nervous about the security
> implications of a PYTHONRUNFIRST environment variable that is enabled
> by default,

Rightly so.

> but the idea of a -C option doesn't pose any such
> concerns)

Cool. I implemented something like that for "-m postgresql.bin.pg_python"[1]
Never ended up using it much.

But I could see myself using it with the Python executable: alias py='python3 -C …'
Tucking that into a sh source file that sets up my dev context.

> In general though, messing with process global state (including the
> import system) implicitly is just *not cool*. It leads to applications
> that have a dependency on a particular tool being present, but don't
> actually make sure that it is available. Thus you end up with manual
> instructions that say "before running this script, make sure
> infrastructure X is in place to automagically take care of things I
> should be doing myself".


Even in the case of some designated mechanism where there is some
attempt to expose/record changes to process global state so that it can
be easily referenced by a dependency, is there a win over making an explicit
attempt to validate that the state change occurred by the dependency?

I think in this particular case involving meta_path, there would be a
small win with regards to being able to perform the check at package
installation time as opposed to every time the package is imported,
given a "designated mechanism" of sorts.
Essentially, the package being installed requests assurance that a state
change *will* occur upon Python startup.

[1] https://github.com/jwp/py-postgresql/blob/v1.1/postgresql/python/command.py


More information about the Python-ideas mailing list