Jsmoka
Jsmoka

Reputation: 59

How can I import a Modul from folder

I work with spyder and have a problem to import of modul from folder.

What I did:

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

Answers (1)

Bipin Maharjan
Bipin Maharjan

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

Related Questions