[Python-Dev] Add a -z interpreter flag to execute a zip file

Nick Coghlan ncoghlan at gmail.com
Thu Jul 12 17:46:13 CEST 2007

Phillip J. Eby wrote:
> At 10:09 AM 7/12/2007 +0200, Martin v. Löwis wrote:
>> "" should not be removed from sys.path. It is *not* meant to be
>> the current directory, but the directory where the main script
>> lives.
> Right; it should be replaced with the zipfile path instead.
> I would personally rather see this option defined as simply placing a 
> directory at the front of sys.path, and perhaps defining a default -m 
> value of __main__, unless overrridden.  Being able to use the option 
> more than once would be nice, too.  On Windows, you can't set an 
> environment variable on the same line as a command, so this would 
> give you a one-liner way of setting sys.path and running an application.
> I do not see a reason to make this option zipfile-specific in any 
> way, though; it's just as useful (and sometimes more so) to be able 
> to distribute an application as a directory, since that lets you use 
> .pyd, .so, .dll etc. without needing the egg cache system for using those.

I've thought about this a little further since my last comment on SF, 
and I think it may be a better idea to handle this as a runpy module 
parameter rather than as a parameter for the main interpreter.

For those that aren't aware, the two commands:

   python -m <module>
   python -m runpy <module>

actually have the same effect - both run the specified module. The 
second version is just a little indirect, as it first executes the runpy 
module, which then makes its a second call to run_module(). It was done 
this way so that -m style functionality was readily available for Python 
versions prior to 2.4.

The current version of runpy doesn't accept any options, but it would be 
pretty easy to make the following changes:

1. Accept a -p option that prepends path entries. These path entries 
would be combined into a single list from left to right on the command 
line, then the whole list prepended to sys.path. If at least one -p 
option is given, the default '' entry would be removed from sys.path 
(the current directory could be added back in explicitly via -p '.').

2. Attempt to run the module __main__ if no module is otherwise specified

Startup would be fractionally slower than it would be with the C-level 
option, but the code would be much simpler, and the new feature would be 
readily available on any Python implementation which can execute the 
runpy module.

The relevant shebang line to be prepended to a zip file would then look 
something like:

#!/usr/bin/env python -m runpy -p


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list