简体   繁体   中英

How to run a python script installed from distutils

I have a medium sized python command line program that runns well from my source code, and I've created a source distribution file and installed it into the virtual environment using "python setup.py install"

Since this is a pure Python program, and provided that the end users have installed Python, and the required packages, my idea is that i can distribute it through PyPi for all available platforms as a source distribution.

Upon install, I get an 'appname' directory within the virtualenv site-packages directory, and it also runs correctly when I write "python 'pathtovirtualenv'/Lib/sitepackages/'myappname'

But is this the way the end user is supposed to run distutils-distributed programs from the command line.

I fnd a lot of information on how to distribute a program using distutils, but not on how the end user is supposed to launch it after installing it.

Since you already created a setup.py , I would recommend looking at the entry_points :

  entry_points={
      'console_scripts': [
          'scriptname=yourpackage.module:function',
      ],
  },

Here, you have a package named yourpackage , and a module named module in it, and you refer to the function function . This function will wrapped by the script called scriptname , which will be installed in the users bin folder, which is normally in the $PATH , so the user can simply type scriptname after he installed your package via pip install .

To sum up: a user will install the package via pip install yourpackage and finally be able to call the function in module via script name .

Here are some docs on this topic:

https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation http://www.scotttorborg.com/python-packaging/command-line-scripts.html

Well, I eventually figured it out.

Initially, I wanted to just use distutils, I like it when the end user can install it with minimum of extra dependencies. But I have now discovered that setuptools is the better option in my case.

My directory structure looks like this (Subversion):

trunk
  |-- appname
  |     |-- __init__.py   # an empty file
  |     |-- __main__.py   # calls appname.main()
  |     |-- appname.py    # contains a main() and imports moduleN
  |     |-- module1.py
  |     |-- module2.py
  |     |-- ...
  |-- docs
  |     |-- README
  |     |-- LICENSE
  |     |-- ...
  |-- setup.py

And my setyp.py basically looks like this:

# This setup file is to be used with setuptools source distribution
# Run "python setup sdist to deploy

from setuptools import setup, find_packages

setup( name = "appname",
       ...
       include_package_data = True,
       packages = find_packages(),
       zip_safe = True,
       entry_points = {
           'console_scripts' : 'appname=appname.appname:main'
       }
   )

The next step now is to figure out how to install the contents of the docs directory on the users computer.

But right now, I'm thinking about adding --readme, --license, --changes, --sample (and so forth) options to the main script, to display them at run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM