0

I'm trying to import two modules so that they run after each other. Here is my code:

from Game import *
main()
from Typing_Question_Screen import *

When I run my code, the Game module loads first but if I close the file, the Typing_Question_Screen module is loaded too. But once the Game module finishes, nothing loads next.

How can I run these two right after each other so that when the Game file ends, the Typing_Question_Screen loads too? Thank you.

Davina
  • 63
  • 8

2 Answers2

0

The statements in the global scope of imported modules are executed when they are imported. So, you may consider adding function calls or other desired statements in the global scope. Let me give an example:

Game.py:

def main():
    print("game")

main()

Typing_Question_Screen.py:

def test():
    print("tqs")

test()

The importing file:

from Game import *
from Typing_Question_Screen import *

print("import complete")

The output of this file would be:

game
tqs
import complete

Note that you can check whether the module is being executed independently. (Please keep in mind that usually the opposite of this behavior is used, and generally imports are not expected to execute code by themselves.)

For example, the below implementation calls main only if the module is being imported:

def test():
    print("tqs")

if __name__ != __main__:
    test()

__name__, a special variable of the module set by the interpreter, is __main__ when the module is run without being imported.

Also, I would avoid importing with *. Moreover, the imports should be at the top of the importing file, separated from other statements and definitions. See: PEP8 on importing

I think you may modify your code as:

import Game
import Typing_Question_Screen

Game.main()
  • If you are testing using the last piece of code I have wrtitten, you can run a desired function of Typing_Question_Screen in a similar fashion to `Game.main()`. Try adding `Typing_Question_Screen.test()` below `Game.main()` to execute them one after the other. – cildiracagim Feb 20 '21 at 17:51
  • Are you calling the desired function (`main` in our example)? If you have the line `Game.main()` somewhere after importing `Game` and it does not work as intended, please make sure that you have implemented the Game module and the `main` function correctly. If the program gives any errors, please share them as well. – cildiracagim Feb 20 '21 at 20:56
  • I’ve updated my question so hopefully it makes more sense. I’m calling the Game module only. But when I tried just importing it it didn’t work. I asked a question on stack overflow and someone mentioned to add main(). So it works. But I’m asking how to make another module (Typing_Question_Screen) run straight after the Game module ends. Hopefully this makes sense. – Davina Feb 20 '21 at 21:21
  • Normally, if you have a main function for this second module too (named `test` in my example above), and if you are calling it after calling the `main` function of `Game`, and if no modules have code to be executed in their global scopes, then they should be executed one after another, and after the execution of the first function terminates. Are you sure your program and modules satisfy these properties? Maybe your first function does not terminate and the second function cannot start? Unfortunately, I'm having trouble understanding the root of the issue... – cildiracagim Feb 20 '21 at 21:35
  • Let me explain this clearly. I have a module called “Game”. This contains a game which I’m creating and has a lot of functions. I also have another module called “Typing Question Screen”. This contains another part of my game. The aim is to import the “Game” module first so the user can play. And once this ends I want “Typing Question Screen” to run straight after. Then the user plays that part and the program will end anyways. The trouble is creating the link between these different modules. My logic is just to import both of them on a new file and hope it can run in order (Game then Typing) – Davina Feb 20 '21 at 22:57
  • I need help making the modules run in order- right after “Game” ends. I’ve got no clue about the main function because I tried imported “Game” but it didn’t work. Here is the link to why I used main in the first place: https://stackoverflow.com/questions/66284549/why-is-nothing-running-if-i-import-a-module Hopefully you can get why I have main in my code as I was only following the instructions someone answered to this question. – Davina Feb 20 '21 at 22:59
  • I think I understand your problem better now. Though, the explanation in the answer should should have solved you problem. I would make sure that 1) you have implemented "main functions" for both modules and 2) after importing them, you are calling both functions in order. Theoratically this is all you need to make two routines run one after the other. You may also try the batch file approach mentioned in the other answer to run the modules independently. – cildiracagim Feb 21 '21 at 21:16
  • If you are still having trouble, you may consider editing your question to include the code of the modules. I can help if your issue is related to the modules themselves. – cildiracagim Feb 21 '21 at 21:21
0

From my understanding you have two files:

file1.py file2.py

If you want to run file.1py and then run file2.py without having to manually do it, there are multiple ways. I would recommend creating a batch.

So you have your two .py files. You will want to create a new file. Don't worry about the extension. We will take care of that later. Navigate to your terminal at the bottom and type in where python. That is the first thing you will put in the double quotes. The next is where your file1.py file is located. You will do the same for file2.py indicated in picture 5. You can use the pause function to pause the process. So after file1.py and ran you will be promted to start file2.py. If you don't want to be prompted, then just remove the pause. You then need to navigate to your file explore to where the batch file was created. In my case it is here (picture 6). Right click on the batch file and select rename. Add .bat at the end. That will convert it to the batch file. Then when you run it (doubleclick). The two .py files that we set up in the batch will run!

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Binx
  • 382
  • 7
  • 22
  • @Davina If you have any questions or get stuck anywhere, please feel free to let me know. – Binx Feb 19 '21 at 23:24
  • It says: is not recognized as an internal or external command, operable program or batch file. I can't get a batch file like you did. – Davina Feb 20 '21 at 15:04
  • Where is the error located? When you run the batch file? When you create the batch file? – Binx Feb 22 '21 at 15:17
  • So when I type in "where file" where you have "(base)" in picture 4, instead of "(base)" mine says "(venv)". It also doesn't create a batch file like you have in picture 5. – Davina Feb 22 '21 at 20:05
  • Picture four you want to type in `where python` not `where file`. And "venv" should be okay. I sometimes set up my PyCharm to run its interpreter through an anaconda environment. Your interpreter path will just read differently then mine. It could be similar to something like `C:\Users\name\anaconda3\env\virtural_environment\python.exe – Binx Feb 23 '21 at 18:33
  • For the batch file; simply just create a blank text file shown in picture 5. Where ever your files are saved to in your file explore you will see that the it is a txt (I believe). You can see in the last picture the `batch` file icon is a blank white piece of paper. Right click and choose rename. At the end of it type `.bat` and click enter. You should get a window prompting you to either accept the change or not (at least on windows). Accept it and the file icon should update to what looks like two gears. – Binx Feb 23 '21 at 18:38
  • Let me know if you have any other problems. – Binx Feb 23 '21 at 18:39