Ask coding questions

← Back to all posts
"no such file: main.py" How to organize and import class files on replit
RichardPeskett (0)

Hi,
I have not been able to import a class that I have created from a file in my repl. I've been able to import other packages, but not my own classes from files that I have created. this might be a problem related to how to organize the files within a replit project.

How do you set up the class files and the main file in Replit? I've tried it a couple different ways, each time getting "no such file: main.py" In the case of the 0121 Classes test project (mistakenly named 02121 instead of 0421 for today's date), I tried it with main outside of the folder called 0421Classtest and after I dragged main into that folder. Same result of "no such file: main.py ."

Thank you!
Rick

Comments
hotnewtop
Bookie0 (6261)

When making new Python files, it's [file name].py. So instead of triangle, it should be triangle.py. Same for polygon. Then, you can just keep from triangle import *, and it should work.

Good luck! :D

RichardPeskett (0)

@Bookie0
Hi,
Thank you very much! After I changed the file extensions to .py, the Python logo appeared to the left of the file names and main.py ran!

If I commented out everything related to the Rectangle class, I was able to print the area of the t object (Triangle class), so the class files were imported to main! Nice.

But, I kept getting "name Rectangle is not defined," on line 5 of main.py I attached screenshots. It seems to me that the Rectangle class is properly defined.

Also, is it necessary to attach screenshots, or can you run my main and see for yourself, in replit?
Thanks,
Rick

Bookie0 (6261)

@RichardPeskett Hm it seems like everything is defined correctly..not very sure why. Perhaps try instead of from rectangle import * you could just import the class: from rectangle import Rectangle.

Also, yes I can run your repl and make changes (on a copy), but replit is being extremely slow and laggy, I can't manage to run it as it keeps on installing polygon3 (do you use it?)

So yeah sorry, but good luck anyways :)

robowolf (549)

Try taking the files out of the folder. If that doesn't work, start a new repl and make sure main.py always stays out of the folder.

RichardPeskett (0)

@robowolf
Hi,
Thank you for the quick reply! Screenshot attached after I took all files out of folder.

I'll try a new repl and keep main out of any folder.

robowolf (549)

@RichardPeskett The file triangle should be triangle.py

RichardPeskett (0)

@robowolf
Thank you very much! It worked!