Finley Adams
Finley Adams

Reputation: 831

How to import class from other directory following this structure python

Reading other posts I have structured my project as below:

/__init__.py
/src/main.py
/src/search-tree/node.py
/src/search-tree/multi_child_node.py
/src/utils/node_generator.py

Now when I'm inside node_generator.py I would like to import multi_child_node.py and use its class but I don't know how to do it, I have tried from .x import y but nothing. I'm new in python so maybe I'm missing something very simple.

Upvotes: 0

Views: 46

Answers (1)

Mina Melek
Mina Melek

Reputation: 305

Actually, the file structure depends on where do you run your main file, not the file itself.

For example, if your main.py calls node_generator.py like this

from utils import node_generator

and then node_generator.py calls multi_child_node.py, it should call it as it was called from main.py:

from search_tree import multi_child_node

P.S, you shouldn't use dash - or spaces in the naming for your module, but if you have to, check out the answer for this question How to import module when module name has a '-' dash or hyphen in it?

Upvotes: 1

Related Questions