Search code examples
pythonimportfile-structure

Python nested import


I can't get my mind around this problem

Let's suppose we have a file structure

Folder
├── main_file.py
├── Subfolder
│   ├── __init__.py
│   ├── file_a.py # def foo()
│   ├── file_b.py # from file_a import foo; def bar(...use function foo())

How can I use function bar in my main_file.py without nothing going wrong, because if I only import function bar from file_b it throws me error: 'there's no module named file_a'

Thank you for answer

edit thanks, I only have trouble with some imports inside my file, so after adding the Subfolder. to all imports it works well Thank you


Solution

  • You need to specify the RELATIVE directory w.r.t main_file.py. In this case you can find and import the function bar like this :

    from Subfolder.file_b import bar