-1

I'm importing a module but when I run it, it says Process finished with exit code 0

My code: from Game import *

I've tried everything that's been suggested and I even looked at answers from stack overflow but nothing is working.

The module works fine when I run it but I have no idea on why it isn't working. If you have any suggestions please let me know. Thank you.

Davina
  • 63
  • 8
  • Does Game module have an entrypoint ? – Lucas Abreu Feb 19 '21 at 20:27
  • Could you explain what you mean? I'm new to python. But I'm using pycharm which I've imported pygame to make this module. – Davina Feb 19 '21 at 20:29
  • Sure thing. If Game is a module than you likely have only functions, constants, etc. So if you do what you wrote, python imports its and returns 0, because it worked. If you want the code to run, you need to apply in the game module if __name__ == '__main__': begin of your code. So that python knows where to start the execution of your code. Read more on __main__ in python to get a better idea – Lucas Abreu Feb 19 '21 at 20:34
  • I actually have: if __name__ == '__main__': main() in my code at the end. Does this still work? – Davina Feb 19 '21 at 20:38
  • Because I can't add this at the start of the code as it doesn't work. – Davina Feb 19 '21 at 20:50
  • Do you have the main() method in `Game` ? – Lucas Abreu Feb 19 '21 at 20:53
  • Yep I do have it – Davina Feb 19 '21 at 20:59
  • Hello @Davina if you want to debug a specific code example edit the question to include it. Otherwise going from the error message alone the duplicate target thread contains a good repository of answers. – bad_coder Feb 19 '21 at 21:29
  • 1
    The "duplicate" of this post has nothing to do with the actual issue here, and doesn't provide a working solution, so I'm voting to reopen. – Random Davis Feb 19 '21 at 21:43
  • @RandomDavis the correct solution in this case is adding the post you linked in the answer to the duplicate targets (it's a list and there can be more than one). That action can be done by a gold badge holder or a mod. Merging the 2 posts is also a possibility but can only be done by a mod. The best solution would have been posting your answer under the canonical post and closing this one. This thread still serves as a useful sign post, however reopening it will at best result in a number of duplicate answers that will tend to replicate answers in the other posts and pollute search results. – bad_coder Feb 20 '21 at 00:52
  • @RandomDavis as you probably know, if this action hadn't been taken now it would be taken eventually when either a Python gold badge user or a mod came across this post. – bad_coder Feb 20 '21 at 00:55

1 Answers1

1

The whole point of the if __name__ == 'main'block is to stop the code inside it from running when importing that script as a module; only the "main" script, the one you're directly executing, will execute the code within that statement. If you just replace that statement with a direct call to main(), then that function will run upon import.

For more info, see: What does if __name__ == "__main__": do?

If you want to execute Game's main from your script, your code would have to look like:

from Game import *
main()
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • How would I directly import another module right after this one? So it runs after the current one ends? I tried it but it doesn't work. – Davina Feb 19 '21 at 21:44
  • Usually separate questions should be posted separately. However I think this is pretty simple. If you import two modules `Game` and `Game1`, each one having a `main`, for example, then you'd have to do `from Game import main` and `from Game1 import main`, then `Game.main()`, followed by `Game1.main()`. – Random Davis Feb 19 '21 at 21:49
  • Hmm not working but thank you for your help. I will ask another question. – Davina Feb 19 '21 at 22:10
  • @Davina yeah sorry I wasn't actually completely sure if that would work. – Random Davis Feb 19 '21 at 22:23