Python local imports still not working: ModuleNotFoundError or ImportError

63 Views Asked by At

How can I import src/constants.py from within src/data/compute_embeddings.py?

Project structure

I have the following project structure:

.
├── data
│   └── raw
│       └── sample.xlsx
├── __init__.py
├── notebooks
│   └── clustering.ipynb
└── src
    ├── constants.py
    ├── data
    │   ├── compute_embeddings.py
    │   └── __init__.py
    ├── embeddings.py
    └── __init__.py

Error

Now, within src/data/compute_embeddings.py, I try to import src/constants.py. Here is what happens when I run python src/data/compute_embeddings.py:

Import method Error
from src.constants import ... ModuleNotFoundError: No module named 'src'
from ..constants import ... ImportError: attempted relative import with no known parent package

I tried running as a module with python -m src/data/compute_embeddings, but I get No module named src/data/compute_embeddings.

Undesirable fix

I am however able to add src to sys.path and then my import works. However, I would rather not have to include import sys + sys.path.append(...) at the beginning of every file.

Thank you in advance for your help!

0

There are 0 best solutions below