2

所以我在学习使用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控制台,它按预期运行。

我想知道是什么原因?

4

1 回答 1

1

这是因为 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 成为可能

于 2014-05-16T18:24:34.527 回答