在使用 python3 過度的過程中總是會(huì)出現(xiàn)很多問題,這里慢慢收集記錄,如有錯(cuò)誤歡迎指正。
安裝問題
Lunix 系統(tǒng)一般默認(rèn)都是 python2.7.5 升級(jí)到 Python3.x 版本一般都需要通過編譯安裝。這里主要記錄下編譯安裝需要依賴的包,我們需要先安裝。
yum groupinstall 'Development Tools'
yum install zlib-devel bzip2-devel openssl-devel ncurese-devel
Development 套件里面安裝的工具較多,包括 git 等
安裝完成后,除了常規(guī)的軟鏈接之外,需要修改 /usr/bin/yum 的 python 路徑,目前都是基于 python2 的,不修改使用 yum 安裝的時(shí)候就會(huì)各種報(bào)錯(cuò)。
ln -s /path/to/python3/bin/python3 /usr/bin/python
ln -s /path/to/python3/bin/pip3 /usr/bin/pip
#!/usr/bin/python2.7
不僅僅是 yum 命令需要修改路徑,很多配置了 python 路徑的文件基本都需要修改,特別是遇到類似報(bào)錯(cuò):
File "urlgrabber-ext-down.py", line 28
except InvalidUserPass, e:
^
SyntaxError: invalid syntax
基本就是 python 路徑的問題,例如 yum 安裝過程中的 urlgrabber-ext-down.py 這個(gè)文件。
交互式命令行問題
進(jìn)入交互式命令行之后,當(dāng)輸入上下左右方向鍵等不能實(shí)現(xiàn)其應(yīng)有的功能,而是打印出 ^[[A ^[[B等。
查找資料可以安裝 readline 解決,并且不能使用 pip 安裝,需要使用 easy_install readline 安裝,但是安裝的時(shí)候會(huì)報(bào)錯(cuò):
pkg_resources.VersionConflict: (setuptools 33.1.1 (/usr/local/python3/lib/python3.6/site-packages/setuptools-33.1.1-py3.6.egg), Requirement.parse('setuptools==0.9.8'))
During handling of the above exception, another exception occurred:
...
...
pkg_resources.DistributionNotFound: The 'setuptools==0.9.8' distribution was not found and is required by the application
需要 setuptools==0.9.8 這個(gè)工具,安裝好之后還出出現(xiàn)報(bào)錯(cuò):
$sudo pip install setuptools==0.9.8
Collecting setuptools==0.9.8
...
...
Installing collected packages: setuptools
Found existing installation: setuptools 33.1.1
Uninstalling setuptools-33.1.1:
Successfully uninstalled setuptools-33.1.1
Successfully installed setuptools-0.9.8
$ sudo easy_install readline
...
...
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
最終放棄了通過 python3 的 pip 或者 easy_install 來(lái)安裝。
最后解決通過系統(tǒng)安裝包安裝 readline-deve 這個(gè)工具,然后重新編譯安裝 Python3
yum -y install readline-devel
./configure --prefix=/usr/local/python3 --enable-loadable-readline-deve
make && make install
sqlite3 模塊問題
很多應(yīng)用都是使用的 sqlite 數(shù)據(jù)庫(kù),但是安裝了 python3 之后會(huì)發(fā)現(xiàn)怎么找不到這個(gè)模塊了,類似報(bào)錯(cuò):
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.5/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/usr/local/lib/python2.5/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
這個(gè)是由于編譯安裝時(shí)沒有安裝 sqlite3 的拓展,可以先安裝 sqlite-devel 或者 libsqlite3-dev,然后重新編譯安裝 python3,注意到編譯的時(shí)候有具體說(shuō)明
If you want a release build with all optimizations active (LTO, PGO, etc),
please run ./configure --enable-optimizations
所以再次編譯的時(shí)候建議加上 --enable-loadable-sqlite-extensions
yun install sqlite-devel
./configure --prefix=/usr/local/python3 --enable-loadable-sqlite-extensions
make && make install
uwsgi 的問題
首先 uwsgi 可以在 python2 和 python3 中同時(shí)存在,所以安裝的時(shí)候可以使用
python2 -m pip install uwsgi
python3 -m pip install uwsgi
來(lái)安裝,安裝完成之后,可以使用 whereis uwsgi 來(lái)查看命令所在位置,我安裝(阿里云的鏡像)后看到3個(gè)位置都有
/usr/sbin/uwsgi
/usr/bin/uwsgi
/usr/local/python3/bin/uwsgi #python3安裝路徑
比較坑爹的是這具有三個(gè)是完全不同的,由于 $PATH 路徑,默認(rèn)是第一個(gè),但是這個(gè)的 plugins 是不全的,執(zhí)行基本的命令都會(huì)報(bào)錯(cuò)
[root@actual src]# uwsgi --http:8000 --chdir . --module kirr/wsgi.py
uwsgi: unrecognized option '--http:8000'
getopt_long() error
[root@actual src]# uwsgi --http :8000 --chdir . --module kirr/wsgi.py
uwsgi: option '--http' is ambiguous; possibilities: '--http-socket' '--https-socket-modifier2' '--https-socket-modifier1' '--https-socket' '--http-socket-modifier2' '--http-socket-modifier1'
getopt_long() error
[root@actual src]# uwsgi --http-socket :8000 --chdir . --module kirr/wsgi.py
uwsgi: unrecognized option '--module'
getopt_long() error
查詢文檔是由于 plugins 沒有安裝或者沒有加載的原因
[root@actual local]# uwsgi --plugins-list
*** uWSGI loaded generic plugins ***
corerouter
*** uWSGI loaded request plugins ***
100: ping
101: echo
--- end of plugins list ---
可以和正常的做對(duì)比
[root@dev ~]# uwsgi --plugin-list
*** uWSGI loaded generic plugins ***
gevent
nagios
rrdtool
carbon
corerouter
fastrouter
http
ugreen
syslog
rsyslog
logsocket
router_uwsgi
router_redirect
router_basicauth
zergpool
redislog
mongodblog
router_rewrite
router_http
logfile
router_cache
rawrouter
router_static
sslrouter
cheaper_busyness
transformation_tofile
transformation_gzip
transformation_chunked
transformation_offload
router_memcached
router_redis
router_hash
router_expires
router_metrics
transformation_template
stats_pusher_socket
*** uWSGI loaded request plugins ***
0: python
17: spooler
18: symcall
100: ping
110: signal
111: cache
173: rpc
--- end of plugins list ---
另外還需要注意 python2 版本的問題,如果使用基于 python2 版本的 uwsgi 運(yùn)行 python3 的應(yīng)用,客戶端會(huì)包 500 的錯(cuò)誤。瀏覽器顯示:Internal Server Error
命名行可以看到錯(cuò)誤如下
--- no python application found, check your startup logs for errors ---
當(dāng)然運(yùn)行 logs 中有對(duì)應(yīng)的 Python version
具體安裝使用方法參考官網(wǎng)的使用 uWSGI 和 nginx 來(lái)設(shè)置 Django 和你的 web 服務(wù)器