Skip to content
← Back to Community
How do you run another file from main.py?
Profile icon
Jalcon

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:

This file cannot be displayed: https://storage.googleapis.com/replit/images/1554138211728_73b89674399190c5f3245e2092295fdb.pn

Answered by ArchieMaclean [earned 5 cycles]
View Answer
Voters
Profile icon
RowanJuhl
Profile icon
cohbev
Profile icon
AlenZokovic
Profile icon
FlyingShadows
Profile icon
loganv22
Profile icon
DaHud1001
Profile icon
youknowhisyam
Profile icon
RoyMuhaimin
Profile icon
Yeetboi287
Profile icon
Mahieedaboss
Comments
hotnewtop
Profile icon
ArchieMaclean

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 :)

Profile icon
Jalcon

@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!

Profile icon
ArchieMaclean

@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)

Profile icon
ArchieMaclean
Profile icon
Jalcon

@ArchieMaclean It works!

Profile icon
marky0marky

HELP! I cannot figure out how to do this when the file is inside a folder? Every path expression I have tried to identify the location of the file to import does not work?

Profile icon
JensenAndre1

Hi did you ever figure this out? @marky0marky

Profile icon
ArchieMaclean

@JensenAndre1 @marky0marky
Hi, sorry I'm a bit late, but you can but a period between like this:

from myfolder.myfile import f

where you obviously replace myfolder and myfile and f with the relevant information.

Profile icon
williamldn

In the .replit file add:
language = "bash"
run = "python3 <filename.py>"

Profile icon
coynerm

@williamldn Hi-I'm trying to run .py files from the main that are located in folders. The import [filename] works only for files not in a folder. I tried using your code segment but it would not run a file in a folder. Do I need to include the path in the run = "python3<./Unit1/practice.py>"? This is my config:

Screen Shot 2020-08-26 at 11.07.13 AM

Profile icon
mangboi

cool