Python Forum
How can I import a variable from another script without executing it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I import a variable from another script without executing it
#1
Question 
I have a little beginner issue doing imports between scripts, maybe someone can help? Blush

I wrote 2 Modules:

- B01_Import_CSV_V1.py

In the import script, i did load a .csv and saved it as a pandas dataframe (named "df").
#if __name__=="__main__":

import pandas as pd
global df
global df2


   

    
#%% load file via static path

if True:

   
    
    
    tester=r'C:\local_calc\Python_DemoFiles\20220219_70788_406_02_C10_275C_Ges.csv'
    df=pd.read_csv(tester, sep=';',decimal=",", skipfooter=1300, engine='python')
    
    
    
    
    print ('FINISHED loading file        ' + tester)
- plot.py

The plot script should process this data.
#%% Imports
import matplotlib.pyplot as plt
import B01_Import_CSV_V1       #<- this works
#from B01_Import_CSV_V1 import df    #<- this does not work







#%%% plot
fig=plt.figure(figsize=(15,8))
ax=fig.add_subplot(111)
ax2=ax.twinx()
ax3=ax.twinx()
ax.plot( df['70788.1.E602000_W1:6'], c = 'b')
My question:

If I integrate the code of Import-Script in a main function (if __name__=="__main__": ), i learned that this code won´t be executed anymore while import. The plot script won´t work then:

Error:
File "U:\031_Python\030_eigene_Scripte\D01_Plot_ConditioningData_V1.py", line 17, in <module> from B01_Import_CSV_V1 import df ImportError: cannot import name 'df' from 'B01_Import_CSV_V1' (U:\031_Python\030_eigene_Scripte\B01_Import_CSV_V1.py)
-> Does this mean, I always have to execute the csv-Import within the plot script?

Is there any way to execute scripts separately and then use the variables from each other?
There´s actually no advantage doing it separately YET, but there will be ;)

Thanks for helping! :)
Reply
#2
The solution is to write functions that you execute only when they are needed
# B01_Import_CSV_V1.py
import pandas as pd
#%% load file via static path
 
def load_df():
    tester=r'C:\local_calc\Python_DemoFiles\20220219_70788_406_02_C10_275C_Ges.csv'
    return pd.read_csv(tester, sep=';',decimal=",", skipfooter=1300, engine='python')
then
# plot.py
from B01_Import_CSV_V1 import load_df

df = load_df()
...
Reply
#3
Why are there two files? There isn't much code and I am not seeing any attempt at reuse. Why have the complication of having to maintain two files instead of one?
Reply
#4
(May-03-2022, 02:15 PM)deanhystad Wrote: Why are there two files? There isn't much code and I am not seeing any attempt at reuse. Why have the complication of having to maintain two files instead of one?

Hi deanhystad, this code is just the start. it will get bigger and bigger, it definetley makes sense to split things up here in the beginnning!
Reply
#5
(May-03-2022, 12:20 PM)Gribouillis Wrote: The solution is to write functions that you execute only when they are needed
# B01_Import_CSV_V1.py
import pandas as pd
#%% load file via static path
 
def load_df():
    tester=r'C:\local_calc\Python_DemoFiles\20220219_70788_406_02_C10_275C_Ges.csv'
    return pd.read_csv(tester, sep=';',decimal=",", skipfooter=1300, engine='python')
then
# plot.py
from B01_Import_CSV_V1 import load_df

df = load_df()
...

Hi Gribouillis, thanks for your idea - looks good! Smile
But I´m still wondering if it´s possible to not call a function, but only use the variable which was already loaded in the workspace then.

Example:
Load via Script
Process data with other script
Do some other stuff
Process data with one more script (without reloading the .csv, because this is slow)
Reply
#6
Hi Gribouillis, thanks for your idea - looks good! Smile
But I´m still wondering if it´s possible to not call a function, but only use the variable which was already loaded in the workspace then.

Example:
Load via Script
Process data with other script
Do some other stuff
Process data with one more script (without reloading the .csv, because this is slow)
Reply
#7
(May-03-2022, 08:57 AM)ThomasFab Wrote: If I integrate the code of Import-Script in a main function (if __name__=="__main__": ), i learned that this code won´t be executed anymore while import. The plot script won´t work then:

I'm not sure I understand what you want here. Putting code in a if __name__=="__main__": section is what you do when you don't want it to be run on import.

If you want it to be run on import, don't put it in such a section. Is that possible?
Gribouillis likes this post
Reply
#8
Also, I don't think I understand what part of your import is "not working" in your first attempt. Can you make a reproducible example that shows the error? When I do something similar, it seems to import fine. I'm not sure how mine differs from your attempt.

b01_import.py
import pandas as pd
global df

if True:
    df = pd.DataFrame([3, 2, 1])
plot.py
from b01_import import df

print(df)
Output:
0 0 3 1 2 2 1
Reply
#9
Question 
Quote:I'm not sure I understand what you want here. Putting code in a if __name__=="__main__": section is what you do when you don't want it to be run on import.

If you want it to be run on import, don't put it in such a section. Is that possible?
It is "possible", but I´d like to avoid run the script while import again, to save time/performance. This stuff I`m working on will get quite big the next months..



My prefered way would be:

Load via ImportScript
Process data with other script
(do some other stuff)
Process imported data with a 3rd script (without reloading the .csv, because this is slow)





I tried another example:

example_import_csv
import pandas as pd
global df



if __name__=="__main__":
    df = pd.DataFrame([3, 2, 1])

example_plot


from example_import_csv import df
    
    


df2 = df   #generate a second dataFrame 
it works perfectly, but only if I do NOT use if __name__=="__main__":


If I do, this error occurrs:
Error:
ImportError: cannot import name 'df' from 'example_import_csv' (U:\031_Python\030_eigene_Scripte\example_import_csv.py)
So, to make my question more precise: Is there a way to NOT let "example_import_csv" run during the import at example_plot (e.g. with if __name__=="__main__":), AND still use the variable df?



Thank you for helping!! Angel
Reply
#10
-edited- see other post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with threading and reading variable from a different script Lembas 14 3,075 Apr-26-2023, 11:21 PM
Last Post: Lembas
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Problem executing a script on a remote host tester_V 3 2,483 Sep-26-2021, 04:25 AM
Last Post: tester_V
  Using import in python script offline - HELP!! blackhatter1001001 1 2,267 Jul-25-2021, 05:57 AM
Last Post: ndc85430
  Import output from python script to another varietyjones 1 1,917 Oct-12-2020, 09:07 PM
Last Post: bowlofred
  Use dynamic variable from parallel running python script Sicksym 0 1,856 May-15-2020, 02:52 PM
Last Post: Sicksym
  [split] script: remove all "carriage return" from my json variable pete 2 2,814 May-05-2020, 03:22 PM
Last Post: deanhystad
  Using variable from a class in another .py script keegan_010 4 3,001 Mar-25-2020, 07:47 AM
Last Post: keegan_010
  Import and variable level tazalapizza 1 1,756 Mar-16-2019, 07:47 PM
Last Post: Larz60+
  script: remove all "carriage return" from my json variable mfran2002 4 11,229 Feb-20-2019, 05:07 AM
Last Post: mfran2002

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020