在Juypter Notebook中导入python文件时出现缩进错误

时间:2020-07-23 14:05:05

标签: python-3.x jupyter-notebook

我正在运行下面提到的简单代码,但是得到indentation error。我正在使用Python 3.xJuypter笔记本。帮助将不胜感激。

import os
import sys
sys.path.insert(0, os.path.abspath('C:\dir python util'))

import h
h.my_first_function()

位于驱动器h.py中的文件c:\dir python util中的内容如下:

def my_first_function():
 print ("my first program of python")

我得到的错误:

Traceback (most recent call last):

File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 5, in import h

File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block

1 个答案:

答案 0 :(得分:0)

File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block

错误表明它很简单indentation error。您需要在代码中保留适当的缩进。

缩进后的结果如下:

def my_first_function():
    print ("my first program of python")

因此print语句需要有4个空格而不是一个空格。

如果您还不了解Python的缩进规则,则建议您仔细阅读this文档。