[Distutils] Using an egg like a Java jar file

P.J. Eby pje at telecommunity.com
Tue Jul 28 06:19:23 CEST 2009


At 12:13 PM 7/27/2009 -0700, mhearne808 wrote:
>In Java, it is possible to create a .jar file with a main() method
>embedded somewhere in one of the .class files.  It is then possible to
>run the main() method from the command line something like this:
>
>java -jar foo.jar
>
>I believe this will work with only one main() defined - if there are
>multiple ones, then you have to specify the path (internal to the jar
>file).
>
>I gather from previous googling that there isn't a python command line
>option equivalent to -jar for eggs (although that might be kind of
>handy)

As of Python 2.6, you can run "python someziporeggfile" and have it 
work - you just have to include a __main__.py module.


>but that you can add an egg to your path without installing it by
>doing something like the following (in a bash shell):
>export PYTHONPATH=foo.egg
>
>This will then allow you to run a python script at the command line
>without having to actually install the egg to the normal "site-
>packages" directory.
>
>My questions:
>  - Is it possible to embed a script with a "main" entry point in an
>egg, and run that from the command line?

As of Python 2.4, you can use 'python -m modulename' to load the 
named module as the main module.

>  - If so, how do you pass command line arguments to that main?

python -m modulename args...

>  - Is this a massive abuse of the intended usage of setuptools?

Nope.  There is also a way to make an egg an executable in its own 
right; you can declare an entry point 'eggsecutable' in the 
'setuptools.installation' entry point group, and when you bdist_egg 
it, the egg will include a simple #!/bin/sh script header to put the 
egg on sys.path and run the entry point function.  The main downside 
to this is that the egg cannot be renamed or run via a symlink with 
another name; you have to run it as foobar-1.2.3.egg.



More information about the Distutils-SIG mailing list