python 为什么www.example.com的存在会token.py导致内置help()中断?[duplicate]

7uzetpgm  于 2023-01-08  发布在  Python
关注(0)|答案(4)|浏览(117)
    • 此问题在此处已有答案**:

Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError(2个答案)
14小时前关门了。
As a hobby/learning project, I'm writing a parser generator in Python. One of my code files is named "token.py" - which contains a couple of classes for turning plain strings into Token objects. I've just discovered that using the "help()" function from the console in Python raises an error for any module defined in a directory that contains a "token.py".
以下是重现错误的方法。创建包含以下文件的新目录:

/New Folder
  main.py
  token.py

Leave 'token.py' blank. In main.py, write a simple function - for example:

def test():
    pass

然后,在Python控制台中,导入'main'并调用'help(main.test)'--下面是您将得到的结果:

C:\New Folder>python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import main
>>> help(main.test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python31\lib\site.py", line 428, in __call__
    import pydoc
  File "C:\Python31\lib\pydoc.py", line 55, in <module>
    import sys, imp, os, re, inspect, builtins, pkgutil
  File "C:\Python31\lib\inspect.py", line 40, in <module>
    import tokenize
  File "C:\Python31\lib\tokenize.py", line 37, in <module>
    COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined
>>>

如果你删除了'token.py'文件,help()将正常工作,Python 3.1和Python 2.5都有这种行为。
这是已知问题吗?如果不是,我如何报告?
编辑:
Several comments state that this behavior isn't a bug. The module that defines "help" imports a module called "token" from Python's standard library. However, Python looks in the application folder before it looks in its library to find modules. In the example above, "help" tries to use my "token.py" instead of Python's, which causes the error.
由于Python被定义为表现这种行为,我想这不是一个bug。但是为什么人们认为这种行为是可以接受的呢?这意味着向Python库中添加新模块--即使不更改现有模块--也可能破坏现有应用程序。这还意味着程序员应该记住Python库中所有模块的名称--这比期望程序员记住. NET或Java中的每个名称空间更荒谬吗?为什么Python应用程序没有自己的名称空间?为什么Python标准库模块不在自己的名称空间中?

y4ekin9u

y4ekin9u1#

问题是你的本地token.pyhelp()导入,而不是Python的实际token.py。这种情况会发生在任何数量的.py文件中,这些文件的名称与内置模块冲突。例如,尝试在CWD中创建一个pydoc.py文件,然后在Python中尝试help()help()函数只是一个内置的Python函数。所以它和其他Python代码的导入路径一样。

798qvoo8

798qvoo82#

无论何时,只要你选择模仿Python标准库中定义的模块名来命名你的模块,你就要对结果负全责--其他Python标准库模块很可能依赖于你隐藏/覆盖的模块,如果你自己的模块没有仔细模仿你隐藏的模块的功能,那不是Python的bug:这是代码中的一个bug。当然,这同样适用于模块令牌和其他任何令牌。
如果这是偶然发生在您身上的,并且您正在寻找一种方法来检查代码中可能存在的bug或有问题的构造(而不是像您所说的,寻找一种方法来报告Python标准库中不存在的bug),我认为像pylint这样的工具可能会有所帮助。

7rtdyuoh

7rtdyuoh3#

您可以在此处搜索问题并报告新问题:http://bugs.python.org/.

ep6jt1vc

ep6jt1vc4#

这不是help()方法的bug,标准库中有一个名为 * tokenize. py * 的模块,它是由 * tokenize. py * 导入的(您看到的错误就是从这里产生的)。
from tokenize.py:

from token import *

So tokenize.py expects to have a bunch of variables from the standard library token.py, but since the token.py in your working directory is actually imported they are not present which is causing the NameError (one amoungst many reasons *import**shouldn't be used).

相关问题