Ask coding questions

← Back to all posts
How do you run another file from main.py?
Jalcon (19)

Right now were creating a program called Jalcon Industrial:
https://repl.it/@Jalcon/jalcon-industrial
And there is a /run command which runs other programs on different .py documents, so we were wondering how you can run those files from the main program?

P.S To run the python program you may have to open it in a new tab with this:

Answered by ArchieMaclean (590) [earned 5 cycles]
View Answer
Commentshotnewtop
ArchieMaclean (590)

You can use the import command to import another file. Usually this is used to import functions. Here is an example:

test.py:

def greet():
  print("hello world!")

main.py:

# first way
import test
test.greet()

# second way
from test import *    # import * means import everything
greet()

# third way
from test import greet
greet()

Please upvote if this helps :)

Jalcon (19)

@ArchieMaclean First of all, we upvoted your post :)
And WOW you are speedy! You also provide an answer to ask posts within (most of the time) the first minute!

ArchieMaclean (590)

@ArchieMaclean btw, if you need some code to run when you import it, just put the code in the file (test.py) and it will run when (note that it usually runs at the start of the program (main.py), not when it is imported)