我尝试将两个字符串Start='2018-07-01'
和End='2019-07-01'
从一个脚本mainfile.py
传递到另一脚本plot.py
。在这里,这些日期用于为地块赋予标题。
mainfile.py:
from choose_weatherstation import Choose_Weatherstation
# from summary import scatter_size
#choose_weatherstation is the function: Input has to be
'Weatherstation',perhour(true/false), weather(false/true)
save(True/false),statistic (true/False)
Start='2018-09-01'
End='2018-12-31'
Choose_Weatherstation('Zeeland',Start,End,True,True,True,True)
plot.py
import matplotlib.pyplot as plt
import seaborn as sns
from mainfile import Start, End
Start=mainfile.Start
End=mainfile.End
def three_plot(condition,plot1,plot2,plot3,filename,Save=False):
...(Not of interest here)
从我在其他线程中阅读的内容,我尝试使用以下内容导入这两个变量:
from mainfile import Start, End
但是,当我这样做时,发生以下错误cannot import name 'Choose_Weatherstation' from 'choose_weatherstation' (L:\Improved Code\venv\choose_weatherstation.py)
这是指脚本mainfile.py
的第一行,我在其中导入了Choose_Weatherstation from choose_weatherstation.py
。
[当我不尝试导入Start
和End
时,没有发现错误。
谁能解释我在这里做错了什么?
from mainfile import Start, End
因此,从字面上说,“从mainfile.py导入”开始“和”结束“对象。看起来您输入正确。
您不必致电:
Start=mainfile.Start
End=mainfile.End
如果您直接从模块导入变量。导入时只需传递它即可:
start, end = Start, End
命名对象的示例:
模块名称,程序包名称,类名称,方法名称,异常名称,函数名称,GLOBAL_CONSTANT_NAME,全局变量名称,实例变量名称,函数参数名称,本地变量名称