46

我无法将项目正确安装package_fiddler到我的虚拟环境中。

我发现MANIFEST.in 它负责将 Package_fiddler-0.0.0.tar.gz执行时生成的非 .py 文件放入其中python setup.py sdist

然后我做了:

(virt_envir)$ pip install dist/Package_fiddler-0.0.0.tar.gz

但这并没有将数据文件或软件包安装到/home/username/.virtualenvs/virt_envir/local/lib/python2.7/site-packages.

我尝试了许多设置参数的配置,package_data但似乎每次都使用了错误的配置。include_package_datadata_files

哪种配置package_data和/或include_package_data和/或data_files将正确安装package_fiddler到我的虚拟环境中?

项目树

.
├── MANIFEST.in
├── package_fiddler
│   ├── data
│   │   ├── example.html
│   │   └── stylesheets
│   │       └── example.css
│   └── __init__.py
├── README.rst
└── setup.py

安装程序.py

from setuptools import setup


setup(
    name='Package_fiddler',
    entry_points={
    'console_scripts': ['package_fiddler = package_fiddler:main', ],},
    long_description=open('README.rst').read(),
    packages=['package_fiddler',])

清单文件

include README.rst
recursive-include package_fiddler/data *

我尝试了哪些 setup.py 配置(上面有代码库)?

配置1

添加:

package_data={"": ['package_fiddler/data/*',]}

配置2

添加:

package_data={"": ['*.html', '*.css', '*.rst']}

配置3

添加:

include_package_data=True

配置4

添加:

package_data={"": ['package_fiddler/data',]}

删除:

packages=['package_fiddler',]

配置5(克里斯的建议)

添加:

package_data={"data": ['package_fiddler/data',]}

删除:

packages=['package_fiddler',]

配置6

添加:

package_data={"": ['package_fiddler/data/*',]}

删除:

packages=['package_fiddler',]

这些配置都导致根本没有文件安装在/home/username/.virtualenvs/virt_envir/local/lib/python2.7/site-packages.

编辑

Toshio Kuratomi的注意事项:在我的原始帖子中,为了清楚起见,我使用了最简单的树结构,但实际上我的树看起来更像下面的树。对于那棵树,奇怪的是,如果我只是__init__.pystylesheets某种方式放入文件夹中的所有数据文件texts也正确安装!!!这让我很困惑。

树 2(这会以某种方式正确安装所有数据文件!!)

.
├── MANIFEST.in
├── package_fiddler
│   │── stylesheets
|   |     ├── __init__.py
|   |     ├── example.css  
|   |     └── other
|   |          └── example2.css
|   |__ texts
|   |     ├── example.txt  
|   |     └── other
|   |          └── example2.txt
│   └── __init__.py
├── README.rst
└── setup.py
4

5 回答 5

37

在这里找到了对我有用的解决方案。

使用setuptools==2.0.2我做了:

setuptools.setup(
    ...
    packages=setuptools.find_packages(),
    include_package_data=True,  # use MANIFEST.in during install
    ...
)
于 2014-08-19T04:29:34.500 回答
29

我个人不喜欢 setuptools 在概念上和实现上混合代码和数据的方式。我认为正是这种实现让你陷入了困境。为了让 setuptools 找到并使用 package_data,它需要将数据驻留在 python 包中。python包可以是一个目录,但目录中必须有一个__init__.py文件。所以看起来你需要以下(空的很好)文件:

./package_fiddler/data/__init__.py
./package_fiddler/data/stylesheets/__init__.py
于 2012-11-09T20:01:35.753 回答
13

The easiest way to include package data in "setup.py" is like so:

package_data = {'<package name>': ['<path to data file within package dir>']}

So in your example:

package_data = {'package_fiddler': ['data/*', 'data/stylesheets/*']}

package_data is a dictionary where the keys are the names of the packages included in the installer. The values under these keys should be lists of specific file paths or globs/wildcards within the package directory.

You also need to include the flag:

zip_safe=False

in setup(...) if you want to be able to resolve file system paths to your data. Otherwise you can use pkg_resources to do this: http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources

You definitely don't need an __init__.py file in the "data" directory - this directory is not a module and is not meant to be imported.

于 2015-09-17T16:53:51.417 回答
-2

利用

package_data={"data": ['package_fiddler/data',]}

代替

packages=['package_fiddler',]
于 2012-11-09T11:41:07.803 回答
-2

这对我有用。希望能帮助到你。

package_data={
    "package_fiddler": [
        '\*.\*',
        '\*/\*.\*',
        '\*/\*/\*.\*',
    ],
},
于 2016-12-28T22:42:15.757 回答