在 SublimeREPL 中导入第 3 方模块
-
23-12-2019 - |
题
所以我在学习使用SublimeREPL时,遇到了一个问题。
我有一个 main.py
文件,并在同一文件夹中 timer.py
. 。我写的 import
中的声明 main.py
:
import timer
那么如果我打开
1)SublimeREPL --> Python --> Python--IPython,并将代码传输到InteractiveConsole,出现错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 1, in <module>
ImportError: No module named timer
2)SublimeREPL --> Python --> Python,并将代码传输到REPL控制台,它按预期运行。
请问这是什么原因呢?
解决方案
这是因为 sys.path 不包含给定的目录。您可以通过下面的代码进行编辑
import os
import sys
sys.path.append(os.getcwd())
# os.getcwd() is the current directory, make sure it's the right one.
这将使导入timer.py成为可能
不隶属于 StackOverflow