Python软件包安装脚本安装二进制可执行文件 [英] Python package setup script install binary executable

查看:131
本文介绍了Python软件包安装脚本安装二进制可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含setup.py的软件包awesomepkg.当用户运行pip install awesomepkg时,我想将二进制可执行文件awesometool与软件包本身一起安装到命令行.我为awesometool编译了不同的OS版本,该版本位于setup.py旁边的bin/文件夹中.

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.

但是,我找不到配置setup.py的好方法.我尝试了以下操作:

However, I can't find a good way to configure setup.py. I have attempted the following:

  1. setup()中使用scripts=[]关键字.不幸的是,可执行文件"必须是python脚本.

  1. Use the scripts=[] keyword in setup(). Unfortunately, the "executable" must be a python script.

所以我尝试使用os.system('bin/awesometool')委托将二进制文件包装在python脚本中.它也失败了,因为包装脚本是通过pip复制到其他位置的,因此它不知道相对路径bin/awesometool在哪里.

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 path bin/awesometool is.

另一个可能的解决方案是data_files关键字.但是,由于某些原因,即使运行python setup.py bdist_wheel表示数据文件已被复制,数据文件也不会复制到site_packages安装目录中.

Another potential solution is the data_files keyword. However, for some reason the data files are not copied over to site_packages installation dir, even though running python setup.py bdist_wheel says they have been copied.

参考: https://docs.python.org/3/distutils/setupscript. html

推荐答案

我本人只是遇到了这个问题.我的解决办法是三倍.

I just ran into this issue myself. My solution was three-fold.

  1. 我添加了程序,例如awesometool,到我的包结构中,所以我可以通过package_data关键字将其添加: package_data={'awesomepkg': ['awesometool']}. 这会导致它在安装过程中实际上被复制到与主 init .py相同的文件夹中.

  1. I added the program, e.g. awesometool, to my package structure so I could add it via the package_data keyword: package_data={'awesomepkg': ['awesometool']}. This causes it to actually be copied into the same folder as the main init.py during installation.

我制作了一个类似于您的步骤2的python脚本.但是,我首先导入了awesomepkg并使用awesomepkg.__path__来获取该软件包的安装文件夹的绝对路径,而不是相对路径.看起来像这样:

I made a python script similar to your step 2. However, instead of the relative path, I first import awesomepkg and use awesomepkg.__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.

我将此脚本添加到setup()

这篇关于Python软件包安装脚本安装二进制可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆