uwsgi no python application found錯誤的解決(python3+centos6)

近期在努力把本身的項目從python2轉到python3上,由於生產環境沒法拋棄centos7,因此只好在centos7上安裝了python3。裝好了python3,將python命令軟鏈接改爲python3的,同時也將pip指向了python3版本的pip。一切都很順利,但在用uwsgi啓動一個django的web服務時才發現出了問題:服務是啓動了,可是一訪問接口就返回500,再一看uwsgi這邊的日誌,顯示:no python application found 。python

nginx+uwsgi+django的模式在以前屢次配過,都沒有出現問題啊,爲啥一到了python3的環境下就出問題了?個人環境應該配置得差很少了啊,並且全部的模塊也都是在python3的環境下裝的啊。nginx

再次檢查了個人配置文件:web

[uwsgi]
socket = 127.0.0.1:3031
chdir = /opt/testproj/
wsgi-file = testproj/wsgi.py
processes = 4
threads = 2

感受也沒有啥問題啊。因而又開始仔細查看uwsgi這邊輸出的日誌,再往前看看,纔看到了這麼個報錯:django

*** Operational MODE: preforking+threaded ***
Traceback (most recent call last):
  File "testproj/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***

沒有找到django模塊?原來成功啓動只是一個假象,實際上如今uwsgi沒找到django這個模塊,一提供服務確定就500了。centos

可是爲啥會沒有找到django模塊呢?uwsgi和django都是用python3的pip來裝的啊,雖然這個centos7上還有python2,但也只是爲了讓yum還能正常使用而保留的啊。app

在網上一頓找,才發現,uwsgi還真是有點特殊,在python2和python3共存的系統上就會有點問題,此次啓動時沒法找到django模塊是由於uwsgi命令使用python2的環境來進行了啓動。須要在配置文件中指定所須要的庫環境才能夠。socket

因而開始找django的環境,使用pip show能夠看到django安裝的location:centos7

[root@knktc testproj]# pip show django|grep -i location
Location: /usr/local/lib64/python3.6/site-packages

順便把pytz的路徑也找下:日誌

[root@knktc testproj]# pip show pytz|grep -i location  
Location: /usr/local/lib/python3.6/site-packages

把這兩個路徑使用pythonpath參數加入到配置文件中,修改後的uwsgi配置文件是下面的這個樣子的:code

[uwsgi]
socket = 127.0.0.1:3031
chdir = /opt/testproj/
wsgi-file = testproj/wsgi.py
processes = 4
threads = 2
pythonpath = /usr/local/lib64/python3.6/site-packages
pythonpath = /usr/local/lib/python3.6/site-packages

再啓動一次試試:

uwsgi --ini uwsgi.ini 一切正常!

相關文章
相關標籤/搜索