Skip to content
← Back to Community
Can't seem to run multiple python files in one project
Profile icon
staedtlernorica

I have two .py files in my project. The first file, main.py ran 100% fine. I added a second file, hell.py, which only has print('hello') in it. When I switch the file viewer to hell.py, clicked run, and the console only displays the output of main.py, but does not display 'hello'. Why can't I get 'hello' to show up?

Answered by Geocube101 [earned 5 cycles]
View Answer
Voters
Profile icon
SocratesLawson
Profile icon
AlpanaPanwar
Profile icon
JINHIT
Profile icon
SaurjayanBhatta
Profile icon
DrMineBlox
Profile icon
DakicMihailo
Profile icon
ColtCaraway
Profile icon
IceMagic
Profile icon
Finwadd
Profile icon
Saileshlanka
Comments
hotnewtop
Profile icon
Geocube101

The replit editor only runs the main file. In order to run the second file, you will have to import it import [file_name]. When importing another python file, do not include the .py extension.
Note: The file runs immediately after import meaning that as soon as the file imports, it runs. If you need the file to only run at a specified line, that is where you put the import command.

Profile icon
staedtlernorica

@Geocube101 Do you know if there a reason for this limitation? I don't think I ran into this problem when I last used a standalone IDE.

Profile icon
Geocube101

@sodalover I do not know the reason.

Profile icon
ash15khng

@Geocube101 @sodalover repl.it needs to have a builtin "run point" (I'm not sure what the proper word is) to run your repl, so it always finds main.py and runs it. This is also why you cannot rename main.py

Profile icon
GulliverM

you can also use exec(open("hell.py").read()) (this would be useful if you have a variable named hell, because import would override it, and this also saves some memory space)