Swind D.C. Xu Swind D.C. Xu - 1 month ago 8
Python Question

How can I just import a subdir and use "subdir.pythonfile.function" in python?

I have a python project with following structure:

project

--subdir

----__init__.py

----func.py (has a function a)

--main.py


I want to use the function "a" in main.py. So I can

import subdir.func as F


F.a()


But this is not what I want to do, I want to use

import subdir


subdir.func.a()



How can I do that easily?




Solved, Thanks.

Answer

Keep your directory structure as it is and add this line in your init.py :

from . import func