我是新手,我编写了一个实用程序脚本来生成一些我需要的数字.我把这个文件命名为numbers.py
.该脚本依赖于PyGame库.我发现仅仅导入该库就会导致错误,因为PyGametry 从Python标准库转换为import numbers
,并且默认的查找规则有我自己的文件来跟踪标准库.
我试过的短毛(皮林特,拉夫)似乎没有抓住我这个新秀的错误,但我很难理解如何最好地避免它.据我所知,"只需重命名文件"以避免与标准库冲突的建议需要以下两种之一:
- 记住307个标准库名称(没有句点;总共1027个名称),或者
- 能够通过来自其他库的错误来识别,也许你不小心隐藏了一个库,所以你可以重命名
我希望有一个更好的解决方案,比如区分标准库和本地文件的#include <foo>
vs#include "foo"
,或者检测我创建的文件何时隐藏在标准库中的lint配置.
我如何在前进的过程中避免这个问题?
phrogz:~/proj/ai$ ls
numbers.py
phrogz:~/proj/ai$ cat numbers.py
import pygame
phrogz:~/proj/ai$ python numbers.py
Traceback (most recent call last):
File "/home/phrogz/proj/ai/numbers.py", line 1, in <module>
import pygame
File "/home/phrogz/.local/lib/python3.11/site-packages/pygame/__init__.py", line 264, in <module>
import pygame.surfarray
File "/home/phrogz/.local/lib/python3.11/site-packages/pygame/surfarray.py", line 47, in <module>
import numpy
File "/home/phrogz/.local/lib/python3.11/site-packages/numpy/__init__.py", line 130, in <module>
from numpy.__config__ import show as show_config
File "/home/phrogz/.local/lib/python3.11/site-packages/numpy/__config__.py", line 4, in <module>
from numpy.core._multiarray_umath import (
File "/home/phrogz/.local/lib/python3.11/site-packages/numpy/core/__init__.py", line 72, in <module>
from . import numerictypes as nt
File "/home/phrogz/.local/lib/python3.11/site-packages/numpy/core/numerictypes.py", line 595, in <module>
_register_types()
File "/home/phrogz/.local/lib/python3.11/site-packages/numpy/core/numerictypes.py", line 590, in _register_types
numbers.Integral.register(integer)
^^^^^^^^^^^^^^^^
AttributeError: module 'numbers' has no attribute 'Integral'