Search code examples
pythonpython-importrelative-path

How to reference a module in its own folders?


I have python module, that i added to some other source root folder like this

MyProject/my_new_file.py
MyProject/MyModule/__init__.py
MyProject/MyModule/script1.py
MyProject/MyModule/script2.py

And because root source is folder "MyProject", script1.py cannot include script2.py file like this:

from script2 import my_awesome_function

Does someone know a way that my modules imports are isolated within their own folder, so i dont have to reference import path from source root


Solution

  • You can use relative imports:

    from .script2 import my_awesome_function