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.
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)
$ 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.
$ 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
Now you can type the myapp command from anywhere
$ myapp hoge bar
['/usr/local/bin/myapp', 'hoge', 'bar']
Recommended Posts