简体   繁体   English

当setup.py与Python版本3解释器一起运行时,如何构建py2 wheel软件包?

[英]How do I build a py2 wheel package, when setup.py runs with a Python version 3 interpreter?

I have a package that is supposed to be Python version 2 only but needs to be build running a version 3 interpreter. 我有一个仅应为Python 版本2的软件包,但需要构建运行版本3解释器的软件包。

The setup.py of this package looks something like hits: 该程序包的setup.py看起来像点击:

from setuptools import setup

setup(
    python_requires="<3.0, >=2.7.5",
    classifiers=[
        'Programming Language :: Python :: 2',
        'Intended Audience :: Developers',
    ],
    # ... more keyword arguments ... 
 )

If I call python2 setup.py build bdist_wheel , I get: 如果我调用python2 setup.py build bdist_wheelpython2 setup.py build bdist_wheel得到:

$ ls dist
mypackage-0.3.dev14-py2-none-any.whl

If I run it with a version 3 interpreter, ie python3 setup.py build bdist_wheel , I get: 如果使用版本3解释器(即python3 setup.py build bdist_wheel运行它, python3 setup.py build bdist_wheel得到:

$ ls dist
mypackage-0.3.dev14-py3-none-any.whl

I would have expected that regardless of the interpreter version, I will get a py2 package, because I specified it with python_requires (and in the tags). 我本来希望无论解释器版本如何,我都会得到一个py2包,因为我是使用python_requires (以及标签中)指定的。 My package build server only has a Python 3 interpreter. 我的软件包构建服务器只有一个Python 3解释器。

How can I build a wheel that targets Python 2 when running setuptools with a Python 3 interpreter? 使用Python 3解释器运行setuptools时,如何构建针对Python 2的轮子? Is that at all possible? 那有可能吗? Does the -py3- / -py2 in the filename mean something different than I think it does? 文件名中的-py3- / -py2表示的含义与我认为的有所不同吗?

Modified from How to force a python wheel to be platform specific when building it? 修改自《 如何在构建Python轮子时使其特定于平台》? , this change to setup.py seems to work. ,对此setup.py更改似乎可以正常工作。

But I suspect there might be a less hacky way. 但我怀疑可能没有那么hacky的方式。

from setuptools import setup

try:
    from wheel.bdist_wheel import bdist_wheel as _bdist_wheel

    class bdist_wheel(_bdist_wheel):

        def finalize_options(self):
            _bdist_wheel.finalize_options(self)
            self.root_is_pure = False  # Mark us as not a pure python package

        def get_tag(self):
            python, abi, plat = _bdist_wheel.get_tag(self)
            python, abi = 'py2', 'none'  # python, abi, plat = 'py2', 'none', 'any'  
            return python, abi, plat
except ImportError:
    bdist_wheel = None

setup(      
    cmdclass={'bdist_wheel': bdist_wheel}
    # ... other keyword args ...
)

Edit: 编辑:

With this solution the platform ( plat ) seems to change, because the resulting filename ends in -py2-none-linux_x86_64.whl . 使用此解决方案,平台( plat )似乎发生了变化,因为生成的文件名以-py2-none-linux_x86_64.whl

I suspect that is a consequence of self.root_is_pure = False . 我怀疑这是self.root_is_pure = False Since I have no binaries in my package I assume it's safe to set the platform to any ant pure to True . 由于我的软件包中没有二进制文件,因此我认为将平台设置为anyTrue蚂蚁都是安全的。

Edit2: EDIT2:

Another possible solution: 另一个可能的解决方案:

import sys
import setuptools

if 'bdist_wheel' in sys.argv:
    if not any(arg.startswith('--python-tag') for arg in sys.argv):
        sys.argv.extend(['--python-tag', 'py2'])

setuptools.setup(
    # ...
)

Try passing the python-tag argument to bdist_wheel: 尝试将python-tag参数传递给bdist_wheel:

python setup.py bdist_wheel --python-tag=py2

It could also be passed as 它也可以作为

from setuptools import setup
setup(options={'bdist_wheel':{'python_tag':'py2'}})

Or in setup.cfg 或在setup.cfg

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

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