Reputation: 59
I work with spyder and have a problem to import of modul from folder.
What I did:
1- create a folder with name "Modul" on my desktop.
2- create __init__.py
in this folder.
3- create hallo.py
in this folder.
I wrote in hallo.py:
def welt():
print("Hallo Welt")
def mars():
print("Hallo Mars")
Now I want to use this syntax to run welt function:
from Modul import hallo
hallo.welt()
But it doesn't work and have this error:
ModuleNotFoundError: No module named 'Modul'
What should I do or what I doing wrong?
Upvotes: 0
Views: 31
Reputation: 523
The import statement depends on where is your importing file and implementing file. In your case, its where is hallo.py
file and a file where you will write import
statement.
like, if your implementing file and hallo.py
is in same folder then you should use:
import hallo
and if your folder structure is like this:
|module_folder
|---->hallo.py
|---->__init__.py
|implement.py
then you should use:
from module_folder import hallo
to import from the folder you need to create an empty file name __init__.py
else it won't work.
Upvotes: 1