Skip to content
← Back to Community
Run file other than main.py
Profile icon
HemaShah

I have created a file with another name. When I run, it does not run. Do I have to do any change in the setting?

Voters
Profile icon
CoderKid365
Profile icon
HemaShah
Comments
hotnewtop
Profile icon
SixBeeps

By default, main.py is the file that gets run (so that you can have projects split up into multiple files, but it's still only one project)

If you want to run other files, either use the python command in the command window or use a .replit file.

Profile icon
HemaShah

Thanks everyone for your reply. Really appreciate it.

Profile icon
Whippingdot

Oh, I had the same question. There are 2 ways to do this.

Either type python3 and the file name in the place where main.py runs.

image

(Or)

Type 'import os' in the main.py file. Then, you can type code in main.py, if you want, and at the end of the file you can type 'os.system("python3 filename.py")'. Then run main.py. The filename.py file will run after all the code in main.py.

image

Profile icon
HemaShah

@Whippingdot , Thanks for your response. I found the way similar to your suggestion.
Type import [file name]. Run main.py. Make sure your file name has an extension .py .
Code will run wherever you have your import syntax.

Profile icon
FishballNooodle

To do this, you need to either comment out the main code or if you want to run that file with the main code just leave it.

At the start of main.py, add this to import the seconde py file.
from <filename> import <filename>

Note: filename should not include .py
For Example, my file name is authorize.py
I would do: from authorize import authorize

Next is running it. To run it you can call it like a function.
So in my case, that would be...
authorize()
Do note that you can change how you call it,aka alias,by adding an extra line while importing it.
from authorize import authorize as auth
As you can see, I used the as keyword to make an alternative of running that file.
Now, it would be auth().
Hope I have helped you :)