Python使用不同的数据目录打包多个子包 [英] Python Packaging multiple subpackages with different data directories

查看:232
本文介绍了Python使用不同的数据目录打包多个子包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有目录结构,例如foobaralphabet数据目录以及代码something.py:

I have a structure of the directory as such with foobar and alphabet data directories together with the code something.py:

\mylibrary
    \packages
         \foobar
             foo.zip
             bar.zip
         \alphabet
             abc.zip
             xyz.zip
          something.py
     setup.py

目标是使用户可以像这样通过pip安装该模块:

And the goal is such that users can pip install the module as such:

pip install mylibrary[alphabet]

并且仅包含packages/alphabet/*数据和python代码中的数据.类似的行为应该对pip install mylibrary[foobar]可用.

And that'll only include the data from the packages/alphabet/* data and the python code. Similar behavior should be available for pip install mylibrary[foobar].

如果用户未按照规范进行安装:

If the user installs without the specification:

pip install mylibrary

然后它将包含packages/下的所有数据目录.

Then it'll include all the data directories under packages/.

目前,我已经尝试使用Python3.5编写setup.py了:

Currently, I've tried writing the setup.py with Python3.5 as such:

import glob
from setuptools import setup, find_packages


setup(
  name = 'mylibrary',
  packages = ['packages'],
  package_data={'packages':glob.glob('packages' + '/**/*.txt', recursive=True)},
)

这将在用户执行pip install mylibrary时创建具有所有数据目录的分发.

That will create a distribution with all the data directories when users do pip install mylibrary.

我应该如何更改setup.py,以便可以安装像pip install mylibrary[alphabet]这样的特定点子?

How should I change the setup.py such that specific pip installs like pip install mylibrary[alphabet] is possible?

推荐答案

您必须打包并发布alphabetfoobar作为pip install mylibrary[alphabet]表示的单独程序包

Firs you have to package and publish alphabet and foobar as a separate packages as pip install mylibrary[alphabet] means

pip install mylibrary
pip install alphabet

之后,将alphabetfoobar添加为extras:

setup(
    …,
    extras = {
        'alphabet': ['alphabet'],
        'foobar': ['foobar'],
    }
)

字典中的键是pip install mylibrary[EXTRA_NAME]中使用的名称,值是将从PyPI安装的软件包名称的列表.

The keys in the dictionary are the names used in pip install mylibrary[EXTRA_NAME], the values are a list of package names that will be installed from PyPI.

PS.不,您不能使用Extras安装一些数据文件,这些数据文件不能从PyPI包中获得.

PS. And no, you cannot use extras to install some data files that are not available as packages from PyPI.

这篇关于Python使用不同的数据目录打包多个子包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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