0

Summary

I've a repository with the following structure:

phd-notebooks
├── notebooks
|   ├── resources
|   |   └── utils.py
|   ├── notebooks-group-1
|   |   ├── notebook1.ipynb
|   |   └── notebook2.ipynb
|   └── notebooks-group-2
|       ├── notebook1.ipynb
|       └── notebook2.ipynb
├── .gitignore
├── LICENSE
└── README.md

In my notebooks, say in notebooks-group-1\notebook1.ipynb for example, I want to import the module utils.py.

If I try from resources import utils I get ModuleNotFoundError: No module named 'resources', if I try from ..resources import utils I get ImportError: attempted relative import with no known parent package.

I've tried to debug notebooks-group-1\notebook1.ipynb prompting os.getcwd() and I get notebooks-group-1 as the current working directory.

I am using Visual Studio Code and a conda environment with python 3.9.13.

What I've tried

I've tried adding an __init.py__ file in all folders (notebooks, notebooks\resources, notebooks\notebooks-group-1, notebooks\notebooks-group-2) but I get the same errors. I don't want to use the sys.path.append hack or make my entry point at the top level, as suggested here, because when I will make the repository available I want to make running my notebooks by other people as simple as possible.

I'm aware of this frequently cited question, but I could not find any solution that would suit my case. Running my script using -m is not possible because I'm using notebooks, and even if was it would go against the requirement of making running my notebooks as simple as possible. Setting __package__ manually is not well-suited for use in real-world code, similarly to the sys.path.append hack. I'm doutbful about using setuptools because resources is supposed to be a folder with a collection of utility modules for the notebooks, not a real package (it feels like an overcomplication).

How can I import the module utils.py in my notebooks in a pythonic way? Or it's the structure of my repository that is bad?

fma
  • 219
  • 2
  • 14

0 Answers0