Importing class from another file

Before you mark it as duplicate please read my problem:

I am trying to import a class from a file from a subdirectory

> main.py
> --->folder/
> ----->file.py

and in file.py i have a class imlpemented ( Klasa)
What have I tried:

putting in main.py:

from folder import file
from file import Klasa

I am getting the error:

from file import Klasa

ImportError: No module named ‘file’

When I try to use just:

from folder import file

I get this error:

tmp = Klasa()

NameError: name ‘Klasa’ is not defined

I have put an empty __init__.py in the subfolder and it still does not work, and I have put in the __init__.py : from file import Klasa and still doesnt work.

If main and file are in the same folder this work:

from file import Klasa

but i want them to be in separate files.

Can someone tell me what i am doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your problem is basically that you never specified the right path to the file.

Try instead, from your main script:

from folder.file import Klasa

Or, with from folder import file:

from folder import file
k = file.Klasa()

Or again:

import folder.file as myModule
k = myModule.Klasa()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x