gunicorn+django+postgres:

执行命令:
gunicorn your_project.wsgi -c gunicorn.conf.py
出现错误
raise ImproperlyConfigured("Error loading psycopg2 or psycopg module") django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module

问题描述:所有依赖都安装了,但是启动项目的时候依旧是出现上面的错误.
第1步:当前的venv环境与项目的是否对应

python -c "import sys; print(sys.prefix)"

第2步:当前使用的gunicorn命令的venv环境与项目的是否对应

which gunicorn
head -n 1 $(which gunicorn)

第3步:如果以上没问题,则强制使用当前环境启动项目

python -m gunicorn your_project.wsgi -c gunicorn.conf.py

如果启动成功则说明之前启动命令时,没有用到当前环境的依赖

以下时gpt的解释:

  • unicorn 作为 console script:
  • 入口代码是在安装时生成的
  • 受 pkg_resources / importlib.metadata、PATH、缓存影响
  • 在某些情况下(尤其升级过 gunicorn / pip / Python)
  • 直接执行 gunicorn ≠ python -m gunicorn
    如果有强迫症,不想用python -m 命令,可以执行下面的命令:
pip uninstall gunicorn -y
pip install gunicorn
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐