After looking at many answers here, I still dont understand how to structure a python project with imports...
Let's say my dir structure is
# We are in create_bla_dataset.py
from ..utils import tool1
... define how to create a dataset
if __name__ == "__main__":
# demo the dataset
> cd <dir_contains_myproject>
> python -m myproject.datasets.create_bla_dataset
from ..datasets import create_bla_dataset
ValueError: attempted relative import beyond top-level package
When you use IPython/Jupyter Notebooks you need take account that ipython server only see files from a root directory defined from where you launch the notebook instance. In this case, maybe you are launched directly the notebook file and not from the project root.
Also, you can use
Updated: You can add a cell at top of the notebook.
import sys, os sys.path.insert(0, os.path.dirname(os.path.abspath('..')))
myproject is recognized as a package and you can import as is.
from myproject.datasets.create_bla_dataset import bar bar() >>> 'hi foo hi bar'
This is because relative import only work in packages and if you need use as package you need to add the path where is the package.