【问题标题】:AttributeError: 'module' object has no attribute 'pydebug'AttributeError:“模块”对象没有属性“pydebug”
【发布时间】:2012-05-17 18:11:28
【问题描述】:

尝试运行 python 脚本时,我收到错误 AttributeError: 'module' object has no attribute 'pydebug'。我正在使用 Python 2.6。

完全错误:

File "/lib/python2.6/distutils/sysconfig.py", line 238, in get_makefile_filename
    return os.path.join(lib_dir, "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'

【问题讨论】:

  • 你的代码是什么?哪一行抛出异常?
  • 将来您还应该提供代码 sn-p 本身。你从哪里得到这个代码?不是你自己写的?
  • 这很可能与实际脚本无关,因为 sysconfig 是在解释器启动期间导入的,在加载用户脚本之前。运行任何嵌入 Python 的程序时出现此错误,这使我的 Ubuntu 12.04 上的 gdb 无法使用。分发包中似乎存在一些奇怪的配置问题。

标签: python python-2.6


【解决方案1】:

我在自己构建的 python 上尝试运行 Ubuntu 12.04.1 系统 gdb 时遇到了这个问题。我希望 Ubuntu 已经在系统 gdb 中构建了一些钩子,以便它使用 Python 的调试版本;但是这些钩子并没有抓住我自己的蟒蛇中的任何东西。我通过构建自己的 gdb 并运行它来解决这个问题。

这是命令行和完整的回溯:

price@neverland:~/LSST/ip_diffim[master] $ gdb --args python tests/SnapPsfMatch.py 
Traceback (most recent call last):
  File "/usr/lib/python2.7/site.py", line 562, in <module>
    main()
  File "/usr/lib/python2.7/site.py", line 544, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/lib/python2.7/site.py", line 271, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/lib/python2.7/site.py", line 246, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/lib/python2.7/site.py", line 236, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/lib/python2.7/sysconfig.py", line 577, in get_config_var
    return get_config_vars().get(name)
  File "/usr/lib/python2.7/sysconfig.py", line 476, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/lib/python2.7/sysconfig.py", line 337, in _init_posix
    makefile = _get_makefile_filename()
  File "/usr/lib/python2.7/sysconfig.py", line 331, in _get_makefile_filename
    return os.path.join(get_path('platstdlib').replace("/usr/local","/usr",1), "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'

所以它似乎在寻找错误的 python(在/usr/lib),尽管我已经告诉系统不要这样做:

price@neverland:~/LSST/ip_diffim[master] $ which python
/home/price/eups/Linux/python/2.7.2/bin/python
price@neverland:~/LSST/ip_diffim[master] $ echo $PYTHONPATH | grep usr
price@neverland:~/LSST/ip_diffim[master] $ 

【讨论】:

  • 这很可能是由于路径中存在错误的 python 可执行文件引起的。我使用 strace 找出它是哪一个(在我的例子中是 /usr/local/bin/python2.7),删除它并尝试重新安装 python。
  • 根据我的经验,从LD_LIBRARY_PATH 中省略非系统的Python lib 目录可以避免错误(当然它不允许将gdb 附加到该Python,只能运行gdb自己)。
【解决方案2】:

当我在 Ubuntu 系统上运行 gdb 时收到错误消息,该系统已经安装了 Python 的替代版本并且链接器首选该版本。您可以通过使用 ldd 询问 gdb 正在使用哪些库来验证您的情况是否发生这种情况:

# ldd $(which gdb)
...
        libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00007ff75e044000)
...

您可以看到,在 /usr/local/lib 中运行的非品牌 Python 版本将 libpython 提供给 gdb,而不是在 /usr/lib 中提供官方 Ubuntu Python,这会导致错误 — 无论是什么非品牌 Python在/usr/local 中的编译方式一定与Ubuntu Python 的编译方式不同,因此gdb 的期望值令人失望。

解决方案是使用环境来控制链接器的行为并使其更喜欢系统libpython。为了更好地衡量,我还将我的PATH 重新设置为完全标准的东西。我发现这行得通:

PATH=/bin:/usr/bin LD_LIBRARY_PATH=/usr/lib gdb ...

【讨论】:

    【解决方案3】:

    我认为无论您尝试运行什么,都希望与 python 的特殊调试版本一起使用。 sys.pydebug 通常不会在 sys 模块的标准版本中找到,我相信如果你构建了一个调试 python,它就会在那里:

    http://docs.python.org/c-api/intro.html#debugging-builds

    这也可能是 Debian/Ubuntu 发行版正在使用的特定构建的一部分。

    【讨论】:

      【解决方案4】:

      在 Ubunut-12.04 pyinstaller 构建的二进制文件中,从主机 python 安装调用“site.py”,调用跟踪试图获取“sys.pydebug”值。

      $ python
      Python 2.7.3 (default, Feb 27 2014, 19:58:35)
      [GCC 4.6.3] on linux2
      Type "help", "copyright", "credits" or "license" for more 
      information.
      >>> import sys
      >>> sys.pydebug
      False
      

      自定义构建的 python 缺少这个。

      HACK:让 pyinstaller 二进制文件在 Ubuntu-12.04 上运行。 在自定义构建的 python 中添加了以下代码更改,它为“sys.pydebug”返回零。

      $ diff -Naur Python/sysmodule-org.c Python/sysmodule.c
      --- Python/sysmodule-org.c      2018-03-15 09:37:26.539515000 -0700
      +++ Python/sysmodule.c  2018-03-15 19:58:34.503031000 -0700
      @@ -1106,6 +1106,7 @@
      maxunicode -- the largest supported character\n\
      builtin_module_names -- tuple of module names built into this 
      interpreter\n\
      version -- the version of this interpreter as a string\n\
      +pydebug -- always return zero\n\
      version_info -- version information as a named tuple\n\
      hexversion -- version information encoded as a single integer\n\
      copyright -- copyright notice pertaining to this interpreter\n\
      @@ -1420,6 +1421,8 @@
      
           SET_SYS_FROM_STRING("version",
                            PyString_FromString(Py_GetVersion()));
      +    SET_SYS_FROM_STRING("pydebug",
      +                         PyInt_FromLong(0));
           SET_SYS_FROM_STRING("hexversion",
                            PyInt_FromLong(PY_VERSION_HEX));
        svnversion_init();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-18
        相关资源
        最近更新 更多