I have a package awesomepkg with setup.py. I'd like to install a binary executable awesometool to the command line along with the package itself when users run pip install awesomepkg. I have compiled different OS versions for awesometool, which lives in a bin/ folder beside setup.py.
However, I can't find a good way to configure setup.py. I have attempted the following:
Use the
scripts=[]keyword insetup(). Unfortunately, the "executable" must be a python script.So I try to wrap the binary in a python script using
os.system('bin/awesometool')to delegate. It also fails because the wrapper script is copied somewhere else by pip, so it doesn't know where the relative pathbin/awesometoolis.Another potential solution is the
data_fileskeyword. However, for some reason the data files are not copied over tosite_packagesinstallation dir, even though runningpython setup.py bdist_wheelsays they have been copied.
Reference: https://docs.python.org/3/distutils/setupscript.html
I just ran into this issue myself. My solution was three-fold.
I added the program, e.g.
awesometool, to my package structure so I could add it via thepackage_datakeyword:package_data={'awesomepkg': ['awesometool']}. This causes it to actually be copied into the same folder as the main init.py during installation.I made a python script similar to your step 2. However, instead of the relative path, I first import
awesomepkgand useawesomepkg.__path__to get the absolute path to the installation folder for the package. This would look like:import awesomepkg import subprocess as sp import sys path = awesomepkg.__path__[0] command = path + "/awesometool" sp.call([command] + sys.argv)I also used subprocess instead of system, but the result should be the same.
I added this script to the
scriptskeyword ofsetup()
From within a package can use
import os
command = os.path.join(os.path.dirname(__file__), "awesometool")
来源:https://stackoverflow.com/questions/46142938/python-package-setup-script-install-binary-executable