如何组织 PyPI 的 Python 模块以支持 2.x 和 3.x
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现
setup.py
对于httplib2
似乎有一种优雅的方式来支持 Python 2.x 和 3.x。所以我决定复制这个方法。任务是为包分发制作一个可与所有受支持的 Python 分发版配合使用的
setup.py
。然后使用相同的setup.py
,您可以执行以下操作:以及
应该可以使
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 扩展代码放在
python2
和python3
下的src
目录中。因此,目录结构是:
3)在
setup.py
中,我在顶部附近有这些行:4)在对
setup
的调用中,我将包指定为正常:5) 我使用
package_dir
选项指定了 Python 代码的基目录(请参阅步骤 3 了解base_dir
):6) 对于 C 扩展,我给出了路径:
setup.py
就是这样。setup.py
文件可由 Python 2.x 和 3.x 解析。7) 最后,如果您使用以下方式构建源发行版:
那么默认情况下它将仅提取为该 Python 构建特别需要的文件。例如,在上述情况下,您只能获取源代码分发中
python2
下的文件,而不是python3
下的文件。但对于完整的源代码发行版,您需要包含 2.x 和 3.x 的文件。为此,请创建一个MANIFEST.in
文件,其中包含如下内容:要查看我做了什么,请参阅 PyPI 或 BitBucket。
I found that
setup.py
forhttplib2
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 samesetup.py
, you can do:as well as
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 packagecobs
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 apython3
subdirectory.2) I put the C extension code for 2.x and 3.x in a
src
directory underpython2
andpython3
.So, the directory structure is:
3) In the
setup.py
, I had these lines near the top:4) In the call to
setup
, I specified the packages as normal:5) I specified the base directory for the Python code using a
package_dir
option (refer to step 3 forbase_dir
):6) For the C extensions, I gave the path:
That was about it for
setup.py
. Thesetup.py
file is parsable by both Python 2.x and 3.x.7) Finally, if you build a source distribution using:
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 underpython3
. But for a complete source distribution, you want to include the files for both 2.x and 3.x. To do that, create aMANIFEST.in
file that contains something like this:To see what I did, see the
cobs
source code on PyPI or BitBucket.最简单的解决方案是使用单一源发行版。
The simplest solution is to use a single source distribution.