PetarMI
PetarMI

Reputation: 390

ModuleNotFoundError in a Python project using packages

I have a Python project (python 3.6) with a rather simple package structure. My goal is to have a package containing modules which are reused across other packages. Here is the structure:

    my_project                   
    ├── /docs           
    ├── /src
    │   ├── __init__.py                
    │   ├── /common
    │   |    |── __init__.py      
    │   |    |── common_module.py
    │   ├── /packageA
    │   |    |── __init__.py      
    │   |    |── modA.py

The problem is that when I do the following inside modA.py:

    from src.common import common_module as cm

I get the following error: ModuleNotFoundError: No module named src.common

What I have tried so far is

None of these worked and I have been struggling with this problem for a while now so any suggestion would be appreciated.

Upvotes: 0

Views: 351

Answers (2)

sslloo
sslloo

Reputation: 521

Yes you should put __init__.py in project,
then export PYTHONPATH = <LOCAL_PATH_TO_...>/project

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

In order for this import to work, PYTHONPATH would need to be .../my_project, not .../my_project/src.

Upvotes: 2

Related Questions