You can do this if you use setuptools entry_points
. Here's an example:
foo.py
def main():
print "Hello world"
setup.py
from setuptools import setup
setup(
name="foo",
version = "0.1",
py_modules=['foo'],
entry_points = {
'console_scripts': ['foo = foo:main']
}
)
Example usage, once the module has been installed using a tool like pip
:
$ foo
Hello world
$ python -c 'import foo; foo.main()'
Hello world