How to import a project subfolder so that it is available once a project has been imported?
For example how to import the contents of project.tools
so that after importing the project using import project
, project.tools.common.function()
is available?
project
|
|--tools
| |--__init__.py
| \--common.py
|
|--__init__.py
|--core.py
\--cli.py
I've tried the following:
from . import tools
inproject/__init__.py
andfrom . import *
inproject/tools/__init.py
which resulted in:ImportError: cannot import name 'tools'
.from .tools import *
inproject/__init__.py
andfrom . import *
inproject/tools/__init.py
which resulted in:ModuleNotFoundError: No module named 'project.tools'
.from .tools import common
inproject/__init__.py
andfrom . import *
inproject/tools/__init.py
which resulted in:ModuleNotFoundError: No module named 'project.tools'
.
Use
import tools
in your .py script directly. Then usetools.common.function()
in the function call.When you place init.py in your folder, the folder becomes callable. Hence you can import that folder and its scripts directly into your scripts.