How do I do imports from a python script when my main scripts are in a subfolder of the whole package? -
i'm trying create small folder structure project , not want complicate things much. want split components into
- a folder reusable code each tool try
- a folder main script files, can call via python script.py
so current structure looks this:
project | - tools | - __init__.py - tool_a | - __init__.py - file.py - file2.py | - tool_b | - __init__.py | - start_scripts | - start_use_case_1.py - start_use_case_2.py
now inside start_use_case_1.py
i'd import of module tools.tool_a.file
. following approaches have failed:
import tools.tool_a.file
and folder project/
call python start_scripts/start_use_case_1.py
. cannot find module tools
. when same interactive python shell, works.
the following didn't work either:
from ..tools.tool_a import file
systemerror: parent module '' not loaded, cannot perform relative import
i read it's not allowed relative imports __main__
scripts. though not care style, because these startup scripts throwaway part of project. not want retype them each time in interpreter.
you need add path directory importing code snippet file can done :
import sys sys.path.append("./")
now can : from tools import tool_a
Comments
Post a Comment