👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
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?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Importing class from another file, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Importing class from another file
- Deutsch Importing class from another file
- Français Importing class from another file
- Español Importing class from another file
- Türk Importing class from another file
- Русский Importing class from another file
- Português Importing class from another file
- Polski Importing class from another file
- Nederlandse Importing class from another file
- 中文 Importing class from another file
- 한국어 Importing class from another file
- 日本語 Importing class from another file
- हिन्दी Importing class from another file
San Francisco | 2023-03-22
I was preparing for my coding interview, thanks for clarifying this - Importing class from another file in Python is not the simplest one. Will use it in my bachelor thesis
Massachussetts | 2023-03-22
Maybe there are another answers? What Importing class from another file exactly means?. I just hope that will not emerge anymore
Texas | 2023-03-22
Simply put and clear. Thank you for sharing. Importing class from another file and other issues with code Python module was always my weak point 😁. Will use it in my bachelor thesis