0

I have a simple file structure as follows

+-- project
|   +-- app
    |   +-- server.py
    +-- box
    |   +-- __init__.py
    |   +-- boxes.py

I am trying to import functions in boxes.py to server .py as follows:

from box.boxes import functions

but I keep getting the following error:

ModuleNotFoundError: No module named 'box'

Can anyone explain what I am doing wrong?

Abdul9401
  • 17
  • 6

2 Answers2

0

You have to set the PYTHONPATH variable to point to the 'project' directory. Doing so will cause all import statements to start indexing from that location. Currently, your imports look in your local directory from where the caller script (server.py) is located (aka the app folder) and of course there is no box folder containing a boxes module in there.

Here's a simple workbook containing your exact problem to get you quickly set up:

https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html

Hope this helped :) Good luck!

  • But this would only make the code run on my machine and not other machines, right? – Abdul9401 Nov 05 '21 at 18:17
  • Only if you do absolute path. What you want is relative path (aka in relation to the location of where the caller script is located and not from the root directory). Alternatively, if you want to not bother with the environment variable, you can do something like this if you don't mind the long line in the code and the imports not being at the top: https://stackoverflow.com/questions/54874706/setting-relative-pythonpath-for-project-in-windows-visual-studio-code PS: If this helped, don't hesitate to thumbs up the answer and mark it as the answer :) It would be much appreciated! – Marc-Alexandru Baetica Nov 05 '21 at 18:29
0

try with import .box.boxes or from box import boxes