Run file other than main.py
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?
Thanks everyone for your reply. Really appreciate it.
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.
(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.
@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.
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 :)
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.