Search code examples
pythonpython-importpython-packaging

How do you reference other branches of a python package?


I'm trying to create a python package where code in one branch references code in a parallel branch.

For example:

my_package/
 ├─utils/
 │  └─foo.py
 └─other_branch/
    └─bar.py

How can I reference code from utils.foo.py in other_branch.bar.py?


Solution

  • If my_package is a valid package, you can just import from it as usual:

    import my_package.branch_1.foo
    

    Alternatively, you can use relative imports:

    import ..branch_1.foo
    

    For details, check the documentation.