【问题标题】:Where can I get the Python 2to3 library on Buildroot在哪里可以获得 Buildroot 上的 Python 2to3 库
【发布时间】:2018-01-17 16:33:59
【问题描述】:

在 Buildroot Linux 系统上的 Python 2.7 中导入 gmusicapi 时出现以下错误:

>>> import gmusicapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gmusicapi/__init__.py", line 4, in <module>
    from gmusicapi.clients import Webclient, Musicmanager, Mobileclient
  File "gmusicapi/clients/__init__.py", line 4, in <module>
    from gmusicapi.clients.webclient import Webclient
  File "gmusicapi/clients/webclient.py", line 5, in <module>
    from past.builtins import basestring
  File "past/__init__.py", line 88, in <module>
    from past.translation import install_hooks as autotranslate
  File "past/translation/__init__.py", line 41, in <module>
    from lib2to3.pgen2.parse import ParseError
ImportError: No module named lib2to3.pgen2.parse

Python 找不到 lib2to3。我也不行 ;-)。有没有什么地方可以下载这个库?我正在使用 Buildroot,所以我不能简单地进行 pip 安装。

不是以下的副本: How to use/install python 2to3?

【问题讨论】:

  • 根据docs,“2to3 通常会以脚本的形式与 Python 解释器一起安装。它也位于 Python 根目录的 Tools/scripts 目录中。”
  • 这是什么操作系统? Python是如何安装的? lib2to3标准库的一部分,应该已经自动安装了。例如,在 Ubuntu 上,我希望 libpython2.7-stdlib package(包含 lib2to3)已作为 python2.7 的依赖项安装。
  • @ForceBru:我也没有这个目录,我希望它不是一个工具,而是一个 Python 库,因为 Python 代码会导入它。
  • @MartijnPieters:Linux (Buildroot)。也许它在一个名称有误导性的库中,但没有什么像 lib2to3 或任何指向那个方向的东西。

标签: python python-2.7 buildroot


【解决方案1】:

lib2to3 是一个标准库,通常包含在 Python 中。但是,buildroot 构建系统explicitly removes it

我不确定 Buildroot 是否真的允许你禁用他们的补丁; python.mk file 似乎对标志进行了硬编码:

PYTHON_CONF_OPTS += \
    --without-cxx-main \
    --without-doc-strings \
    --with-system-ffi \
    --disable-pydoc \
    --disable-test-modules \
    --disable-lib2to3 \
    --disable-gdbm \
    --disable-tk \
    --disable-nis \
    --disable-dbm \
    --disable-pyo-build \
    --disable-pyc-build

我没有看到添加--enable-lib2to3 来覆盖它的选项。如果这完全可以选择,您可能需要咨询 Buildroot 社区。否则,我只需要编辑那个 make 文件。

【讨论】:

  • 在嵌入式系统上使用 2to3 并不理想,这就是它被禁用的原因。
【解决方案2】:

解决方法:修补过去的库(问题的根源):

--- past/translation/__init__.py.orig   2017-09-24 08:23:47.646644743 +0200
+++ past/translation/__init__.py    2018-01-01 10:31:36.584576652 +0100
@@ -38,8 +38,32 @@
 import os
 import sys
 import copy
-from lib2to3.pgen2.parse import ParseError
-from lib2to3.refactor import RefactoringTool
+
+try:
+    from lib2to3.pgen2.parse import ParseError
+
+except ImportError:
+
+    class ParseError(SyntaxError):
+        pass
+
+try:
+    from lib2to3.refactor import RefactoringTool
+
+except ImportError:
+
+    class RefactoringTool:
+
+        def __init__(self, *args, **kwargs):
+            self._args = [repr(a) for a in args]
+            self._args += ["{}={!r}".format(k, v) for k, v in kwargs.items()]
+
+        def __repr__(self):
+            return "{}({})".format(self.__class__.__name__, ", ".join(self._args))
+
+        def refactor_string(self, *args, **kwargs):
+            raise NotImplementedError("dummy RefactoringTool")
+

 from libfuturize import fixes

在这里找到:https://github.com/PythonCharmers/python-future/issues/209

如果有人可以将我指向可下载的 lib2to3,我会接受这个答案,现在这个解决方法可以解决问题。

【讨论】:

  • 请将您的问题发布到 Buildroot 的邮件列表。
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 2010-10-20
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多