Ask coding questions

← Back to all posts
'Run' only works with main.py
projectuwb1 (1)

Just started my python journey and realise that the 'Run' button only works when I have the main.py file in my repl.

I tried to import a new file i added to the repl called "learn.py" by inserting import learn.py as the first line in main.py but it will not run.

The error I receive is: Traceback (most recent call last):
File "main.py", line 1, in <module>
import learn.py
ModuleNotFoundError: No module named 'learn.py'; 'learn' is not a package

Anybody knows how I could run files other than main.py?

Answered by GermanScot (5) [earned 5 cycles]
View Answer
Comments
hotnewtop
GermanScot (5)

1) did you add learn.py to your workspace?
2) did you import learn? You don't need the .py in the import statement

If you just use "import learn" then you need to use it like: learn.func(). -> better way
If you want to just use func(), then use from learn import * -> potential name clashes and unclarity

projectuwb1 (1)

Thanks for the response @GermanScot I think removing the .py seems to have solved it. I now have multiple file and they all run when I have them imported!

FloCal35 (667)

or you can import os and use os.system("python learn.py") and it just runs the file