Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setuptools installing script to path

My Python module contains an external script that users can execute from the command line. I'd like users to be able to install the Python module and script all in one go. Using setuptools, I have tried adding:

scripts=['bin/mybin']

which almost does the trick. Instead of adding mybin to the user's PATH, it adds a small script that calls mybin:

#!/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# EASY-INSTALL-SCRIPT: 'myscript==2.0.4','mybin'
__requires__ = 'myscript==2.0.4'
__import__('pkg_resources').run_script('myscript==2.0.4', 'mybin')

This indirection is causing some problems, notably a small lag when the script is executed (compared to copying mybin to the PATH directly).

Is there any way to have setuptools copy the script directly to the PATH rather than have it call my script indirectly?

My first thought is to write a custom post-install function to copy the script to the user's path. But if there is a way to do this natively in setuptools, I would prefer that.

like image 239
Jeff Avatar asked May 04 '26 00:05

Jeff


1 Answers

One solution is to add this to your setup.py:

  data_files=[
    ('/usr/local/bin', ['bin/mybin'])
    ]

which copies bin/mybin to /usr/local/bin.

Another solution that does not require hard-coding a bin directory is to first detect the script directory that setuptools would copy your script to, then use data_files to copy it there directly. This StackOverflow answer explains how to detect the setuptools script path.

like image 141
Jeff Avatar answered May 06 '26 14:05

Jeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!