Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package a command line Python script

I've created a python script that's intended to be used from the command line. How do I go about packaging it? This is my first python package and I've read a bit about setuptools, but I'm still not sure the best way to do this.


Solution

I ended up using setup.py with the key configurations noted below:

setup( ....     entry_points=""" [console_scripts] mycommand = mypackage.mymodule:main """, .... ) 

Here's a good example in context.

like image 390
Zach Avatar asked Jul 12 '09 21:07

Zach


People also ask

How do I pass a command line argument to a Python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.

How do you package data in Python?

Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .


1 Answers

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.

like image 169
Alexis Métaireau Avatar answered Oct 04 '22 03:10

Alexis Métaireau