【问题标题】:2.7 setup script fails under 3.42.7 安装脚本在 3.4 下失败
【发布时间】:2014-09-16 07:04:53
【问题描述】:

我有一个带有安装脚本的软件包,在 2.7 下可以正常安装,但是在 3.4 下运行时安装脚本(使用 setuptools)失败:

± python setup.py develop
running develop
Traceback (most recent call last):
  File "setup.py", line 45, in <module>
    url = "http:www.planetfour.org",
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/distutils/dist.py", line 973, in run_command
    cmd_obj.ensure_finalized()
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/distutils/cmd.py", line 107, in ensure_finalized
    self.finalize_options()
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/setuptools/command/develop.py", line 50, in finalize_options
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/setuptools/command/easy_install.py", line 313, in finalize_options
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/setuptools/package_index.py", line 269, in __init__
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 802, in __init__
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 832, in scan
  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 852, in add
TypeError: unorderable types: str() < NoneType()
(py34)-> [1]

我尝试了什么:

  • 我在安装 2.7 版本的同一源文件夹中执行此操作,这是否有害/危险?我删除了所有 *.pyc FWIW?
  • 我尝试了 2to3,如果它会在我的 setup.py 脚本中显示任何必需的更改,但它说不需要:
± 2to3 setup.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: No changes to setup.py
RefactoringTool: Files that need to be modified:
RefactoringTool: setup.py
(py34)maye@lunatic|~/Dropbox/src/P4_sandbox on master

根据评论进行编辑: 我能找到的唯一命令是install_requires 行: install_requires = ['pandas>='+pandas_version] 和 pandas_version = '0.13.1'

如果这是罪魁祸首,如何在 Python 3.4 中正确地做到这一点?

这是整个 setup.py:

import ez_setup
ez_setup.use_setuptools()
import sys
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

pandas_version = '0.13.1'

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = ['-v']
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.test_args)
        sys.exit(errno)


setup(
    name = "Planet4",
    version = "0.1beta2",
    packages = find_packages(),

    install_requires = ['pandas>='+pandas_version],
    tests_require = ['pytest'],

    cmdclass = {'test': PyTest},

    entry_points={
        "console_scripts": [
            'p4reduction = planet4.reduction:main',
            'plot_p4_imageid = planet4.markings:main',
            ]
    },

    #metadata
    author = "K.-Michael Aye",
    author_email = "kmichael.aye@gmail.com",
    description = "Software for the reduction and analysis of Planet4 data.",
    license = "BSD 2-clause",
    keywords = "Mars Planet4 Zooniverse",
    url = "http:www.planetfour.org",
)

【问题讨论】:

  • 2to3 并不完美;排序的语法仍然相同,但行为发生了变化。见docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
  • 啊,那么一定是这一行:install_requires = ['pandas&gt;='+pandas_version] with pandas_version = '0.13.1' ?那么如何在 3.4 中正确执行此操作?我在 setuptools 文档中看不到 3.4 的任何特殊说明?
  • 不,这不是排序,只是字符串连接。这是一个干净的虚拟环境吗?
  • 清洁是如何定义的?它是用 conda for python 3.4 设置的,一些包是通过 pip 进来的,它是通过“source activate ”系统激活的。我还能说什么?
  • 但是当确定可用的 pandas 版本是否 >= 所需的版本时,难道没有某处发生排序吗?

标签: python-3.x setuptools setup.py


【解决方案1】:

traceback的最后一个源码行:

  File "/Users/maye/miniconda3/envs/py34/lib/python3.4/site-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 852, in add

如果您转到该行并开始向后工作,您有望找到正在比较的两个变量——其中一个是str,另一个是NoneNone 变量可能是从失败的查找中得到的。

找出查找失败的原因,也许您可​​以通过修改setup.py 来解决问题——否则您可能需要修补pkg_resources.py 以处理一个变量为None 的情况。

深入setuptools 5.7版本,我们在错误站点发现这个:

def add(self, dist):
    """Add `dist` if we ``can_add()`` it and it has not already been added
    """
    if self.can_add(dist) and dist.has_version():
        dists = self._distmap.setdefault(dist.key, [])
        if dist not in dists:
            dists.append(dist)
            dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)

最后一行是发生错误的地方 -- 在对 dists 进行排序时。

正在使用的keyhashcmp

@property
def hashcmp(self):
    return (
        getattr(self, 'parsed_version', ()),
        self.precedence,
        self.key,
        _remove_md5_fragment(self.location),
        self.py_version,
        self.platform,
    )

如您所见,它不会返回 None


关于我剩下的唯一想法是:您是否已经在 Python 路径中的某个位置安装了一个版本?如果你这样做了,可能是元数据丢失了一些重要的部分,这就是你收到错误的原因。

【讨论】:

  • 伙计,有人声称 Python3 错误消息已得到改进。 :(
  • 我正在使用 conda 虚拟环境,因此路径中没有其他版本。所以你是说,hashcmp 不可能返回“None”,对吧?令人费解。另一方面,错误消息表明使用 str() 尝试比较失败。我也不明白它怎么可能是一个字符串,因为 hashcmp 返回一个大元组。使用这个大元组作为键,排序如何工作?
  • @K.-MichaelAye: tuples 是按元素排序的。尝试在sort 之前放置一个print(dists),看看会发生什么——希望你能从那里找到它。
【解决方案2】:

我认为这个错误可能导致了这种行为:

https://bitbucket.org/pypa/setuptools/issue/137/typeerror-unorderable-types-str-nonetype

特别是,以下错误症状可以帮助其他人确定这是否是他们的问题(这使用conda经理,但应该与其他人类似):

conda create -n test-hash python=3 pip
source activate test-hash
pip install requests
conda install requests
pip install anything # <- fails with str < None

问题是,IIUC,distutils 和 setuptools 现在因为站点包中有超过 1 个 Python Egg 而感到困惑,并且在找到现在最适合安装的版本时无法正确sort 它们。

两个动作帮助我解决了这个问题:

  1. 使用 setuptools 而不是 distutils。为此,在setup.py 我替换了

    从 distutils 导入设置

from setuptools import setup
  1. setuptools升级到最近发布的14.3版,解决了上面链接的难以重现的bug。

我正在使用我的 conda 包管理器来执行此操作,但它不应该依赖于此。

【讨论】:

  • 已更新,希望您喜欢。如果您同意,如果您删除反对票会很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2017-08-02
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
相关资源
最近更新 更多