你不应该使用的 Python 文件名

日期:
分类: Python 2

在新 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 🐍

版权许可

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

评论

评论将在审核后显示,阁下可以在本博客的 Github 仓库的 拉取请求列表 中查看。提交成功后会自动跳转。

本站不支持 Dark Reader 的暗色模式,请对本站关闭后再访问,亮色模式的对比度、亮度等选项不受影响。部分页面右上角提供暗色模式切换按钮,如果你没看到,说明你的浏览器尚不支持此特性。本提示不依赖于 JavaScript,你可自行查找其他用户在本站发表的关于如何关闭此提示的评论。