Ask coding questions

← Back to all posts
Running a program
TimiA (0)

Hello,
I have created multiply python files however i am only able to run the first program (main.py). Is there any way of specifing the file i want to run everytime i want to run it?
Thanks

Voters
TimiA (0)
Commentshotnewtop
MATTHEWBECHTEL (122)

If he answered then you should Upvote their comment and give them the 5 cycles.
@TimiA

ash15khng (393)

You'll have to move the code in main.py somewhere else first.
Then, add this in main.py:

a = input("Name of file to run (without py extension): ")
__import__(a)

__import__(a) is just a function that imports your input, running your code.

JustARatherRidi (190)

Hey TimiA, while ash15khng is correct, you usually don't have more than one starting point in a project, and that's why it was made this way. In repl.it and in Python projects in general, you have something along the lines of main.py that acts as the starting point of your project. The other files in your project may contain classes, methods, or constants that you can use within your project (using import and/or open).

If you have files that run independently to each other or do different things, it's probably a good idea to make them two separate repls (using the blue 'new repl' button up top) and not two files in the same repl.

However, if your two programs are independent of each other but are related, you might want to store the two repls in one folder. You can do this in your My repls page.

Hope this helped; if something confused you, I'll be more than glad to clarify!