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

问题描述 投票:0回答:1

我尝试将两个字符串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时,没有发现错误。

谁能解释我在这里做错了什么?

python import
1个回答
1
投票

正确的语法是:

from mainfile import Start, End

因此,从字面上说,“从mainfile.py导入”开始“和”结束“对象。看起来您输入正确。

您不必致电:

Start=mainfile.Start
End=mainfile.End

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

start, end = Start, End

您可能遇到的错误:

  • 您可能有错误的导入模块路径(例如,如果您在软件包中不工作,请输入完整路径),
  • 您输入了错误的对象名称(您可能应该用小写字母写函数名称)。根据您的评论。

您还可以注意以下事项:

请参见Google Python Style Guide

命名对象的示例

模块名称,程序包名称,类名称,方法名称,异常名称,函数名称,GLOBAL_CONSTANT_NAME,全局变量名称,实例变量名称,函数参数名称,本地变量名称

© www.soinside.com 2019 - 2024. All rights reserved.