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
#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


Messages In This Thread
RE: How can I import a variable from another script without executing it - by ThomasFab - May-04-2022, 09:06 AM

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