Back

Importing custom modules in python

  • 0
  • Functions
  • Cloud
Enricrivel
27 Mar, 2024, 21:21

I am working with a custom python module to catch AppwriteExceptions and return them in a standard format within my company, I am using the following import statement:

TypeScript
from src.errors.error_codes import handle_error

and while the code works just fine when I run it localy, when I deploy it, it returns a Module Not Found error, my current file directory looks like this:

TypeScript
│   .gitignore
README.md
│   requirements.txt

└───src
    │   main.py

    └───errors
            error_codes.py
            __init__.py
TL;DR
Developers are having trouble importing custom modules in Python, specifically getting a 'No module named 'src'' error when trying to import `'src.errors.error_codes'`. The error occurs during deployment but works fine locally. They are wondering about the need to `pip install` a module inside the function directory. One solution suggested is to check if `pip install -r requirements.txt` is set in the build settings. Another possibility is to try importing the module with `from errors.error_codes import handle_error` based on the directory structure.
Evdog
27 Mar, 2024, 21:24

Do you have pip install -r requirements.txt set in the build settings? What's the full error?

Enricrivel
27 Mar, 2024, 21:26
TypeScript
from src.errors.error_codes import *
ModuleNotFoundError: No module named 'src'
Enricrivel
27 Mar, 2024, 21:27

i do have the pip install set in the build settings, but do i need to pip install a module that is inside the function directory?

Evdog
27 Mar, 2024, 21:28

What does your requirements.txt look like?

Enricrivel
27 Mar, 2024, 21:30

just appwrite

Evdog
27 Mar, 2024, 22:37

based on the tree, any change if you just do from errors.error_codes import handle_error?

Enricrivel
27 Mar, 2024, 22:56

I tried doing that, and it returned the same error, just without 'src'

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more