Reputation: 27
I'm new to python, I have three python script pyapp_1.py , pyapp_2.py ,pyapp_3.py in same dir . I want to make a file like run.py where I take input from user like this:
press 1 to run pyapp_1.py
press 2 to run pyapp_2.py
press 3 to run pyapp_3.py
if user press 1 only pyapp_1.py should run .
how can i do this, Thanks.
Upvotes: 2
Views: 122
Reputation: 36
For running a python script example:
import runpy
runpy.run_path(path_name='pyapp_x.py')
If you use Python 3.10 or higher:
x=int(input("Please insert a number bewtween 1 and 3: "))
match x:
case 1:
runpy.run_path(path_name='pyapp_1.py')
case 2:
runpy.run_path(path_name='pyapp_2.py')
case 3:
runpy.run_path(path_name='pyapp_3.py')
case _:
print(f'Error: {x} is not between 1 and 3')
Upvotes: 1
Reputation: 29
Put you code in function, import the code into a new file like main.py
main.py
import file1.py
import file2.py
import file3.py
while true:
x=input()
if (x == 1):
file1()
if (x == 2):
file2()
if (x == 3):
file3()
Upvotes: 2
Reputation:
Try this:
x=int(input("press 1 to run pyapp_1.py\npress 2 to run pyapp_2.py\npress 3 to run pyapp_3.py"))
if x==1:
import pyapp_1
elif x==2:
import pyapp_2
elif x==3:
import pyapp_3
Upvotes: 3