user2290820
user2290820

Reputation: 2749

proper way to import modules in python 3

I've a project like:

./project_dir/
├── project.py
├── __init__.py
└── lib
    ├── constants.py
    ├── __init__.py
    ├── a.py
    ├── b.py
    ├── test_a.py
    └── test_b.py

in a.py

if i do import constants

and in b.py i do from a import someClass

what would be the proper way to import a and b in project.py ? It usually throws

ModuleNotFoundError: No module named 'a'

how to import things from a and b?

Upvotes: 0

Views: 115

Answers (2)

Philippe
Philippe

Reputation: 26422

complete solution:

project.py

from lib import a,b

a.py

from . import constants

class someClass:
    pass

b.py

from .a import someClass

Upvotes: 1

P Cresswell
P Cresswell

Reputation: 51

Module not found lets you know that it hasn't been discovered in the current path. If you're active in the project.py directory (which is normal when running from there), you'll need to include as follows:

from lib.a import someClass

# call someClass
var = someClass()

or

from lib import a,b

# call someClass
var = a.someClass()

Upvotes: 1

Related Questions