0

Dear StackOverflow community,

i have two py-files (example1.py and example2.py), plan to run example1.py first, example2.py second and want to import a list from the first to the second one. Indeed I tried import example1 and wrote then example1.list1 in example2.py, which is used to get the list list1 from the example1.py, but it actually makes the example1.py file run in example2.py. So, my question is: How can I import the list variable directly to the second py-file without starting the first py-file again?

Ozan
  • 27
  • 5
  • .py files are for Python code, not data. The usual ways to transfer data from one piece of code to another (whatever file the code is in) is by returning the data from a function call or writing it to a file and the reading back in the other code. "importing" like code makes no sense. – martineau Dec 17 '21 at 13:57
  • Does this answer your question? [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – Matthias Dec 17 '21 at 13:57
  • @martineau There are things like `settings.py` in Django. Just data, no code (at least not much). – Matthias Dec 17 '21 at 13:59
  • @Matthias: I was simplifying things for the OP (and seriously doubt that is what they are taking about). – martineau Dec 17 '21 at 14:09

1 Answers1

1

when you import any file, it must be run so that the list is created by the interpreter, if there is any code in example1 that you don't want to run, you should wrap it in:

if __name__ == '__main__':

condition in order to prevent it from running unless you are running that file as your main file, (and not as an import).

so your example1.py, should look like this:

list1 = [1,2,3] # for example
if __name__ == '__main__':
    # some code you don't want to run when you import this file,
    # but want to run when you run this file separately.
Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • Thank you for your answer! The solution is useful indeed, but unfortunately not for my case exactly, because if I prevent the code, which I don't want to run, I cannot get the list I wanted. Actually I would like to get the list from the module, where variables are temporarily saved after running example1, to example2 – Ozan Dec 17 '21 at 14:18
  • i am not sure what you mean by temporary, if your variables are local to a function, you can expose them to the module using [global](https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python) keyword, so they will stick around after the function ends, then import them from the other file, however as noted, both files will be run when your code runs. – Ahmed AEK Dec 17 '21 at 14:21
  • So, my second question: In which module are variables temporarily saved from a py-file? How to get variable from Variable Explorer in Spyder? Is there actually a specific module or package of the explorer? – Ozan Dec 17 '21 at 14:22
  • when you import a module, all variables inside the module are saved in a variable with the module name, so example1.list1 should work, however if you are running your code in spyder using the 'run file' or 'run code block' buttons, the code in that file is executed inside your interpreter __main__ module, therefore you should find the data in list1, and not stored inside any variable, since they are in your global namespace. – Ahmed AEK Dec 17 '21 at 14:25
  • Ahmed: I my opinion you should not be encouraging the use of global variables (which are evil). @Ozan: why did you accept this answer if it doesn't doesn't do what you wanted? – martineau Dec 17 '21 at 14:31
  • because the answer is true in general and useful in some aspects even if not so for my case – Ozan Dec 17 '21 at 14:37
  • Well, I undo it because the answer is not directly to do with my main question. Thanks for your advice, martineau! – Ozan Dec 17 '21 at 14:40
  • 1
    @Ozan, i recommend that you edit the question to be your case, since your case is clearly not mentioned in your question. – Ahmed AEK Dec 17 '21 at 14:43