AttributeError:'module'对象没有属性'pydebug'

时间:2012-05-17 18:11:29

标签: python python-2.6

尝试运行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'

4 个答案:

答案 0 :(得分:3)

我试图在我自己构建的python上运行Ubuntu 12.04.1系统gdb时遇到了这个问题。我希望Ubuntu在系统gdb中构建一些钩子,以便它使用Python的调试版本;但钩子不会锁在我自己的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] $ 

答案 1 :(得分:3)

在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。错误 - 在/usr/local中的任何非品牌Python都不能以与编译Ubuntu Python相同的方式编译,因此gdb的期望值令人失望。

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

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

答案 2 :(得分:2)

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

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

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

答案 3 :(得分:0)

在Ubunut-12.04上,pyinstaller内置二进制文件调用&#34; site.py&#34;从主机python安装调用跟踪试图获取&#34; sys.pydebug&#34;值。

$ 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中添加了以下代码更改,为&#34; sys.pydebug&#34;返回零。

$ 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();