setuptools:包数据文件夹位置 [英] setuptools: package data folder location

查看:37
本文介绍了setuptools:包数据文件夹位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 setuptools 分发我的 python 包.现在我需要分发额外的数据文件.

I use setuptools to distribute my python package. Now I need to distribute additional datafiles.

从我从 setuptools 文档中收集的信息来看,我需要将我的数据文件放在包目录中.但是,我宁愿将我的数据文件放在根目录的子目录中.

From what I've gathered fromt the setuptools documentation, I need to have my data files inside the package directory. However, I would rather have my datafiles inside a subdirectory in the root directory.

我想避免的:

/ #root
|- src/
|  |- mypackage/
|  |  |- data/
|  |  |  |- resource1
|  |  |  |- [...]
|  |  |- __init__.py
|  |  |- [...]
|- setup.py

我想要的:

/ #root
|- data/
|  |- resource1
|  |- [...]
|- src/
|  |- mypackage/
|  |  |- __init__.py
|  |  |- [...]
|- setup.py

如果不是必需的,我只是对拥有这么多子目录感到不舒服.我找不到原因,为什么我/have/将文件放在包目录中.恕我直言,使用这么多嵌套子目录也很麻烦.或者有什么好的理由可以证明这种限制是合理的?

I just don't feel comfortable with having so many subdirectories, if it's not essential. I fail to find a reason, why I /have/ to put the files inside the package directory. It is also cumbersome to work with so many nested subdirectories IMHO. Or is there any good reason that would justify this restriction?

推荐答案

选项 1:作为包数据安装

将数据文件放在 Python 包的根目录中的主要优点是它让您不必担心文件将在用户的系统,可能是 Windows、Mac、Linux、某些移动平台,或在 Egg 中.你可以始终找到相对于 Python 包根目录的 data 目录,无论它安装在何处或如何安装.

The main advantage of placing data files inside the root of your Python package is that it lets you avoid worrying about where the files will live on a user's system, which may be Windows, Mac, Linux, some mobile platform, or inside an Egg. You can always find the directory data relative to your Python package root, no matter where or how it is installed.

例如,如果我有这样的项目布局:

For example, if I have a project layout like so:

project/
    foo/
        __init__.py
        data/
            resource1/
                foo.txt

您可以在 __init__.py 中添加一个函数来定位数据的绝对路径文件:

You can add a function to __init__.py to locate an absolute path to a data file:

import os

_ROOT = os.path.abspath(os.path.dirname(__file__))
def get_data(path):
    return os.path.join(_ROOT, 'data', path)

print get_data('resource1/foo.txt')

输出:

/Users/pat/project/foo/data/resource1/foo.txt

项目安装为 Egg 后,data 的路径会改变,但代码不需要改变:

After the project is installed as an Egg the path to data will change, but the code doesn't need to change:

/Users/pat/virtenv/foo/lib/python2.6/site-packages/foo-0.0.0-py2.6.egg/foo/data/resource1/foo.txt

<小时>

选项 2:安装到固定位置

另一种方法是将您的数据放在 Python 包之外,然后要么:

The alternative would be to place your data outside the Python package and then either:

  1. 通过配置文件传入data的位置,命令行参数或
  2. 将该位置嵌入到您的 Python 代码中.
  1. Have the location of data passed in via a configuration file, command line arguments or
  2. Embed the location into your Python code.

如果您计划分发您的项目,这远不是可取的.如果您真的想要这样做,您可以通过传入元组列表来指定每组文件的目的地,将您的data安装在目标系统上的任何位置:

This is far less desirable if you plan to distribute your project. If you really want to do this, you can install your data wherever you like on the target system by specifying the destination for each group of files by passing in a list of tuples:

from setuptools import setup
setup(
    ...
    data_files=[
        ('/var/data1', ['data/foo.txt']),
        ('/var/data2', ['data/bar.txt'])
        ]
    )

更新:递归grep Python文件的shell函数示例:

Updated: Example of a shell function to recursively grep Python files:

atlas% function grep_py { find . -name '*.py' -exec grep -Hn $* {} \; }
atlas% grep_py ": \["
./setup.py:9:    package_data={'foo': ['data/resource1/foo.txt']}

这篇关于setuptools:包数据文件夹位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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