Search code examples
pythonpython-importrelative-pathpython-module

python importing relative modules


I have the Python modules a.py and b.py in the same directory. How can I reliably import b.py from a.py, given that a.py may have been imported from another directory or executed directly? This module will be distributed so I can't hardcode a single path.

I've been playing around with __file__, sys.path and os.chdir, but it feels messy. And __file__ is not always available.


Solution

  • Put the directory that contains both in your python path... or vice versa.