【问题标题】:How to package perl files in python wheel如何在 python wheel 中打包 perl 文件
【发布时间】:2019-06-27 21:21:58
【问题描述】:

我们为一个包含 python 和 perl 脚本集合的包创建了一个 pip 轮文件。由于 python 打包只会将 python 文件添加到 wheel 文件中,因此打包 perl 文件的最佳方式是什么。

这是我的项目结构

.
|____myproject
| |____logging.ini
| |____utils.py
| |____myperlscript.pl
| |____config.py
| |____version.py
| |____scripta.py
| |____scriptb.py
| |____scriptc.py
| |______init__.py
|____test
| |____test_scripts.py
|______init__.py
|____MANIFEST.in
|____README.md
|____setup.py
|____.gitignore
|____Jenkinsfile

【问题讨论】:

标签: python perl setuptools distutils python-wheel


【解决方案1】:

如果您当前在项目的setup.py 中使用setuptools,并使用python setup.py bdist_wheel 作为生成.whl 文件的方法,则将以下行添加到MANIFEST.in 文件中,即已经存在于项目的根目录中。

recursive-include myproject *

当然,将myproject 替换为将包含目标.pl 脚本(或任何其他文件)的实际顶级目录。

作为一个演示,如果你的setup.py 大致是这样写的:

from setuptools import setup
from setuptools import find_packages

setup(
    name='myproject',
    version='0.0.0',
    description='demo package',
    long_description=open('README.md').read(),
    classifiers=[
        'Programming Language :: Python',
    ],
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
)

运行python setup.py bdist_wheel 将显示如下输出:

...
adding 'myproject/__init__.py'
adding 'myproject/config.py'
adding 'myproject/logging.ini'
adding 'myproject/myperlscript.pl'
adding 'myproject/scripta.py'
adding 'myproject/utils.py'
adding 'myproject/version.py'
adding 'test/__init__.py'
...

文件打包在.whl:

$ unzip -t dist/myproject-0.0.0-py3-none-any.whl 
Archive:  dist/myproject-0.0.0-py3-none-any.whl
    testing: myproject/__init__.py    OK
    testing: myproject/config.py      OK
    testing: myproject/logging.ini    OK
    testing: myproject/myperlscript.pl   OK
...

在新环境中安装生成的.whl 文件:

$ pip install -U myproject-0.0.0-py3-none-any.whl 
Processing myproject-0.0.0-py3-none-any.whl
Installing collected packages: myproject
Successfully installed myproject-0.0.0
$ ls env/lib/python3.6/site-packages/myproject/
config.py    logging.ini      __pycache__  utils.py
__init__.py  myperlscript.pl  scripta.py   version.py

另请注意,如果不需要 MANIFEST.in 方法,则在 setup 调用中包含 package_data={'': ['*']}, 参数也应该使其适用于最新版本的 setuptools

进一步的附录:setuptools 包实际上有一个 MANIFEST.in 包含此特定语法,但仅限于他们想要包含的文件的特定文件扩展名。尽管有一些指南/文档可能会提出其他建议,但这显然是一个受支持的选项。实际上,这是 Python 默认附带的功能provided by the core distutils module。相关问题:

【讨论】:

  • @phd 文档有误,它确实有效,因为我有以这种方式打包的项目。
  • @phd 如果您阅读我对此答案的更新,您可能会遇到this 链接(通过this answer)解释为什么您使用package_data 的建议实际上不起作用,并且data_files要多花点功夫才能用。
  • 两个链接都指向一个相当过时的答案(用他们自己的话来说是“该死的谎言”)。如今,主要的分发格式是轮子,package_data 完美运行。
  • 至于MANIFEST.in——你的更新答案包含include_package_data=True,——这使它工作。
猜你喜欢
  • 2016-11-26
  • 2014-07-17
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2020-11-04
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多