[SOLVED] how to run a python file in a python program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

[SOLVED] how to run a python file in a python program?

the code will be something like x = 1 while x < 10: if x == 5: #runfile or something like right here print("file ran") x += 1

10th Oct 2018, 12:03 AM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
22 Answers
17th Oct 2018, 6:58 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 10
The simplest way: Put all the code of the python file in a main() function. Import it as a module (e.g. import snake) Run the main function (e.g. snake.main() ) Another way is using command line arguments but I feel like it's more for geeks.
10th Oct 2018, 2:28 AM
DAB
DAB - avatar
+ 5
LONGTIEšŸ‘” of course you can if your test.py has worked() and failed() inside, you can use them from the module "test"
17th Oct 2018, 7:38 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
import os os.system("python {.py-file}") # or import subprocess subprocess.run("python {.py-file}")
10th Oct 2018, 4:32 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
LONGTIEšŸ‘” if __name__ == "__main__": main()
17th Oct 2018, 6:36 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
LONGTIEšŸ‘” of course with quotes, it's written as a text file and saved as a py file, which can be a module that you can importšŸ˜€
17th Oct 2018, 7:05 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
LONGTIEšŸ‘” oh, if you mean the module file, yesšŸ˜€
17th Oct 2018, 7:09 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
LONGTIEšŸ‘” what do you mean?šŸ¤”
17th Oct 2018, 7:22 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 2
Flandre Scarlet like this import test x = 1 y = 0 while x < 10: if x == 5: test.worked() print("file ran") y = 1 if x == 6 and y == 0: test.failed() break x += 1 of course in this code failed would never be called bit it's just a example
17th Oct 2018, 7:27 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
+ 1
playing I wanna learn how to write a script
10th Oct 2018, 6:59 PM
Kelvin
+ 1
anyone can teach npw
10th Oct 2018, 7:00 PM
Kelvin
+ 1
Kirk Schafer all the codes and treads you posted are about writing and running a python code in a code. not really what i asked about, but interesting none the less.
17th Oct 2018, 6:38 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
+ 1
Flandre Scarlet so like....? if __name__ == "__main__": main(): print("hello world") then import test_main.py x = 1 if x == 1: test_main.main() the output would be hello world
17th Oct 2018, 6:41 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
+ 1
LONGTIEšŸ‘” I admit to being a little confused then. As far as I know we can't run another Python file here without creating something to run first and then pretending the file already existed. (I don't mind deleting that post as too much content; seems there's no need for it)
17th Oct 2018, 7:11 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Kirk Schafer its ok, you can keep it there. I might need to refer to it.
17th Oct 2018, 7:12 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
+ 1
Flandre Scarlet cool, thanks again šŸ‘āœŒ
17th Oct 2018, 7:40 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
0
save and open it in pythonshell .to run it use f5 key
14th Oct 2018, 8:38 AM
ARUNACHALAM.K
0
į Œ į Œ į Œį ŒšŸ į Œ į Œ how would i make a main() function? would i use a class or something?
17th Oct 2018, 6:35 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar
0
Flandre Scarlet but with out the quotes, right?
17th Oct 2018, 7:03 PM
LONGTIEšŸ‘”
LONGTIEšŸ‘” - avatar