1

Is there a way to make a program which executes other programs? I was creating a project for school then I had an idea of creating a main program which will run different program according to my choice. I am using VS code and I created 2 programs in same folder(Here is the screenshot in case you are confused):

enter image description here

Here as you can see I have 2 programs(one is rockpaper.py and other is pass.py) inside of a folder named (ROCKPAPER) is there a way I can run both of these program by making another program inside of this same folder(ROCKPAPER)???? Is there a module for this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Shardul
  • 190
  • 8

5 Answers5

0

The best practice is to import the two programs in a 3rd one like

import pass
import rockpaper

which will execute those codes.

Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27
0

No module is needed, this is basic functionality and you can simply import your sub-scripts into the main script.

import rockpaper
import pass

to run them, simply write

rockpaper
pass
TheConfax
  • 144
  • 10
0

rock.py

def stone():
    return ('This is rock')

paper.py Note here I have imported rock and assigned rock's result to a variable

  import rock

def papyrus():
    return ("This is paper")

scissors.py Note here, I got rock and paper imported and their return values are being printed from this file.

import rock
import paper

def sharp():
    rocke = rock.stone()
    papery = paper.papyrus()
    print(f"{rocke} {papery} But I am a scissors")


if __name__ == "__main__":
    sharp()

Output:

This is rock This is paper But I am a scissors

Process finished with exit code 0

You should really be studying these basics though.

Anand Gautam
  • 2,018
  • 1
  • 3
  • 8
0

Here's a short "driver" program you can put in the same folder as the others. What it does is create a menu of all the other Python script files it finds in the same folder as itself and asks the user to pick one to run. If the user enters number of one of those listed, it then runs that script and quits (otherwise it repeats the question).

It uses the subprocess module to execute the chosen script.

from pathlib import Path
import subprocess
import sys

filepath = Path(__file__)
current_folder = filepath.parent
this_script = filepath.name

print(f'{this_script=}')
print(f'{current_folder=}')

scripts = sorted(script for script in current_folder.glob('*.py') if script != filepath)
menu = [script.stem for script in scripts]

print('Available programs:')
for i, script in enumerate(menu, start=1):
    print(f'  {i:2d}. {script}')

while True:
    choice = input('Which program would you like to run (or 0 to quit)? ')
    try:
        choice = int(choice)
    except ValueError:
        print("Sorry, did not understand that.")
        continue

    if choice in range(len(menu)+1):
        break
    else:
        print("Sorry, that is not a valid response.")
        continue

if choice:
    print(f'Running program {menu[choice-1]}')
    subprocess.run([sys.executable, f'{scripts[choice-1]}'])
martineau
  • 119,623
  • 25
  • 170
  • 301
0

It's actually pretty simple!

import os
os.system("python anotherProgram.py")  

Essentially, you're giving it a shell command.

SunAwtCanvas
  • 1,261
  • 1
  • 13
  • 38