您如何向轮子添加其他文件? [英] How do you add additional files to a wheel?

查看:21
本文介绍了您如何向轮子添加其他文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何控制轮子中包含哪些文件?看来 MANIFEST.in 没有被 python setup.py bdist_wheel 使用.

How do control what files are included in a wheel? It appears MANIFEST.in isn't used by python setup.py bdist_wheel.

更新:

我错了从源 tarball 安装与从轮子安装之间的区别.源代码分发包括MANIFEST.in 中指定的文件,但安装的包只有python 文件.需要执行一些步骤来确定应该安装的其他文件,无论安装是通过源代码分发、egg 还是轮子安装.即,额外的包文件需要 package_data,而 data_files 用于包外的文件,如命令行脚本或系统配置文件.

I was wrong about the difference between installing from a source tarball vs a wheel. The source distribution includes files specified in MANIFEST.in, but the installed package only has python files. Steps are needed to identify additional files that should be installed, whether the install is via source distribution, egg, or wheel. Namely, package_data is needed for additional package files, and data_files for files outside your package like command line scripts or system config files.

我有一个项目,我一直在使用python setup.py sdist 构建我的包,MANIFEST.in 控制包含和排除的文件,以及 pyromacheck-manifest 以确认我的设置.

I have a project where I've been using python setup.py sdist to build my package, MANIFEST.in to control the files included and excluded, and pyroma and check-manifest to confirm my settings.

我最近将其转换为双 Python 2/3 代码,并添加了一个 setup.cfg 与

I recently converted it to dual Python 2 / 3 code, and added a setup.cfg with

[bdist_wheel]
universal = 1

我可以用 python setup.py bdist_wheel 构建一个轮子,它看起来是一个需要的通用轮子.但是,它不包括 MANIFEST.in 中指定的所有文件.

I can build a wheel with python setup.py bdist_wheel, and it appears to be a universal wheel as desired. However, it doesn't include all of the files specified in MANIFEST.in.

我挖得更深,现在对包装和轮子有了更多的了解.这是我学到的:

I dug deeper, and now know more about packaging and wheel. Here's what I learned:

我将两个包文件上传到 PyPi 上的 multigtfs 项目:

I upload two package files to the multigtfs project on PyPi:

  • multigtfs-0.4.2.tar.gz - 源 tar 包,包含 MANIFEST.in 中的所有文件.
  • multigtfs-0.4.2-py2.py3-none-any.whl - 有问题的二进制分布.
  • multigtfs-0.4.2.tar.gz - the source tar ball, which includes all the files in MANIFEST.in.
  • multigtfs-0.4.2-py2.py3-none-any.whl - The binary distribution in question.

我创建了两个新的虚拟环境,都使用 Python 2.7.5,并安装了每个包(pip install multigtfs-0.4.2.tar.gz).这两个环境几乎相同.它们有不同的 .pyc 文件,它们是编译"的 Python 文件.有记录磁盘上不同路径的日志文件.从源码tar包安装包含文件夹multigtfs-0.4.2-py27.egg-info,详细安装,wheel install有multigtfs-0.4.2.dist-info 文件夹,其中包含该过程的详细信息.但是从使用multigtfs项目的代码来看,两种安装方式没有区别.

I created two new virtual environments, both with Python 2.7.5, and installed each package (pip install multigtfs-0.4.2.tar.gz). The two environments are almost identical. They have different .pyc files, which are the "compiled" Python files. There are log files which record the different paths on disk. The install from the source tar ball includes a folder multigtfs-0.4.2-py27.egg-info, detailing the installation, and the wheel install has a multigtfs-0.4.2.dist-info folder, with the details of that process. However, from the point of view of code using the multigtfs project, there is no difference between the two installation methods.

明确地说,我的测试中也没有使用 .zip 文件,因此测试套件将失败:

Explicitly, neither has the .zip files used by my test, so the test suite will fail:

$ django-admin startproject demo
$ cd demo
$ pip install psycopg2  # DB driver for PostGIS project
$ createdb demo         # Create PostgreSQL database
$ psql -d demo -c "CREATE EXTENSION postgis" # Make it a PostGIS database 
$ vi demo/settings.py   # Add multigtfs to INSTALLED_APPS,
                        # Update DATABASE to set ENGINE to django.contrib.gis.db.backends.postgis
                        # Update DATABASE to set NAME to test
$ ./manage.py test multigtfs.tests  # Run the tests
...
IOError: [Errno 2] No such file or directory: u'/Users/john/.virtualenvs/test/lib/python2.7/site-packages/multigtfs/tests/fixtures/test3.zip'

指定附加文件

根据答案中的建议,我在 setup.py 中添加了一些额外的指令:

Specifying additional files

Using the suggestions from the answers, I added some additional directives to setup.py:

from __future__ import unicode_literals
# setup.py now requires some funky binary strings
...
setup(
    name='multigtfs',
    packages=find_packages(),
    package_data={b'multigtfs': ['test/fixtures/*.zip']},
    include_package_data=True,
    ...
)

这会将 zip 文件(以及 README)安装到文件夹中,并且测试现在可以正确运行.感谢您的建议!

This installs the zip files (as well as the README) to the folder, and tests now run correctly. Thanks for the suggestions!

推荐答案

您是否尝试过在 setup.py 中使用 package_data?MANIFEST.in 似乎针对 python 版本 <= 2.6,我不确定更高版本是否会查看它.

Have you tried using package_data in your setup.py? MANIFEST.in seems targetted for python versions <= 2.6, I'm not sure if higher versions even look at it.

在探索https://github.com/pypa/sampleproject后,他们的MANIFEST.in 说:

After exploring https://github.com/pypa/sampleproject, their MANIFEST.in says:

# If using Python 2.6 or less, then have to include package data, even though
# it's already declared in setup.py
include sample/*.dat

这似乎暗示这种方法已经过时了.同时,在 setup.py 中,他们声明:

which seems to imply this method is outdated. Meanwhile, in setup.py they declare:

setup(
    name='sample',
    ...
    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
    include_package_data=True,
    package_data={
        'sample': ['package_data.dat'],
    },
    ...
)

(我不确定他们为什么在 MANIFEST.in 中选择通配符,在 setup.py 中选择文件名.它们指的是同一个文件)

(I'm not sure why they chose a wildcard in MANIFEST.in and a filename in setup.py. They refer to the same file)

除了更简单之外,似乎再次暗示 package_data 路由优于 MANIFEST.in 方法.好吧,除非你必须支持 2.6,也就是说,在这种情况下,我为你祈祷.

Which, along with being simpler, again seems to imply that the package_data route is superior to the MANIFEST.in method. Well, unless you have to support 2.6 that is, in which case my prayers go out to you.

这篇关于您如何向轮子添加其他文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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