Home > Back-end >  import inside a python package - ModuleNotFoundError
import inside a python package - ModuleNotFoundError

Time:01-21

I'm trying to build a package where specific modules call other modules. Example of the structure of the package:

provapackage/
├── main.py
└── pippo
    ├── derivative_functions.py
    ├── functions_pippo.py
    └── __init__.py

content of the functions_pippo module:

def add(x,y):
    return x y

content of the derivative_functions module:

from functions_pippo import add

def add_lst(l):
    n=0
    for i in l:
        n = add(n,i)
    return n

content of the main.py file:

from pippo.derivative_functions import add_lst

lst = [1,2,3,4]
print(add_lst(lst)) 

when I run main.py I get this error:

Traceback (most recent call last):
  File "/home/g/Documents/provapackage/main.py", line 1, in <module>
    from pippo.derivative_functions import add_list
  File "/home/g/Documents/provapackage/pippo/derivative_functions.py", line 1, in <module>
    from functions_pippo import add
ModuleNotFoundError: No module named 'functions_pippo'

It seems like python cannot find the functions_pippo module even if it's in the same folder. But when I run derivative_functions.py I don't gat any error message. Is it an import problem?

CodePudding user response:

See this answer here

from .functions_pippo import add
not
from functions_pippo import add

  • Related