简体   繁体   English

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

[英]How to organize Python modules for PyPI to support 2.x and 3.x

I have a Python module that I would like to upload to PyPI. 我有一个Python模块,我想上传到PyPI。 So far, it is working for Python 2.x. 到目前为止,它适用于Python 2.x. It shouldn't be too hard to write a version for 3.x now. 现在编写3.x版本应该不会太难。

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. 我不清楚如何为不同版本的Python支持多个源代码发布,并且不清楚PyPI是否/如何支持它。 I envisage I would have separate code for: 我设想我会有单独的代码:

  • 2.x 2.X
  • 2.6 (maybe, as a special case to use the new buffer API) 2.6(也许,作为使用新缓冲区API的特殊情况)
  • 3.x 3.X

How is it possible to set up a Python module in PyPI so that someone can do: 如何在PyPI中设置Python模块,以便有人可以:

easy_install modulename

and it will install the right thing whether the user is using 2.x or 3.x? 无论用户是使用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. 我发现httplib2 setup.py似乎有一种优雅的方式来支持Python 2.x和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. 任务是为包分发制作单个setup.py ,该分发适用于所有受支持的Python发行版。 Then with the same setup.py , you can do: 然后使用相同的setup.py ,您可以执行以下操作:

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. 应该可以使setup.py足够简单,以便使用所有受支持的Python发行版进行解析。 I've successfully done so with a package cobs that supports 2.4 through 2.6 as well as 3.1. 我已经成功地与一个包这样做的cobs ,支持2.4通过2.6和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. 该软件包包括纯Python代码(Python 2.x和3.x的单独代码)和C扩展,分别为2.x和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. 1)我将Python 2.x代码放入python2子目录,将Python 3.x代码放在python3子目录中。

2) I put the C extension code for 2.x and 3.x in a src directory under python2 and python3 . 2)我将2.x和3.x的C扩展代码放在python2python3下的src目录中。

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: 3)在setup.py ,我将这些行放在顶部附近:

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: 4)在setup调用中,我将包指定为正常:

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 ): 5)我使用package_dir选项为Python代码指定了基本目录(请参阅base_dir步骤3):

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

6) For the C extensions, I gave the path: 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', ]),
    ],

That was about it for setup.py . 那是关于setup.py The setup.py file is parsable by both Python 2.x and 3.x. setup.py文件可由Python 2.x和3.x解析。

7) Finally, if you build a source distribution using: 7)最后,如果您使用以下方法构建源代码分发:

python2 setup.py sdist

then it will by default pull in only the files that are specifically needed to build for that Python. 那么它默认只会拉入为Python构建特别需要的文件。 Eg in the above case, you would only get the files under python2 in the source distribution, but not those under python3 . 例如,在上面的例子中,你只能在源代码发行版中获取python2下的文件,而不是python3下的文件。 But for a complete source distribution, you want to include the files for both 2.x and 3.x. 但是对于完整的源代码分发,您希望包含2.x和3.x的文件。 To do that, create a MANIFEST.in file that contains something like this: 为此,请创建一个包含以下内容的MANIFEST.in文件:

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

To see what I did, see the cobs source code on PyPI or BitBucket . 看我做什么,看到cobs源代码的PyPI到位桶

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM