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

时间:2012-08-07 14:19:51

标签: python package setuptools

我想将./static/data.txt添加到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 ...

我缺少什么?!

5 个答案:

答案 0 :(得分:76)

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

1 - include_package_data = True + MANIFEST.in

同一MANIFEST.in目录中的setup.py文件,如下所示:

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

使用include_package_data = True中的setup.py

2 - setup.py

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

指定setup.py内的文件。


请勿在{{1​​}}中同时使用include_package_datapackage_data

setup.py会使include_package_data信息无效。

https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files

答案 1 :(得分:24)

递归包含所有文件:

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

其中project_name是您拥有setup.py文件的同一行中的文件夹。

答案 2 :(得分:11)

根据文档,有three ways包含数据包数据文件。你有两个包:土豆和番茄。静态目录不在这些包中,因此这就是setup.py中的package_data字典不起作用的原因。 manifest选项要求在setup.py中将include_package_data设置为True。可以按here找到的方式访问非包数据文件。

答案 3 :(得分:6)

使用以下

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

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

答案 4 :(得分:1)

将其放在此处,因为它已在此处所述的评论中丢失。

How include static files to setuptools - python package

主要问题是您不能同时拥有package_data和include_package_data = True