Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying.

I am also trying to generate my executable scripts upon python setuptools install using setuptools entry_points{'console_scripts': ... } options.

How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ?

Here are a few attempts I have made with no success:

setuptools( ... 

(1)

entry_points=        {'console_scripts': ['mypkg=mypkg.__main__'],}, 

(2)

entry_points=        {'console_scripts': ['mypkg=mypkg.main'],}, 

(3)

entry_points=        {'console_scripts': ['mypkg=python -m mypkg'],}, 

Primary resources I have been using:

  • http://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
  • https://www.python.org/dev/peps/pep-0338/
  • http://www.scotttorborg.com/python-packaging/command-line-scripts.html
  • http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
like image 601
StrugglingProgrammer Avatar asked Jan 05 '15 16:01

StrugglingProgrammer


People also ask

What is Setuptools used for in Python?

Setuptools is a package development process library designed to facilitate packaging Python projects by enhancing the Python standard library distutils (distribution utilities). It includes: Python package and module definitions. Distribution package metadata.

How do I set up Setuptools for Python on Windows?

Follow the below steps to install the Setuptools on Windows using the setup.py file: Step 1: Download the latest source package of Setuptools for Python3 from here. Step 2: Extract the downloaded package using the given command. Step 3: Go to the setuptools-60.2.

Does Python include Setuptools?

the setuptools is not part of the python vanilla codebase, hence not a vanilla modules. python.org installers or mac homebrew will install it for you, but if someone compile the python by himself or install it on some linux distribution he may not get it and will need to install it by himself.


1 Answers

How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ?

I think this is the wrong way to look at the problem. You don't want your script to call python -m mypackage, but you want the script to have the same entry point as python -m mypackage

Consider this simple example:

script_proj/ ├── script_proj │   ├── __init__.py │   └── __main__.py └── setup.py 

and the minimalistic setup.py:

from setuptools import setup  setup(     name="script_proj",     packages=["script_proj"],     entry_points = {         "console_scripts": [             "myscript = script_proj.__main__:main",         ]     } ) 

__main__.py is a dummy module and contains the main method.

def main():     print("Hello world!")  if __name__ == "__main__":     main() 

After installing, you have the executable myscript, which calls the main method in __main__.py. In this package design python -m script_proj also calls the same main method.

like image 63
cel Avatar answered Sep 21 '22 18:09

cel