Feb-19-2024, 09:58 PM
(This post was last modified: Feb-19-2024, 09:58 PM by starseeker.)
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:
Many thanks, I'm running the /logic/startup.py script from the base path ...Correlator/ in Windows 11 with Python 3.12
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.htmlIn 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 packageVSCode 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, alertsCWD 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__.pyMany thanks, I'm running the /logic/startup.py script from the base path ...Correlator/ in Windows 11 with Python 3.12
