Ask coding questions

← Back to all posts
How do I run another file in my repl?
Imzecorella (2)

So I have my main.py as well as my combat.py
How do I make combat.py run?
I tried using import but that only activates once and if you try to import again it doesnt run the code in said file. Any help?

Answered by Saml2020 (58) [earned 5 cycles]
View Answer
Comments
hotnewtop
Saml2020 (58)

If you want combat.py to run instead of main.py. Use .replit. If you want both to run, import os then os.system("python combat.py").

Imzecorella (2)

Ok, but how do I activate the .replit in the middle of the code?
It goes something like this for me:
Code
if (you run into an enemy):
Code
(run combat.py)

I tried putting:
import os
os.system("python combat.py)

into the place where I need it to be, but nothing happens

Saml2020 (58)

@Imzecorella
You won't be able to, if you would like to run another file in one file use the os libary to run shell commands.

import os
os.system("python combat.py")
Imzecorella (2)

@Saml2020 And I assume shell commands have to be executed in the shell and thus are not doable in the actual .py scritps?

Saml2020 (58)

@Imzecorella
Not always the case, you can execute shell commands via python with the os libary. Just try it and see.

Imzecorella (2)

@Saml2020 I put "import os" "os.system("python combat.py") where it needs to be and nothing happend

Saml2020 (58)

@Imzecorella
Put something like print("combat.py is running") in combat.py. To test if it works.

Imzecorella (2)

@Saml2020 It is running, but I dont understand why it isnt initiating the rest of the code then

Imzecorella (2)

Oh, now it works. Just simple had to remove one line of code from combat.py, but that line of code is quite important... hmm...

Imzecorella (2)

Ok, added it back and it works. Thanks for everything!