Python打包:数据文件已正确放置在tar.gz文件中,但未安装到虚拟环境中 [英] Python Packaging: Data files are put properly in tar.gz file but are not installed to virtual environment
问题描述
我无法将项目package_fiddler
正确安装到我的虚拟环境中.
I can't properly install the project package_fiddler
to my virtual environment.
我发现 MANIFEST.in
负责将非.py文件放入执行python setup.py sdist
时生成的Package_fiddler-0.0.0.tar.gz
中.
I have figured out that MANIFEST.in
is responsible for putting the non-.py files in Package_fiddler-0.0.0.tar.gz
that is generated when executing 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
.
But this did not install the data files nor the package to /home/username/.virtualenvs/virt_envir/local/lib/python2.7/site-packages
.
我尝试了许多设置参数package_data
,include_package_data
和data_files
的配置,但每次似乎都使用了错误的配置.
I have tried many configurations of the setup arguments package_data
, include_package_data
and data_files
but I seem to have used the wrong configuration each time.
package_data
和/或include_package_data
和/或data_files
的哪个配置将package_fiddler
正确安装到我的虚拟环境?
Which configuration of package_data
and/or include_package_data
and/or data_files
will properly install package_fiddler
to my virtual environment?
项目树
.
├── MANIFEST.in
├── package_fiddler
│ ├── data
│ │ ├── example.html
│ │ └── stylesheets
│ │ └── example.css
│ └── __init__.py
├── README.rst
└── setup.py
setup.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',])
MANIFEST.in
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
上根本没有安装任何文件.
These configurations all result in no files at all being installed on /home/username/.virtualenvs/virt_envir/local/lib/python2.7/site-packages
.
仓美俊男的注释:
在我的原始帖子中,我使用了最简单的树结构,为清楚起见,发生了此问题,但实际上,我的树看起来更像下面的树.对于那棵树,奇怪的是,如果我只将__init__.py
放在stylesheets
中,则以某种方式也正确安装了texts
文件夹中的所有数据文件!这让我感到困惑.
Note to Toshio Kuratomi:
In my original post I used the simplest tree structure where this problem occurs for clarity but in reality my tree looks more like the tree below. For that tree, strangely if I only put an __init__.py
in stylesheets
somehow all the data files in the texts
folder are also installed correctly!!! This baffles me.
树2 (这将以某种方式正确安装所有数据文件!)
Tree 2 (This installs all data files properly somehow!!)
.
├── MANIFEST.in
├── package_fiddler
│ │── stylesheets
| | ├── __init__.py
| | ├── example.css
| | └── other
| | └── example2.css
| |__ texts
| | ├── example.txt
| | └── other
| | └── example2.txt
│ └── __init__.py
├── README.rst
└── setup.py
推荐答案
我个人不喜欢setuptools从概念上和实现上混合代码和数据的方式.我认为正是这种实现使您绊倒了.为了让setuptools查找和使用package_data,它需要将数据驻留在python包中. python包可以是目录,但该目录中需要一个__init__.py
文件.因此,您似乎需要以下(空的)文件:
I personally dislike the way setuptools mixes code and data both conceptually and implementation-wise. I think that it's that implementation that is tripping you up here. For setuptools to find and use package_data it needs for the data to reside inside of a python package. A python package can be a directory but there needs to be a __init__.py
file in the directory. So it looks like you need the following (empty is fine) files:
./package_fiddler/data/__init__.py
./package_fiddler/data/stylesheets/__init__.py
这篇关于Python打包:数据文件已正确放置在tar.gz文件中,但未安装到虚拟环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!