Create a package containing global commands in Python

I made a package with global commands in Python, so make a note of it.

Global commands are commands in a location such as / usr / local / bin / that is normally in the PATH, and the purpose is to be able to install arbitrary commands in / usr / local / bin / with pip. will do.

environment

Constitution

myapp/
  ├ setup.py
  └ myapp/
    ├ __init__.py
    └ main.py

Contents of setup.py

import setuptools 

if __name__ == "__main__":
    setuptools.setup(
        name='myapp',
        version='0.0.1',
        packages=setuptools.find_packages(),
        entry_points={
            'console_scripts':[
                'myapp = myapp.main:main',
            ],
        },
    )

ʻEntry_pointsspecifies the file and function to execute. 'myapp = myapp.main: main'` means that when you run the executable file myapp, it calls the main function in myapp / main.py.

Use setuptools.find_packages () to put the directory containing __init__.py into the package.

Contents of main.py that stores the function to be actually executed

import sys

def main():
    print(sys.argv)

Creating a package

$ python setup.py sdist

With this, the package body dist / myapp-0.0.1.tar.gz and A metadata directory myapp.egg-info / is created.

Install package

$ sudo pip install dist/myapp-0.0.1.tar.gz

In my Mac OS X environment, the following files were installed

  /Library/Python/2.7/site-packages/myapp-0.0.1-py2.7.egg-info
  /Library/Python/2.7/site-packages/myapp/__init__.py
  /Library/Python/2.7/site-packages/myapp/__init__.pyc
  /Library/Python/2.7/site-packages/myapp/main.py
  /Library/Python/2.7/site-packages/myapp/main.pyc
  /usr/local/bin/myapp

You can see that the executable file is registered in / usr / local / bin / myapp

Operation check

Now you can type the myapp command from anywhere

$ myapp hoge bar
['/usr/local/bin/myapp', 'hoge', 'bar']

Recommended Posts

Create a package containing global commands in Python
Create a function in Python
Create a dictionary in Python
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create your own Linux commands in Python
Create a simple GUI app in Python
Create a deb file from a python package
[GPS] Create a kml file in Python
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
Read a file containing garbled lines in Python
Create a Python module
Create SpatiaLite in Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a Python environment
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a data collection bot in Python using Selenium
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create a fake Minecraft server in Python with Quarry
Take a screenshot in Python
Create a Wox plugin (Python)
Run shell commands in python
Create gif video in Python
Make a bookmarklet in Python
Create a python numpy array
Draw a heart in Python
Create a directory with python
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Create a child account for connect with Stripe in Python
Let's create a script that registers with Ideone.com in Python.
Create code that outputs "A and pretending B" in python
Create a tool to check scraping rules (robots.txt) in Python
A record of patching a python package
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Check for external commands in python
Hit a command in Python (Windows)
8 Frequently Used Commands in Python Django
Create a CSV reader in Flask
Create a python GUI using tkinter
Create a Python environment on Mac (2017/4)
Draw a scatterplot matrix in python
Bayesian optimization package GPyOpt in Python
How to create a Conda package
Create a virtual environment with Python!