setup.py and adding file to /bin/
Question:
I can’t figure out how to make setup.py
add a scrip to the the user’s /bin
or /usr/bin
or whatever.
E.g., I’d like to add a myscript.py
to /usr/bin
so that the user can call myscript.py
from any directory.
Answers:
The Python documentation explains it under the installing scripts section.
Scripts are files containing Python source code, intended to be started from the command line.
setup(...,
scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
)
As mentioned here, beside scripts, there is an entry_points
mechanism, which is more cross-platform.
With entry_points
you connect a command line tool name with a function of your choice, whereas scripts
could point to any file (e.g. a shell script).
If you’re willing to build and install the entire python package, this is how I would go about it:
- Edit the setup() function in setup.py to contain a parameter named scripts and set its argument as the location of the file(s) you wish to run from anywhere. e.g.
setup(name='myproject',author='',author_email='',scripts=['bin/myscript.py'])
- Within the directory that contains setup.py, create a bin directory by typing
mkdir bin
- Add myscript.py to this newly-created
bin
directory (and make sure it’s executable!)
cd
into the directory that contains setup.py again, and install the entire python package by typing python setup.py install
- Once the package is installed, you should be able to run myscript.py from anywhere on the system!
Consider using console_scripts
:
from setuptools import setup
setup(name='some-name',
...
entry_points = {
'console_scripts': [
'command-name = package.module:main_func_name',
],
},
)
Where main_func_name
is a main function in your main module.
command-name is a name under which it will be saved in /usr/local/bin/ (usually)
There are two ways in order to get a working command line tool from setuptools and PyPI infrastructure:
- The “scripts” Keyword Argument
This allows the command-line execution of everything you want, it can be a Python script, a shell script or something completely different.
- The “console_scripts” Entry Point
This allows Python functions (not scripts!) to be directly registered as command-line accessible tools.
I can’t figure out how to make setup.py
add a scrip to the the user’s /bin
or /usr/bin
or whatever.
E.g., I’d like to add a myscript.py
to /usr/bin
so that the user can call myscript.py
from any directory.
The Python documentation explains it under the installing scripts section.
Scripts are files containing Python source code, intended to be started from the command line.
setup(...,
scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
)
As mentioned here, beside scripts, there is an entry_points
mechanism, which is more cross-platform.
With entry_points
you connect a command line tool name with a function of your choice, whereas scripts
could point to any file (e.g. a shell script).
If you’re willing to build and install the entire python package, this is how I would go about it:
- Edit the setup() function in setup.py to contain a parameter named scripts and set its argument as the location of the file(s) you wish to run from anywhere. e.g.
setup(name='myproject',author='',author_email='',scripts=['bin/myscript.py'])
- Within the directory that contains setup.py, create a bin directory by typing
mkdir bin
- Add myscript.py to this newly-created
bin
directory (and make sure it’s executable!) cd
into the directory that contains setup.py again, and install the entire python package by typingpython setup.py install
- Once the package is installed, you should be able to run myscript.py from anywhere on the system!
Consider using console_scripts
:
from setuptools import setup
setup(name='some-name',
...
entry_points = {
'console_scripts': [
'command-name = package.module:main_func_name',
],
},
)
Where main_func_name
is a main function in your main module.
command-name is a name under which it will be saved in /usr/local/bin/ (usually)
There are two ways in order to get a working command line tool from setuptools and PyPI infrastructure:
- The “scripts” Keyword Argument
This allows the command-line execution of everything you want, it can be a Python script, a shell script or something completely different. - The “console_scripts” Entry Point
This allows Python functions (not scripts!) to be directly registered as command-line accessible tools.