1

When creating a Python package, the actual top-level command is frequently installed in a PATH folder, such as /usr/local/bin. For it to reference all the related package modules, they need to be explicitly qualified with the package name:

import package.module

and explicitly called

package.module.function(args)

In a big package, with extensive use of argparse, there might be hundreds of references to functions and variables in the package, all of which need to be qualified with the package name when referenced from /usr/local/bin/cmd.py. This is fine for Production use but a pain during development as the package needs to be reinstalled every time a change is tested. (i.e. It can only find modules in that one, explicit location.

And so, finally, to my question:- Can the command itself (/usr/local/bin/cmd.py) be a stub that just imports a module within the package, containing the argparser and its definitions?

Using this method, all the modules (except the stub) live within the package directory and intra-package references will find all the other modules without the need to explicitly state the package name on them.

Steve Crook
  • 1,013
  • 2
  • 13
  • 22
  • In addition to the answers below, make sure that you develop a good unittest suite. If you have really good unittest coverage, then you won't need to do full end-to-end testing quite as frequently (and you'll probably check more of the code is less time). – mgilson Mar 19 '14 at 13:30

2 Answers2

1

Yes, the setuptools.setup( parameter called entry_points takes a dictionary and keys called console_scripts does exactly this for you.

setuptools.setup(
    ...
    entry_points={'console_scripts': ['mycmd = mycmd:main']},
)

This will create a script named mycmd in the bin dir (system or virtualenv) which loads the mycmd module and executes its main function when called.

http://pythonhosted.org//setuptools/setuptools.html#automatic-script-creation

jrwren
  • 17,465
  • 8
  • 35
  • 56
0

Hi I don't think this is the definitive answer but it might be worth you having a look at:

How can I install Python modules programmatically / through a Python script?

Hopefully will provide some insight and a potential solution to your problem

Thanks, //P

Community
  • 1
  • 1
YFP
  • 331
  • 3
  • 8