0

I'm facing a problem with a Data Science project in Python. I'm trying to import a module from a different directory, but I'm getting the error "ImportError: Attempted to import relative with no known parent package". I've read several answers related to this error, but I haven't been able to resolve the issue in my specific case.

The structure of my project is as follows:

 ├── notebooks (folder)
 │ └── data_evaluation.ipynb
 ├── src (folder) 
 │ └── utils.py
 │ └── webscraping (folder)
 │       └── scraping_data.py

I would like to import functions from the utils.py module in both scraping_data.py and data_evaluation.ipynb.

I've tried several combinations of relative relationships, but none seem to be working. How can I resolve this ImportError error and import the util_db.py module functions correctly in different parts of my project?

Rate in advance for any help!

Some of the things I've tried:

from src.utils import function
from utils import function
from ..utils import function
from project.src.utils import function

PS: my project contains an empty init.py in all directories

leon
  • 1
  • 1
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Brian61354270 Aug 17 '23 at 01:59
  • Which folder(s) are your top-level package(s), and how are they being added to the Python path? – Brian61354270 Aug 17 '23 at 02:00
  • Do note that imports _do not navigate folders_. Imports work by searching the Python path. `import x` doesn't mean "search for `x` in the current folder", it means "search for a top-level module named `x` somewhere on the Python path". In order to import modules from your project, _you need to add them to the Python path_. That's usually done by either a) installing your project as a distribution package (preferable, and pretty simple these days), or b) relying on the directory containing your package(s) being your CWD. – Brian61354270 Aug 17 '23 at 02:01
  • How are you running the project? – Mr_and_Mrs_D Aug 23 '23 at 08:20

0 Answers0