Execute an installed Python package as a script?

This is a regression in Python 2.6. See issue2571:

The ability to execute packages was never intended, since doing so
breaks imports in a variety of subtle ways. It was actually a bug in
2.5 that it was permitted at all, so 2.6 not only disabled it again, but also added a test to make sure it stays disabled (2.4 correctly
rejected it with an ImportError, just as 2.6 does).

You have a few options, you can either always run it specifying main:

$ python -m module.__main__

Or you can write a shell script wrapper that detects the python version and then executes it in the different style.

Or you can execute code on the command line that will import and then run the module, and then perhaps place that in a shell script:

$ python -c "import module; module.main()"

In my own command-line projects I have both the shell script that catches errors (python not being installed, etc.) but the shell script will also execute the import code and detect if the necessary modules have been installed and prompt an error (with a helpful link or install text).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)