[英]Importing a function from another script
I experience 'utf-8' codec can't decode byte 0x98 in position 16: invalid start byte
error while I was trying to import a function in the following way :我遇到'utf-8' codec can't decode byte 0x98 in position 16: invalid start byte
error while 我试图以下列方式导入函数:
from Tutorials.Functions.Files import path_to_project_root
After some investigations, I found the problem was due to some additional calls on the path_to_project_root
function in Tutorials.Functions.Files
itself.经过一番调查,我发现问题是由于对Tutorials.Functions.Files
本身中的path_to_project_root
函数进行了一些额外调用。 In other words, my Tutorials.Functions.Files
looks like this:换句话说,我的Tutorials.Functions.Files
看起来像这样:
def path_to_project_root(projectName)
# The function body
return smt
path_to_project_root("myProject")
I realized that having a call to path_to_project_root("myProject")
while I import it in another script is the problem, and just by commenting it out everything worked well.我意识到在我将它导入另一个脚本时调用path_to_project_root("myProject")
是问题所在,只需将其注释掉,一切都运行良好。 I am seeking the rationale behind this issue.我正在寻找这个问题背后的理由。
This is how Python works, python is a script language, everything you want the interpreter know must be interpreted by interpreter first.这就是 Python 的工作原理,python 是一种脚本语言,你想让解释器知道的一切都必须先被解释器解释。
In this case, you import some function from Files
, interpreter must read and execute it.在这种情况下,您从Files
导入一些函数,解释器必须读取并执行它。
Just think what if you do some import in path_to_project_root
function, without run all statements in the file, the module you import may not work fine.试想如果你在path_to_project_root
函数中做一些导入,没有运行文件中的所有语句,你导入的模块可能无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.