如何组织 PyPI 的 Python 模块以支持 2.x 和 3.x

发布于 2024-08-24 09:17:48 字数 600 浏览 5 评论 0原文

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

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

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

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

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

easy_install modulename

:无论用户使用 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:

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 (maybe, as a special case to use the new buffer API)
  • 3.x

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

easy_install modulename

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-08-31 09:17:48

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

任务是为包分发制作一个可与所有受支持的 Python 分发版配合使用的 setup.py。然后使用相同的 setup.py,您可以执行以下操作:

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 编写。

为此:

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

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

因此,目录结构是:

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

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

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

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

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

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

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

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

    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 解析。

7) 最后,如果您使用以下方式构建源发行版:

python2 setup.py sdist

那么默认情况下它将仅提取为该 Python 构建特别需要的文件。例如,在上述情况下,您只能获取源代码分发中 python2 下的文件,而不是 python3 下的文件。但对于完整的源代码发行版,您需要包含 2.x 和 3.x 的文件。为此,请创建一个 MANIFEST.in 文件,其中包含如下内容:

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

要查看我做了什么,请参阅 PyPIBitBucket

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.

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

as well as

python3 setup.py install

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.

To do it:

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

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

So, the directory structure is:

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

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) In the call to setup, I specified the packages as normal:

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

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) 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', ]),
    ],

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

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

python2 setup.py sdist

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 *

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

好久不见√ 2024-08-31 09:17:48

最简单的解决方案是使用单一源发行版。

The simplest solution is to use a single source distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文