Stolen from The Hitchhiker’s Guide to Python1, I use a context.py
file to import python source code located in another directory. This is especially useful for importing source code into test files (which are typically located under the test/
directory for me) and for ipython notebooks (which are typically located under the notebooks
directory for me).
1 https://docs.python-guide.org/writing/structure/
Stick the following snippet in a context.py
file in the directory where the source code is required to be imported.
context.py
import os
import sys
0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.insert(
import sample
And in the file where the module need to be imported, stick the following.
your-python-file.py
from .context import sample