0

I'm trying to import the User class from a file called gui.py into another called snake.py. I want to import the class so that I can use ,a method within the class and an instance of the class called current_user. But when I do:

from gui import User

It imports everything from gui.py. Does anyone know where I've gone wrong and what I can do to fix this? I'm new to working with multiple files in Python and this is quite confusing to me. The files for this are available at: https://github.com/VladRadoi08/Snake-LoginUI

Thanks!

  • Does this answer your question? [Why is Python running my module when I import it, and how do I stop it?](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – Macattack Oct 27 '20 at 17:27
  • So doing the if __name__ = "__main__" should stop running the gui.py code and only import the class? Did I understand that okay? –  Oct 27 '20 at 17:32
  • yes, put a `print` inside and outside the if if it helps you understand – Macattack Oct 27 '20 at 17:38

1 Answers1

0

Anything in the file you import will be run. To prevent this, try putting all the code you don't want to run within this conditional:

if __name__ == "__main__":

That will ensure it only runs if you actually run the python file instead of importing it.

user3150635
  • 509
  • 2
  • 9
  • 26