ModuleNotFoundError: Python cannot identify import statement

Viewed 54

I have tried similar questions related to this topics but none of them worked for me. I have a simple Python project structure as below:

Project
|
---> src
|    |
|    ---> __init__.py  # empty file
|    ---> main.py
|
---> test
     |
     ---> test.py

Inside test.py, I want to import everything from main.py. I've done following methods, none of them worked.

from main import *
from .main import *
from src.main import *
from .src.main import *
from ..src.main import *
from src import *
from .src import *

How can I achieve this?

2 Answers

A common problem of mine. Try writing a setup.py file and installing your package by running python setup.py in your terminal.

Since src directory is "above" the test directory one-level, you can use .. to specify up one-level to src then import the main sub-module as:

from ..src import main

Per PEP328 Rationale for Relative Imports / from ...foo import bar

Related