modulenotfounderror: fastapi... but only when the systemd service starts it??? ugh

pippythonvenv4 replies · 104 views · started
  1. small Flask-ish API, nothing fancy. I moved it to a new box, created a venv, installed requirements, and hooked it up to the systemd service 🤷 instead of a running service I got this traceback:

    PS C:\scripts> python sync_reports.py

    PS C:\scripts> python sync_reports.py
    Traceback (most recent call last):
      File "C:\scripts\sync_reports.py", line 3, in <module>
        import requests
    ModuleNotFoundError: No module named 'requests'
    PS C:\scripts> pip install requests
    Requirement already satisfied: requests in c:\users\...\python311\lib\site-packages
    PS C:\scripts> python sync_reports.py
    ModuleNotFoundError: No module named 'requests'
    

    the weird part: if i source venv/bin/activate and run it, no error at all :) Is teh venv only half real?

    Do i need to activate the venv inside teh service somehow? Or copy packages to the system Python? Id rather understand teh right way than hack something together that breaks again next month. seriously need help here 😭

    4
  2. Did you install with pip or python -m pip, and was teh venv active at that moment? ?

    23 0 helpful
  3. im on Python 3.12 in the venv but the system Python is older. I never noticed cuz teh version banner looked similar. sorry for the wall of text lol

    15 0 helpful
  4. ahh classic almost always this is two different interpreters. compare them directly:

    which python
    python -c "import sys; print(sys.executable)"
    python -m pip --version
    

    then check what gunicorn actually calls. if teh unit or script says /usr/bin/python3, it will never see packages from ur venv. point it at /path/to/venv/bin/python explicitly.

    0 3 helpful
  5. ahh classic stop using bare pip. use python -m pip install redis with teh exact interpreter that runs the app. That way the package lands in the same site-packages that teh interpreter searches.

    1 0 helpful

Reply to this topic

Sign in or create an account to reply.