Damocls
Damocls

Reputation: 1

Impossible to import a package I made. "ModuleNotFoundError"

I have a project organized like so :

application
 ├── app
 │   └── package
         └── __init__.py
 │       └── functions.py
 └── app2
     └── some_folder
         └── file_2.py

My "functions.py" contains a basic function:

#functions.py
def add(x,y):
    return x+y

The file "_init_.py" is empty

I want to use the "add" function in my "file_2.py" file, so I write:

#file_2.py
from application.app.package.functions import add
print(add(2,3))

But it returns an error message:

ModuleNotFoundError: No module named 'application'

it is the same if i try any of these:

from app.package.functions import add
from package.functions import add
from functions import add

Does anyone know where the problem comes from? I'm doing exactly like in this tutorial so I don't understand what's wrong

tutorial's link

Thank you for your help

Upvotes: 0

Views: 56

Answers (1)

Tim0123
Tim0123

Reputation: 195

One way to import functions.add is to import sys and use sys.path.insert() after that you can import add from functions:

import sys
sys.path.insert(1, 'the/local/path/to/package')

from functions import add

print(add(1,2))

Upvotes: 1

Related Questions