weather api
weather api

Reputation: 758

Unable to import module in Python

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

Answers (3)

Nafiul Islam
Nafiul Islam

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

user7560588
user7560588

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

VdF
VdF

Reputation: 49

Put an __init__.py file (even empty) in your bdir folder.

Upvotes: 1

Related Questions