如何将静态文件包含到setuptools-python包


83

我想包含./static/data.txtto setuptools,这是我的代码:

# setup.py
import os,glob
from setuptools import setup,find_packages

setup(
    name = "PotatoProject",
    version = "0.1.1",
    author = "Master Splinter",
    author_email = "splinter@initech.com",
    description = ("The potatoproject!"),
    url = 'http://www.google.com',
    license = "BSD",

    # adding packages
    packages=find_packages('src'),
    package_dir = {'':'src'},

    # trying to add files...
    include_package_data = True,
    package_data = {
        '': ['*.txt'],
        '': ['static/*.txt'],
        'static': ['*.txt'],
    },

    scripts=['src/startPotato'],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
)

文件系统:

.
├── setup.py
└── src
    ├── distutils_setup.py
    ├── Potato
    │   ├── __init__.py
    │   ├── potatoData.txt
    │   └── printer.py
    ├── startPotato
    ├── static
    │   └── data.txt
    └── Tomato
        ├── big.py
        └── __init__.py

运行时的输出: python setup.py sdist

running sdist
running egg_info
creating src/PotatoProject.egg-info
writing src/PotatoProject.egg-info/PKG-INFO
writing top-level names to src/PotatoProject.egg-info/top_level.txt
writing dependency_links to src/PotatoProject.egg-info/dependency_links.txt
writing manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
reading manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
writing manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.txt

creating PotatoProject-0.1.1
creating PotatoProject-0.1.1/src
creating PotatoProject-0.1.1/src/Potato
creating PotatoProject-0.1.1/src/PotatoProject.egg-info
creating PotatoProject-0.1.1/src/Tomato
making hard links in PotatoProject-0.1.1...
hard linking setup.py -> PotatoProject-0.1.1
hard linking src/startPotato -> PotatoProject-0.1.1/src
hard linking src/Potato/__init__.py -> PotatoProject-0.1.1/src/Potato
hard linking src/Potato/printer.py -> PotatoProject-0.1.1/src/Potato
hard linking src/PotatoProject.egg-info/PKG-INFO -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/SOURCES.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/dependency_links.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/top_level.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/Tomato/__init__.py -> PotatoProject-0.1.1/src/Tomato
hard linking src/Tomato/big.py -> PotatoProject-0.1.1/src/Tomato
Writing PotatoProject-0.1.1/setup.cfg
creating dist
Creating tar archive
removing 'PotatoProject-0.1.1' (and everything under it)

并且没有添加txt!不,static/data.txt也不Potato/potatoData.txt...

我想念什么?

Answers:


122

正如评论中指出的那样,有两种添加静态文件的方法:

1-include_package_data = True + MANIFEST.in

一个MANIFEST.in在同一目录下的文件setup.py,看起来像这样:

include src/static/*
include src/Potato/*.txt

include_package_data = Truesetup.py

2-setup.py中的package_data

package_data = {
    'static': ['*'],
    'Potato': ['*.txt']
}

在中指定文件setup.py


不要同时使用include_package_data,并package_datasetup.py

include_package_data将使package_data信息无效。

https://setuptools.readthedocs.io/zh-CN/latest/setuptools.html#include-data-files


3
作品!显然,MANIFEST.in除了选择setup.py / distutils足够聪明以默认包含的文件之外,还选择包含哪些文件。
chbrown 2014年

13
这样做并添加include_package_data = True以上内容对我来说是可行的。两者都必须完成:清单文件和setup.py指令。
djhaskin987 2015年

我只添加“包括* .txt”。伟大的thnx
Priyeshj 2015年

7
应该注意的是,无论何时指定include_package_data=True,基本上都会使的作用无效package_data,这就是setuptools希望您在文件中包含文件的原因MANIFEST.in。使用其中一个(package_datavs include_package_data=True+ MANIFEST.in),不要同时使用两者。
konoufo

在运行时是否可以写入静态目录?
毛的

28

递归包括所有文件:

recursive-include project_name/templates *
recursive-include project_name/static *

这里project_name是在你有相同的线的一个文件夹setup.py的文件。


1
您可能还希望包含一个文件,include funniest/data.json例如:
Rodrigo Dias

11

根据文档,包含包数据文件的方法三种。您有两个包:土豆和西红柿。静态目录不在这两个软件包中,因此这就是setup.py中的package_data词典不起作用的原因。清单选项要求在setup.py中将include_package_data设置为True。可以通过此处找到的方式来访问非包数据文件。


6

使用以下

packages = ['.','templates','static','docs'],

package_data={'templates':['*'],'static':['*'],'docs':['*'],},

1
这可能太包容了。
Acumenus

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.