36

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.

Zach
  • 24,496
  • 9
  • 43
  • 50

5 Answers5

5

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.

Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34
4

@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.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    This answer, fortunately, is a little out of date. A good source of information now is https://packaging.python.org/en/latest/, as well as the article I have pointed out in my answer. – Dr. Jan-Philip Gehrcke Jan 22 '15 at 21:36
3

Last month, I have written an article answering exactly your question. You can find it here: http://gehrcke.de/2014/02/distributing-a-python-command-line-application/

There, I am using only currently recommended methods (twine, pure setuptools instead of distutils, the console_scripts key in the entry_points dictionary, ...), which work for Python 2 and 3.

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
2

What do you mean by packaging? If it is a single script to be run on a box that already has python installed, you just need to put a shebang into the first line of the file and that's it.

If you want it to be executed under Windows or on a box without python, though, you will need something external, like pyinstaller.

If your question is about where to put configuration/data files, you'll need to work platform-dependently (like writing into the registry or the home folder), as far as I know.

soulmerge
  • 73,842
  • 19
  • 118
  • 155
  • It consists of two python files. I want to package it to be distributed on PiPy, for example, and installed with easy_install. – Zach Jul 12 '09 at 22:35
0

For those who are beginners in Python Packaging, I suggest going through this Python Packaging Tutorial.

Note about the tutorial:

At this time, this documentation focuses on Python 2.x only, and may not be as applicable to packages targeted to Python 3.x

Ed Patrick Tan
  • 727
  • 1
  • 9
  • 15