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

I am trying to import from constants in run.py it's throwing this error

Traceback (most recent call last):
  File "local/run.py", line 4, in <module>
    from init import app
  File "/home/manavarthivenkat/ANUESERVICES--BACKEND/local/init.py", line 5, in <module>
    from constants import BaseConstants
ModuleNotFoundError: No module named 'constants'
raiyan22
  • 1,043
  • 10
  • 20

3 Answers3

0
pip install constants

Try this on your shell and try running run.py

make sure you load the constants library

Frank
  • 1
  • 1
  • what if constants.py is module i have written and already present in the current file structure mentioned above – Venkat Manavarthi Nov 27 '22 at 00:46
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 13:24
0

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
Ahmed Aredah
  • 308
  • 2
  • 7
0

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")
jiayu
  • 50
  • 5