1

Currently I have the following directory

my_epic_module
-> .gitignore
-> my_epic_module.py
main.py

When I try to import my_epic_module.py from main.py I do the following

# the following does not work
import my_epic_module
my_epic_module.bar("foo")

# the following works
from my_epic_module import my_epic_module
my_epic_module.bar("foo")

How do I make it so that I can just import it "normally" like the top first example? Do I need to add an __init__.py? If so what do I need to put into it?

2 Answers 2

1

python module is a file. a folder with __init__.py is a package. Both of them can be used with import statement.

if you want to use import my_epic_module, you have serval way to achieve it.

put my_epic_module.py on the top folder

OR

add __init__.py to my_epic_module folder and add from .my_epic_module import * to __init__.py file

OR

add my_epic_module folder to sys.path

Sign up to request clarification or add additional context in comments.

Comments

0

Yes you have to add __init__.py file in your folder.

No need to write anything into __init__.py file. Just keep it in you folder. It makes your folder like a package then you can import it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.