在Juypter Notebook中导入python文件时出现缩进错误 [英] Indentation error while importing python file in Juypter Notebook
问题描述
我正在运行下面提到的简单代码,但得到了indentation error
.我正在使用Python 3.x
和Juypter
笔记本.帮助将不胜感激.
I am running simple code mentioned below, but getting indentation error
. I am using Python 3.x
and Juypter
notebook. Help will be appreciated.
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
中,内容如下:
In file h.py
, which is in drive c:\dir python util
contents are below:
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
推荐答案
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
错误表明它很简单indentation error
.您需要在代码中保留适当的缩进.
Error indicates that it is simple indentation error
. You need to maintain the appropriate indentation in the code.
缩进后的结果如下:
def my_first_function():
print ("my first program of python")
所以print
语句需要有4个空格而不是一个空格.
So print
statement needs to have 4 spaces rather than one.
建议您仔细阅读此文档,以了解有关Python缩进的想法规则,如果您还不知道的话.
Suggest you to go through this document to get idea about Python's indentation rules, if you don't know that already.
这篇关于在Juypter Notebook中导入python文件时出现缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!