Skip to content
← Back to Community
How to run files that are not named main.py?
Profile icon
KarthikeyanM2

By default, repl runs files that are main.py but If I want to run someother file. How can I run it?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
thomasjohns1
Profile icon
SilentFoxy78
Profile icon
JohnBradwell1
Profile icon
ronaldlin0622
Profile icon
KarthikeyanM2
Comments
hotnewtop
Profile icon
Coder100

There are two ways to do this.

1. Creating a .replit file

Creating a .replit file changes what file is being run.

run="python <file>.py"

Change <file> to your other file. Maybe dummy?

2. Importing files

Importing files will allow you to run both files one after the other.

import dummy # ... your main.py code was here ...
Profile icon
MicroSoftHelp

Create a .replit file, and type "run = "main.py examplefile.py"
replace example file with whatever file you are using

Profile icon
OneNose

You can use os.

> from os import system > system("python dummy.py")

Run that in terminal.

Profile icon
OneNose

You can also import the module and then run it, but only after you ran main.py once.

> import dummy

It might be a better solution than the above.

Profile icon
OneNose

There is also a way to configure the run button, but that will change the whole program and is only good if you want to rename main.py but not if you want to test a specific module.
@OperationDemon
explains this.

Profile icon
PabloJuan

On the repl.it

make a file with name ".replit", and type:

language="python" run="python filename.py"

Importing the file

you can do things like:

#example.py def main(): print('xD') return 123
from example import main #You can import the file and use he functions main() #write 'xD' in the terminal x = main() print(x) #write 123 in the terminal
Profile icon
KarthikeyanM2

Thanks for all the answers.

Profile icon
OlauPla

Hello I have come to help!!

what you can do is put that file in a function and call it in main, I would say that is the easy whay.

To call a function in another file you have to do this in your main file or where you want to call it:

from example import *

example is the file you want to import where you have stored your function!

Hope it helped!

Profile icon
RYANTADIPARTHI

You will have to import that file into the main file, and it will run.

# different file def function(): print("hi") function()
# main.py from other file import function

Now run it, and it will work

Or, you can import that file.

# other file def function(): print("hi") function()
# main.py import filename

Any of those should work