簡體   English   中英

mod_wsgi:ImportError:沒有名為'encodings'的模塊

[英]mod_wsgi: ImportError: No module named 'encodings'

我在CentOS 6.5上使用Apache 2.2.15。 我正在嘗試使用mod_wsgi設置Django應用程序。 我正在使用虛擬環境,並且mod_wsgi配置了--with-python=/path/to/virtualenv/bin/python3.4

我把它添加到我的httpd.conf

WSGIPythonPath /srv/myproject:/path/to/virtualenv/lib/python3.4/site-packages
WSGIPythonHome /path/to/virtualenv
<VirtualHost *:80>
WSGIScriptAlias / /srv/myproject/myproject/wsgi.py
...
</VirtualHost>

wsgi.py ,我補充道

sys.path.insert(1, "/path/to/virtualenv/lib/python3.4/site-packages")

問題是,當我嘗試在瀏覽器中打開應用程序時,它會無限期地加載。 這是Apache錯誤日志:

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
[Mon Jun 30 17:37:28 2014] [notice] child pid 19370 exit signal Aborted (6)
[Mon Jun 30 17:37:28 2014] [notice] child pid 19371 exit signal Aborted (6)
...
[Mon Jun 30 17:37:28 2014] [notice] child pid 19377 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

有趣的是,在Python(2.6)的系統安裝和虛擬環境版本(3.4)中, import encodings工作正常! 我已經嘗試使用mod_wsgi CheckingYourInstallation頁面中的示例WSGI腳本來確認Apache正在使用哪個版本的Python,但是我得到了相同的ImportError。

有沒有人對后續步驟有什么建議? 我已經搜過了文檔,但我不知道從哪里開始。

所以在我朋友的幫助下(IE:SysAdmins),我們昨晚得到了這個。 我通過示例最好地學習,所以讓我們假設您使用mod_wsgi運行Apache作為Linux組apache並使用用戶flipperpa。 假設您在my_project / wsgi.py中使用wsgi.py托管/ home / my_project。

在頂層(ls -l /):

drwxr-xr-x.  47 root root  4096 Jul  9 09:43 home

在主目錄(ls -l / home)中:

drwxrwsr-x   7 flipper        apache 4096 Jul 29 10:22 my_project

這是關鍵。 小寫“s”表示設置了apache組的setgid位,並設置了執行位。 當然,最后的“x”意味着任何人都可以執行。

檢查樹下的權限; 這對我們起了作用。

這個問題是由SELinux引起的。 我做的是這樣的:

  • 將項目文件移動到/ home / admin / myproject
  • 在FlipperPA的答案中更改了權限
  • 而關鍵的是,我認為,設置httpd_enable_homedirs中的SELinux到on使用setsebool -P httpd_enable_homedirs on

CentOS wiki上的SELinux指南非常有用。 例如,要調試,您可以運行setenforce Permissive因此SELinux不會強制執行其規則,但仍會寫入日志。 請記住之后再次setenforce Enforcing

在Windows上,我通過將Python安裝的Lib目錄直接放在PYTHONPATH上解決了這個問題。 這是必要的,即使你在同一個shell中直接運行Python,它也不需要這個來找到編碼包:

set PYTHONPATH=C:\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\Lib;%PYTHONPATH%
c:\apache24\bin\httpd.exe

另外一定要使用正確的mod_wsgi版本。 我從非官方二進制文件頁面下載了我的,並選擇它來匹配Python(上例中的64位3.4)。 在上面更改為啟動Apache的.bat文件之后,我的網站可以正常使用Python。

注意我的Python版本WinPython是通過解壓縮安裝並將其放在C:驅動器中來安裝的。 因此,它沒有以通常的Windows方式安裝在注冊表中,這應該最小化注冊表交互注意事項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM