【问题标题】:How do I build a py2 wheel package, when setup.py runs with a Python version 3 interpreter?当 setup.py 使用 Python 版本 3 解释器运行时,如何构建 py2 Wheel 包?
【发布时间】:2019-07-26 01:30:31
【问题描述】:

我有一个包应该是 Python 仅版本 2,但需要构建运行版本 3 解释器。

这个包的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 ... 
 )

如果我打电话给python2 setup.py build bdist_wheel,我会得到:

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

如果我使用版本 3 解释器运行它,即python3 setup.py build bdist_wheel,我会得到:

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

我本来希望不管解释器版本如何,我都会得到一个 py2 包,因为我用python_requires(和标签)指定了它。我的包构建服务器只有一个 Python 3 解释器。

当使用 Python 3 解释器运行 setuptools 时,如何构建针对 Python 2 的轮子?这有可能吗?文件名中的-py3-/-py2 的含义是否与我认为的不同?

【问题讨论】:

    标签: python setuptools packaging python-wheel


    【解决方案1】:

    How to force a python wheel to be platform specific when building it? 修改,此更改为setup.py 似乎有效。

    但我怀疑可能有一种不那么老套的方法。

    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 ...
    )
    

    编辑:

    使用此解决方案,平台 (plat) 似乎发生了变化,因为生成的文件名以 -py2-none-linux_x86_64.whl 结尾。

    我怀疑这是self.root_is_pure = False 的结果。由于我的包中没有二进制文件,我认为将平台设置为any ant pure 为True 是安全的。

    编辑2:

    另一种可能的解决方案:

    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(
        # ...
    )
    

    【讨论】:

      【解决方案2】:

      尝试将 python-tag 参数传递给 bdist_wheel:

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

      也可以传递为

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

      或在setup.cfg

      【讨论】:

      • 不错的解决方案,但不幸的是它给了我'bdist_wheel' has no such option 'python-tag'。即使在将wheel 包升级到v0.33 之后。
      • 看起来它必须是 options={} 字典中的 python_tag(带下划线)。
      • 确实如此。您还缺少一个右括号,希望您不介意我编辑了它。
      猜你喜欢
      • 2012-12-05
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2018-06-18
      • 2011-05-29
      • 1970-01-01
      • 2013-12-11
      • 2015-05-27
      相关资源
      最近更新 更多