Python Forum
Puzzling import issue that I have no idea how to solvr
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Puzzling import issue that I have no idea how to solvr
#1
I'm building an API with django ninja, for design reasons I'm using some logic inside microservice/logic/one_module.py that needs to get JSON data from microservice/service/repository.py

This is my project structure:


Correlator/microservice/   <-------- Root directory is Correlator and that's where my CWD points from terminal
|   .coveragerc
|   manage.py
|   pytest.ini
|   
+---microservice/
    |   asgi.py
    |   models.py
    |   settings.py
    |   urls.py
    |   wsgi.py
    |   __init__.py
    |   
    +---logic/
    |       config.py
    |       correlate.py
    |       correlation_rules.py
    |       generate_ticket.py
    |       send_ticket.py
    |       startup.py  <--------- Code is being executed here
    |       temp.py
    |       __init__.py
    |   
    |           
    +---service/
    |   |   application.py
    |   |   domain.py
    |   |   repository.py   <--------- Data is here (must be imported)
    |   |   __init__.py
    |   |   
    |   +---schemas/
    |   |       application.py
    |   |       domain.py
    |   |       repository.py
    |   |       __init__.py
    |   |       
    |   +---tests/
    |       |   test_application.py
    |       |   test_domain.py
    |       |   test_logic.py
    |       |   test_repository.py
    |       |   __init__.py
    |       |   
    |       +---json/
    |               alert_1.json
    |               client_1.json
    |               secrets_1.json
    |           
    +---static/
    |       logo.png
    |       script.js
    |       styles.css
    |       
    +---templates/
            base.html
            email.html
In service/repository.py I have:

json_call = requests.get(...etc...)
clients, alerts, secrets = ...comprehension to get the data as dict...
In logic/startup.py I have:

from microservice.service.repository import clients, secrets, alerts

clients = ...some code using the data...
Running it like that, it raises an error:

Traceback (most recent call last):
  File "...windows_path...\Projects\Correlator\microservice\microservice\logic\startup.py", line 1, in <module>
    from microservice.service.repository import clients, secrets, alerts
ModuleNotFoundError: No module named 'microservice'
VSCode (in Windows 11) finds it just fine, however, I may also try a relative import:

from ..service.repository import clients, secrets, alerts

clients = ...some code using the data...
Running it like that, it raises an error:

Traceback (most recent call last):
  File "...windows_path...\Projects\Correlator\microservice\microservice\logic\startup.py", line 1, in <module>
    from ..service.repository import clients, secrets, alerts
ImportError: attempted relative import with no known parent package
VSCode does find it too with no issues. It also redirects to __init__.py correctly when trying to inspect it with the IDE. I'll copy a print(sys.path) statement so you know what's going on with that.

['...windows_path...\\Projects\\Correlator\\microservice\\microservice\\logic', '...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip', '...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\DLLs', '...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\Lib', '...windows_path...\\AppData\\Local\\Programs\\Python\\Python312', '...windows_path...\\Projects\\Correlator\\venv', '...windows_path...\\Projects\\Correlator\\venv\\Lib\\site-packages']
Trying to use the sys.path.append (I want to avoid that at all costs, I know writing some import code in __init__.py could be the solution) doesn't work either, will paste code and traceback.

import sys
import pathlib
import os
print("CWD WITH OS --->",os.getcwd())
sys.path.append(str(pathlib.WindowsPath().cwd()))
print("PYTHONPATH ---> ", sys.path)
from microservice.service.repository import clients, secrets, alerts
CWD WITH OS ---> ...windows_path...\Projects\Correlator\microservice\microservice\logic

PYTHONPATH --->  ['...windows_path...\\Projects\\Correlator\\microservice\\microservice\\logic',
'...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip',
'...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
'...windows_path...\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
'...windows_path...\\AppData\\Local\\Programs\\Python\\Python312',
'...windows_path...\\Projects\\Correlator\\venv', '...windows_path...\\Projects\\Correlator\\venv\\Lib\\site-packages'],
'...windows_path...\\Projects\\Correlator\\microservice\\microservice\\logic']

Traceback (most recent call last):
  File "...windows_path...\Projects\Correlator\microservice\microservice\logic\startup.py", line 1, in <module>
    from microservice.service.repository import clients, secrets, alerts
ModuleNotFoundError: No module named 'microservice'
I tried to do a test-A.py and test-B.py with foo() and var:int inside /logic and /service but same issue, no luck solving it. Honestly don't know what is causing the error, maybe it's a Django thing, every directory has an empty __init__.py

Many thanks, I'm running the /logic/startup.py script from the base path ...Correlator/ in Windows 11 with Python 3.12
Reply


Messages In This Thread
Puzzling import issue that I have no idea how to solvr - by starseeker - Feb-19-2024, 09:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Puzzling error PythonNewbee 1 1,421 Dec-10-2021, 05:51 PM
Last Post: deanhystad
  import psycopg2 issue bhuvneshdogra 1 3,053 Dec-27-2018, 04:03 PM
Last Post: Larz60+
  crypto import issue saisankalpj 2 7,981 Dec-20-2018, 06:08 AM
Last Post: saisankalpj
  Win10 Import math issue Oldsquid 2 3,142 May-12-2018, 11:08 AM
Last Post: Oldsquid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020