Reputation: 758
Why am I having an 'undefined bdir module' error, here is my directory,
a.py
bdir->bdir>module.py
in a.py
from bdir import *
Upvotes: 3
Views: 6243
Reputation: 82440
Any folder without a __init__.py
file inside of the folder is not considered a module. Furthermore, if you want to import *
from a module, make sure that actually import the things you require into __init__.py
, or declare a __all__
list.
Also, if you want to make a relative import, meaning that you want to import a file from the package that a module is currently in, then you do a relative import. So, for example, if you have:
bdir
- bdir
- __init__.py
- module.py
- a.py
In order to import anything from the bdir.module
, you have to import it like so if you are in a.py
:
from .module import *
If outside the bdir
module then:
from bdir.module import *
Upvotes: 1
Reputation:
You must create a __init__.py
file, that's how Python knows which folders are packages that can be imported using the import
. Here's the documentation:
The
__init__.py
files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case,__init__.py
can just be an empty file, but it can also execute initialization code for the package or set the__all__
variable.
Upvotes: 1