2

I have the following directory for a project:

photo_analyzer/
│
├── main.py                # Main script to run the photo analyzer
├── gui/                   # Directory for GUI-related files
│   ├── __init__.py        # Package initialization
│   ├── app.py             # Tkinter application class and main GUI logic
│   ├── widgets.py         # Custom Tkinter widgets (if needed)
│   ├── styles.py          # Styling configurations for the GUI
│   └── resources/         # Directory for GUI resources (e.g., icons, images)
│       ├── icon.png       # Application icon
│       └── ...
│
├── photo_analysis/        # Directory for photo analysis-related files
│   ├── __init__.py        # Package initialization
│   ├── photo_utils.py     # Utility functions for photo manipulation
│   ├── light_profiles.py  # Functions to analyze light profiles in photos
│   └── ...

I'm trying to import photo_utils and light_profiles from the photo_analysis module into gui/app.py. Whenever I try to run app.py, I get the following error message:

Traceback (most recent call last):
  File "c:\Users\Josh\Documents\PhotoAnalyzer\gui\app.py", line 6, in <module>
    from photo_analysis import photo_utils
ModuleNotFoundError: No module named 'photo_analysis'

I have empty __init__.py files in both directories, so I don't think that's the issue. I've tried adding the photo_analysis module to the Python path using SET PYTHONPATH="C:\Users\Josh\Documents\PhotoAnalyzer\photo_analysis" and I've tried to use the sys.path technique as well. Both end with the same error message as before.

photo_utils.py imports widgets.py from the "gui" module. Could I be experiencing circular dependency? I did try the work-around explained in text by importing widgets into each method in photo_utils that used it, and while this allowed a test main code in photo_utils to run properly, app.py still gives the same error message that there is no module named 'photo_analysis'.

What am I missing?

Josh Olson
  • 23
  • 5

3 Answers3

2

It is a tricky one. Normally python imports from their sibling files or childs of sibling folders. Here if you want to import from a file that are from same parent then you have to use sys.path.append('.')

Secondly it also depends on the terminal current working directory. If your current working directory is "photo_analyzer" then adding these line will work

import sys
sys.path.append('.')
import photo_analysis.photo_utils as pa

Otherwise if your current working directory is "photo_analyzer/gui", then you have to add these lines

import sys
sys.path.append('./../photo_analysis')
import photo_utils as pa
moeny'''
  • 36
  • 3
0

Navigate to the photo_analyzer/ directory and run

python3 -m gui.app
nate-thegrate
  • 378
  • 2
  • 13
  • I am still getting the same error. Running the file in a command prompt is going to have the same outcome as running the main method in the actual python script. – Josh Olson Aug 01 '23 at 20:11
  • interesting… I replicated your file structure on my machine and the above command worked for me. Hopefully someone can chime in with some insight about this discrepancy. – nate-thegrate Aug 01 '23 at 20:15
0

I think you need to use relative imports.

In gui/app.py, you can use relative imports to import modules from the photo_analysis package.

# gui/app.py

# Use relative import to import photo_utils from photo_analysis package
from ..photo_analysis import photo_utils

# Use relative import to import light_profiles from photo_analysis package
from ..photo_analysis import light_profiles

The .. in the import statement means to go up one level in the directory hierarchy, and then you can access the modules inside the photo_analysis package.

Also, make sure you are running main.py or another script from the root directory (photo_analyzer/) that imports gui.app or other modules. If you run app.py directly, it might not find the photo_analysis package.

This guide has a pretty good explanation of how relative imports work.