将变量传递给项目的另一个脚本时出错

2024-05-31 23:22:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将两个字符串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

当我不尝试导入StartEnd时,没有发现错误

有人能解释一下我做错了什么吗


Tags: frompyimport脚本falsetrueplotstart
1条回答
网友
1楼 · 发布于 2024-05-31 23:22:14

正确的语法是:

from mainfile import Start, End

因此,从字面上看,“from mainfile.py”导入“Start”和“End”对象。
看起来你有正确的导入

您不必拨打:

Start=mainfile.Start
End=mainfile.End

如果直接从模块导入变量。只需在导入时传递它:

start, end = Start, End

可能出现的错误:

  • 可能是导入模块的路径错误(如果不在包中工作,请输入完整路径)
  • 您导入了错误的对象名(您可能应该用小写字母编写函数名)。根据你的意见

您也可以注意以下事项:

Google Python Style Guide

命名对象的示例

module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name

相关问题 更多 >