Hi, I’m learning Python…I can get programs to run fine within the main.py file…but how do I launch code within another Python file within the Repl?
Here is what I have tried:
Then created another Python file called:
Inside that I have a simple print command:
print("I am now running a second file")
But this doesn’t actually work? How can I make use of other files within a Repl like this? Thanks!
Hey @MatthewPolack, welcome to the community!
Just click the three dots then click “Show hidden files”. Next click on the
.replit file and finally you can change the
entrypoint to whatever file you want to run.
You can also change the second line from
run="python main.py" to
run="python howtoRUN.py", though it won’t have an effect for interpreted languages as shown in the comment on line 1.
Also see the docs on how to configure a Repl: https://docs.replit.com/programming-ide/configuring-repl
P.S. nothing will happen because you didn’t call the function in the file.
For that to work you need to either import
PRINTER into the global scope like this:
from howtoRun import PRINTER. Otherwise you will need to access the method through the module like this:
I mentioned global scope above, if you’re a beginner, you might not have learnt about scope yet. Scope is very important in programming. There are two types of scope, global scope and local scope. Python isn’t the best language (in my opinion) when it comes to understanding scope, but the basics are, any variable outside of any function or
while/other statement (aka at the top level) is in the global scope. This means it can be ‘seen’ and accessed by any code if referenced correctly. Any variable such within a statement such as a function like this:
local_variable = 123
Is in the local scope, it can only be ‘seen’ and accessed by code within that statement, so code outside of
func will not be able to access
Thanks for this…think I’m outside of my depth here. I’m following along with a series of tutorial videos and developing simple test programs…and wanted to organise them like “Module 1 - print statements”, “Module 2 - if/else” etc. etc. so that I can easily refer back to them.
I think it is easiest for me just to make a new Repl per Module…rather than trying to combine them within multiple files. Thanks!