[Distutils] Using an egg like a Java jar file

mhearne808 mhearne808 at gmail.com
Tue Jul 28 23:20:26 CEST 2009


All:  Thanks for your suggestions.  Unfortunately, I can't use the
Python 2.6 functionality because I'll be running this on systems where
2.6 is not installed (Red Hat Enterprise).

I'm trying the 'eggsecutable' suggestion, but I can't figure out how
to pass command line arguments to my main() entry point.  I created
two python files, testme.py and setup.py, included below.  I've
determined through experimentation that my entry point is directly
into my main() function.  The code at the end ("if __name__" etc.) is
not being called at all.  When I build this with "python setup.py
bdist_egg", and then run it "sh testme-1.0.0-py2.5.egg 1 2", I get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: main() takes exactly 1 argument (0 given)

How can I pass arguments from the command line in to my main()
function?

Thanks,

Mike


testme.py:
#!/usr/bin/env python
import sys

def main(input):
    print 'Command line arguments are ' % input

if __name__ == '__main__':
    main(sys.argv)

setup.py:
from setuptools import setup, find_packages

setup(name='testme',
      version='1.0.0',
      py_modules=['testme'],
      description = 'foo',
      author = 'Somebody',
      author_email = 'somebody at foo.com',
      url = 'www.foo.com',
      entry_points = {
        'setuptools.installation': ['eggsecutable = testme:main',]}
      )

On Jul 27, 10:19 pm, "P.J. Eby" <p... at telecommunity.com> wrote:
> 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.
>
> _______________________________________________
> Distutils-SIG maillist  -  Distutils-... at python.orghttp://mail.python.org/mailman/listinfo/distutils-sig


More information about the Distutils-SIG mailing list