【发布时间】:2020-10-19 09:04:40
【问题描述】:
我在带有 Python 3.8 的 Windows 10 上使用 PyCharm。我试图让 Python 导入工作从父目录使用:
import sys
sys.path.append("..") # Adds higher directory to python modules path.
from trace_recursion import trace
效果很好,但后来我注意到注释掉 sys.path.append("..") # Adds higher directory to python modules path. 也有效。
我从终端检查了这个,注释掉 sys.path.append("..") 破坏了程序,正如预期的那样。
然后我对print(sys.path) 的输出进行了文本比较,PyCharm 的输出包括当前文件和项目目录,而从终端运行时的输出不包括。
请问这是怎么回事? PyCharm 是否缓存了我第一次运行 sys.path.append("..") 时的扩展路径,还是 PyCharm 的某种默认行为添加它?还是别的什么?
【问题讨论】: