Rather than using setuptools non standard way of proceeding, it is possible to directly rely on distutils setup's function, using the scripts argument, as stated here: http://docs.python.org/distutils/setupscript.html#installing-scripts
from distutils import setup
setup(
...,
scripts=['path/to/your/script',],
...
)
It allows you to stay compatible a) with all python versions and b) not having to rely on a setuptools as an external dependency.
Answer from Alexis Métaireau on Stack OverflowRather than using setuptools non standard way of proceeding, it is possible to directly rely on distutils setup's function, using the scripts argument, as stated here: http://docs.python.org/distutils/setupscript.html#installing-scripts
from distutils import setup
setup(
...,
scripts=['path/to/your/script',],
...
)
It allows you to stay compatible a) with all python versions and b) not having to rely on a setuptools as an external dependency.
@Zach, given your clarification in your comment to @soulmerge's answer, it looks like what you need is to write a setup.py as per the instructions regarding the distutils -- here in particular is how you register on pypi, and here on how to upload to pypi once you are registrered -- and possibly (if you need some extra functionality wrt what the distutils supply on their own) add setuptools, of which easy_install is part, via the instructions here.
Yes, this is possible and quite common for packages that serve as both command-line tools and imported libraries.
In setup.py, add a module's function as an entry point:
setuptools.setup(
...
entry_points={'console_scripts': [
'foo = my_package.some_module:main_func',
]},
...
)
To create a script named foo that calls the my_func function inside of my_package.some_module. Read more at https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation.
It is also a convention it to add:
#!/usr/bin/env python
...
if __name__ == '__main__':
my_func()
to the modules that can be called as "scripts" where my_func is the function you'd like to call externally.
Here is an example of setup.py:
https://github.com/jacebrowning/gdm/blob/fa998167f5f6de64bc8bdfd8b9433870d79ef814/setup.py#L28-L31
and a callable module:
https://github.com/jacebrowning/gdm/blob/fa998167f5f6de64bc8bdfd8b9433870d79ef814/gdm/cli.py#L161-L162
Two years later, I feel that I can answer my own question.
It is preferable to use the Setuptools entry_points method. This is described well in the Click documentation, and the main points are:
- this works better on Windows
- the Pythonpath is managed by Setuptools, so (for example) if your script is installed inside a virtualenv, you don't need to activate the virtualenv for it to work correctly
You need relative imports. Try
from .. import mypackage
or
from ..mypackage import server
The documentation is here
http://docs.python.org/tutorial/modules.html#intra-package-references
These work on Python 2.5 or newer.
To do it only in the development version, try:
try:
from my_package import server
except ImportError:
from ..my_package import server
You can use virtualenv for testing Python code while in development as if it was released