Distribute executable module

2019-08-28 05:03发布

问题:

I developed a program consisting of a package which is executable (i.e. two files x/__init__.py and x/__main__.py), so I can execute it using python -m x, if the package resides in the python path.

I never tried to distribute Python packages/modules and I don't have any experience in that field (yet)... I already noticed that there are many different systems and now I have three questions:

  1. Which system is appropiate for a Python 3(.2) program? distribute? distutils? setuptools?

  2. How can I add kind of a "wrapper script" to the distribution that executes the module directly without the hassle with python -m … so that the user can just type x (of course the actual name is a bit more unique :) on the shell.

  3. How can I do 2. in a platfom-independent way?

Thanks! :)

回答1:

  1. Use either distribute or setuptools, the former is a fork of the latter, with some improvements and better documentation. Either one is a big step up from distutils, which is part of the python standard library.

  2. You want a console script, for which you define an entry point:

    entry_points = {
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ],
    

    where foo and bar would be scripts that you can call on the command line. The indicated function will be called with sys.argv[1:] as the first and only argument.

  3. Let the installation tools take care of that; it works fine on Windows. :-)