0

I try to pass the two strings Start='2018-07-01'and End='2019-07-01' from one script mainfile.py to another script plot.py. Here, these dates are used for giving titles to the plots.

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 what I read in other threads, I tried to import these two variables with the following:

from mainfile import Start, End

However, as I do this, the following error occurs cannot import name 'Choose_Weatherstation' from 'choose_weatherstation' (L:\Improved Code\venv\choose_weatherstation.py) This refers to the first line of script mainfile.py, where I import Choose_Weatherstation from choose_weatherstation.py.

When I do NOT try to import Start and End, no errors are found.

Can anyone explain me what I am doing wrong here?

2
  • Can you post the both scripts here, it is not clear from above context Commented Nov 25, 2019 at 15:18
  • How looks your choose_weatherstation module? Commented Nov 25, 2019 at 15:46

1 Answer 1

1

The correct syntax is:

from mainfile import Start, End

So, literally, "from mainfile.py import the "Start" and "End" object.
Looks like you have correct imports.

You do not have to call:

Start=mainfile.Start
End=mainfile.End

if you have imported variables straight from the module. Just pass it as you imported it:

start, end = Start, End

Errors you may have:

  • You probably have the wrong path to the imported module (enter the full path if you do not work for example in the package),
  • You are importing the wrong object name (you should probably write the function name with a lowercase letter). Based on your comments.

You can also pay attention to these things:

See Google Python Style Guide

Examples of naming objects:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Karol, thanks for your effort! The weird thing is that without the "mainfile import, the whole code runs fine and at the moment I add this very line, the "Choose_Weatherstation" is no longer found. I worked around the problem now by passing the parameters via the functions. The remark about writestyle is a good one! Thanks for that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.