你不应该使用的 Python 文件名

日期:
分类: Python

在新 VPS 上运行 Python 程序的时候出现了问题:pdm 无法正常 install 或者设置 config,poetry 可以但是无法正常运行。两者都在获取运行环境的时候发生了同样的错误。以下是一个最小复现例子:

[neko@host temp]$ ls
select.py
[neko@host temp]$ cat select.py
print("hello, world!")
[neko@host temp]$ python
Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
hello, world!
Traceback (most recent call last):
  File "/usr/lib/python3.10/subprocess.py", line 69, in <module>
    import msvcrt
ModuleNotFoundError: No module named 'msvcrt'
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/platform.py", line 119, in <module>
    import subprocess
  File "/usr/lib/python3.10/subprocess.py", line 76, in <module>
    import selectors
  File "/usr/lib/python3.10/selectors.py", line 291, in <module>
    class SelectSelector(_BaseSelectorImpl):
  File "/usr/lib/python3.10/selectors.py", line 318, in SelectSelector
    _select = select.select
AttributeError: module 'select' has no attribute 'select'
>>>

我一 Linux 怎么会需要 msvcrt一开始以为是 Arch 仓库里的 Python 有问题,或者是 pip 安装的时候在 root 用户操作而破坏了系统,但是重装了几次全部依赖也没有解决。排查许久后,发现只有在我的代码文件夹里进入 REPL import platform 会报错。然后发现项目里用的一个模块的名称叫 select.py。是的,导入 Python 内置的 标准库 的时候,标准库内部的 import 也是在当前工作目录下进行的(!)。

最末的结论是,你不应该使用以下名称作为 Python 代码的文件/模块名:

abc, aifc, argparse, array, ast, asyncio, atexit, audioop, base64, bdb, binascii, bisect, builtins, bz2, calendar, cgi, cgitb, chunk, cmath, cmd, code, codecs, codeop, collections, colorsys, compileall, concurrent, configparser, contextlib, contextvars, copy, copyreg, csv, ctypes, dataclasses, datetime, dbm, decimal, difflib, dis, doctest, email, ensurepip, enum, errno, faulthandler, filecmp, fileinput, fnmatch, fractions, ftplib, functools, gc, getopt, getpass, gettext, glob, graphlib, gzip, hashlib, heapq, hmac, html, http, imaplib, imghdr, imp, importlib, inspect, io, ipaddress, itertools, json, keyword, linecache, locale, logging, lzma, mailbox, mailcap, marshal, math, mimetypes, mmap, modulefinder, msilib, msvcrt, multiprocessing, netrc, nntplib, numbers, operator, optparse, os, pathlib, pdb, pickle, pickletools, pipes, pkgutil, platform, plistlib, poplib, pprint, profile, py_compile, pyclbr, pydoc, pyexpat, queue, quopri, random, re, reprlib, rlcompleter, runpy, sched, secrets, select, selectors, shelve, shlex, shutil, signal, site, smtplib, sndhdr, socket, socketserver, sqlite3, ssl, stat, statistics, string, stringprep, struct, subprocess, sunau, symtable, sys, sysconfig, tabnanny, tarfile, telnetlib, tempfile, test, textwrap, threading, time, timeit, tkinter, token, tokenize, tomllib, trace, traceback, tracemalloc, turtle, types, typing, unicodedata, unittest, urllib, uu, uuid, venv, warnings, wave, weakref, webbrowser, winreg, winsound, wsgiref, xdrlib, xml, xmlrpc, zipapp, zipfile, zipimport, zlib, zoneinfo 🐍

本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议CC BY-NC 4.0 International) 进行许可。阁下可自由地共享(复制、发行)和演绎(修改、转换或二次创作)本作品,唯须遵守许可协议条款。

评论

  • est  

    好像默认敲python会自动把当前路径加入PYTHONPATH。你打一个 python -i 就木有问题了
  • yzqzss  

    py3.13 就不需要这篇文章了瞄,AttributeError 会提示重名了。 AttributeError: module 'xxxxx' has no attribute 'yyyyy' (consider renaming 'xxxxx.py' since it has the same name as the standard library module named 'xxxxx' and the import system gives it precedence)
评论将在审核后显示,阁下可以在本博客的 Github 仓库的 拉取请求列表 中查看。
本表单无 JavaScript,请勿重复提交。

本站不支持 Dark Reader 的暗色模式,请对本站关闭后再访问。
(亮色模式的对比度、亮度等选项不受影响)


This site does not support dark mode by Dark Reader, please turn it off before visiting.
(Contrast, brightness, etc. of light mode are not affected)