Home > Blockchain >  ModuleNotFoundError when import from constants file in python
ModuleNotFoundError when import from constants file in python

Time:11-27

app-main-folder
    /local
        /__init__.py
        /run.py
    constants.py

i am trying to import from constants in run.py its throwing this error Traceback (most recent call last): File "local/run.py", line 4, in from init import app File "/home/manavarthivenkat/ANUESERVICES--BACKEND/local/init.py", line 5, in from constants import BaseConstants ModuleNotFoundError: No module named 'constants'

CodePudding user response:

pip install constants

Try this on your shell and try running run.py

make sure you load the constants library

CodePudding user response:

this is because Python assumes all your python files are in the current directory. you should tell the compiler you are looking for the file somewhere else.

from app-main-folder.constants import constants # assuming constants is the name of your class in that constants.py

CodePudding user response:

You can use sys.path.append to tell Python interpreter where to search the modules.

First, you can check the current Python path

import sys
from pprint import pprint
pprint(sys.path)

If the path to the directory of constants.py is not included in the output, you can manually add that path, by

import sys
sys.path.append("/path/to/constants.py")
  • Related