如何为 PyPI 组织 Python 模块以支持 2.x 和 3.x [英] How to organize Python modules for PyPI to support 2.x and 3.x

查看:48
本文介绍了如何为 PyPI 组织 Python 模块以支持 2.x 和 3.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 模块,我想上传到 PyPI.到目前为止,它适用于 Python 2.x.现在为 3.x 编写一个版本应该不会太难.

I have a Python module that I would like to upload to PyPI. So far, it is working for Python 2.x. It shouldn't be too hard to write a version for 3.x now.

但是,在遵循在这些地方制作模块的指南之后:

But, after following guidelines for making modules in these places:

我不清楚如何为不同版本的 Python 支持多个源发行版,也不清楚 PyPI 是否/如何支持它.我设想我会有单独的代码:

it's not clear to me how to support multiple source distributions for different versions of Python, and it's not clear if/how PyPI could support it. I envisage I would have separate code for:

  • 2.x
  • 2.6(也许,作为使用新缓冲区 API 的特殊情况)
  • 3.x

如何在 PyPI 中设置 Python 模块以便有人可以这样做:

How is it possible to set up a Python module in PyPI so that someone can do:

easy_install modulename

无论用户使用 2.x 还是 3.x,它都会安装正确的东西?

and it will install the right thing whether the user is using 2.x or 3.x?

推荐答案

我发现 setup.py for httplib2 似乎有一种优雅的方式来支持 Python 2.x 和 3.x.所以我决定复制那个方法.

I found that setup.py for httplib2 seems to have an elegant way to support Python 2.x and 3.x. So I decided to copy that method.

任务是为适用于所有支持的 Python 发行版的包发行版制作一个 setup.py.然后使用相同的 setup.py,你可以:

The task is to craft a single setup.py for the package distribution that works with all the supported Python distributions. Then with the same setup.py, you can do:

python2 setup.py install

以及

python3 setup.py install

应该可以保持 setup.py 足够简单,以便使用所有支持的 Python 发行版进行解析.我已经使用支持 2.4 的包 cobs 成功地做到了这一点通过 2.6 和 3.1.该软件包包括纯 Python 代码(Python 2.x 和 3.x 的单独代码)和 C 扩展,分别为 2.x 和 3.x 编写.

It should be possible to keep setup.py simple enough to be parsed with all the supported Python distributions. I've successfully done so with a package cobs that supports 2.4 through 2.6 as well as 3.1. That package includes pure Python code (separate code for Python 2.x and 3.x) and C extensions, written separately for 2.x and 3.x.

这样做:

1) 我将 Python 2.x 代码放入 python2 子目录,将 Python 3.x 代码放入 python3 子目录.

1) I put the Python 2.x code into a python2 subdirectory, and Python 3.x code in a python3 subdirectory.

2) 我将 2.x 和 3.x 的 C 扩展代码放在 python2python3 下的 src 目录中.

2) I put the C extension code for 2.x and 3.x in a src directory under python2 and python3.

所以,目录结构是:

root
  |
  +--python2
  |     |
  |     +--src
  |
  +--python3
  |     |
  |     +--src
  |
  +--setup.py
  +--MANIFEST.in

3) 在 setup.py 中,我在顶部附近有这些行:

3) In the setup.py, I had these lines near the top:

if sys.version_info[0] == 2:
    base_dir = 'python2'
elif sys.version_info[0] == 3:
    base_dir = 'python3'

4) 在对 setup 的调用中,我将包指定为正常:

4) In the call to setup, I specified the packages as normal:

setup(
    ...
    packages=[ 'cobs', 'cobs.cobs', 'cobs.cobsr', ],

5) 我使用 package_dir 选项指定 Python 代码的基本目录(请参阅 base_dir 的第 3 步):

5) I specified the base directory for the Python code using a package_dir option (refer to step 3 for base_dir):

    package_dir={
        'cobs' : base_dir + '/cobs',
    },

6) 对于 C 扩展,我给出了路径:

6) For the C extensions, I gave the path:

    ext_modules=[
        Extension('cobs.cobs._cobs_ext', [ base_dir + '/src/_cobs_ext.c', ]),
        Extension('cobs.cobsr._cobsr_ext', [ base_dir + '/src/_cobsr_ext.c', ]),
    ],

setup.py 就是这样.setup.py 文件可由 Python 2.x 和 3.x 解析.

That was about it for setup.py. The setup.py file is parsable by both Python 2.x and 3.x.

7) 最后,如果你使用:

7) Finally, if you build a source distribution using:

python2 setup.py sdist

然后默认情况下它只会拉入专门为该 Python 构建所需的文件.例如.在上面的例子中,你只会得到源代码分发中 python2 下的文件,而不是 python3 下的文件.但是对于完整的源代码分发,您希望包含 2.x 和 3.x 的文件.为此,请创建一个 MANIFEST.in 文件,其中包含如下内容:

then it will by default pull in only the files that are specifically needed to build for that Python. E.g. in the above case, you would only get the files under python2 in the source distribution, but not those under python3. But for a complete source distribution, you want to include the files for both 2.x and 3.x. To do that, create a MANIFEST.in file that contains something like this:

include *.txt
recursive-include python2 *
recursive-include python3 *

要了解我做了什么,请参阅 PyPI<上的 cobs 源代码/a> 或 BitBucket.

To see what I did, see the cobs source code on PyPI or BitBucket.

这篇关于如何为 PyPI 组织 Python 模块以支持 2.x 和 3.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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