solution for python can not import local module
这次遇到的问题是sys.path的输出不包含'',导致无法import当前文件和文件夹
When no ._pth
file is found, this is how sys.path
is populated on Windows:
- An empty entry is added at the start, which corresponds to the current directory.
- If the environment variable
PYTHONPATH
exists, as described in Environment variables, its entries are added next. Note that on Windows, paths in this variable must be separated by semicolons, to distinguish them from the colon used in drive identifiers (C:\
etc.). - Additional “application paths” can be added in the registry as subkeys of
\SOFTWARE\Python\PythonCore{version}\PythonPath
under both theHKEY_CURRENT_USER
andHKEY_LOCAL_MACHINE
hives. Subkeys which have semicolon-delimited path strings as their default value will cause each path to be added tosys.path
. (Note that all known installers only use HKLM, so HKCU is typically empty.) - If the environment variable
PYTHONHOME
is set, it is assumed as “Python Home”. Otherwise, the path of the main Python executable is used to locate a “landmark file” (eitherLib\os.py
orpythonXY.zip
) to deduce the “Python Home”. If a Python home is found, the relevant sub-directories added tosys.path
(Lib
,plat-win
, etc) are based on that folder. Otherwise, the core Python path is constructed from the PythonPath stored in the registry. - If the Python Home cannot be located, no
PYTHONPATH
is specified in the environment, and no registry entries can be found, a default path with relative entries is used (e.g..\Lib;.\plat-win
, etc).
解决方案删除._pth文件,解决这个bug的经验教训就是一定要一步一步跟踪到问题原因,论英文文档的重要性