tags:

views:

45

answers:

2

Is there a way to enable a package to be executed as a script? For example:

[~]# easy_install /path/to/foo.egg
...
[~]# python -m foo --name World
Hello World

I've tried creating a __main__.py file inside my package but it's not being executed (I'm using Python 2.6). The following error is raised:

foo is a package and cannot be directly executed

The structure of my package is as follows:

foo/
  setup.py
  foo/
    __init__.py
    __main__.py

Running python -m foo.__main__ --name World works as expected, but I would prefer the former way of execution. Is this possible?

A: 

Create a file __init__.py. It can be executed the way you want without mentioning file.

You need to mention the file name if its not __init__.py , during execution.

anand
+1  A: 

as long as the package is on the python path, add at the end of the script.

if __name__ == "__main__":
     call_script()
$ python -m module_name  

will run the module e.g

python -m random
mossplix