在使用 python3 過度的過程中總是會出現很多問題,這里慢慢收集記錄,如有錯誤歡迎指正。
安裝問題
Lunix 系統一般默認都是 python2.7.5 升級到 Python3.x 版本一般都需要通過編譯安裝。這里主要記錄下編譯安裝需要依賴的包,我們需要先安裝。
yum groupinstall 'Development Tools'
yum install zlib-devel bzip2-devel openssl-devel ncurese-devel
Development
套件里面安裝的工具較多,包括 git
等
安裝完成后,除了常規的軟鏈接之外,需要修改 /usr/bin/yum 的 python 路徑,目前都是基於 python2 的,不修改使用 yum 安裝的時候就會各種報錯。
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 路徑的文件基本都需要修改,特別是遇到類似報錯:
File "urlgrabber-ext-down.py", line 28 except InvalidUserPass, e: ^ SyntaxError: invalid syntax
基本就是 python 路徑的問題,例如 yum 安裝過程中的 urlgrabber-ext-down.py 這個文件。
交互式命令行問題
進入交互式命令行之后,當輸入上下左右方向鍵等不能實現其應有的功能,而是打印出 ^[[A
^[[B
等。
查找資料可以安裝 readline 解決,並且不能使用 pip 安裝,需要使用 easy_install readline
安裝,但是安裝的時候會報錯:
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
這個工具,安裝好之后還出出現報錯:
$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 來安裝。
最后解決通過系統安裝包安裝 readline-deve 這個工具,然后重新編譯安裝 Python3
yum -y install readline-devel
./configure --prefix=/usr/local/python3 --enable-loadable-readline-deve
make && make install
sqlite3 模塊問題
很多應用都是使用的 sqlite 數據庫,但是安裝了 python3 之后會發現怎么找不到這個模塊了,類似報錯:
>>> 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
這個是由於編譯安裝時沒有安裝 sqlite3 的拓展,可以先安裝 sqlite-devel 或者 libsqlite3-dev,然后重新編譯安裝 python3,注意到編譯的時候有具體說明
If you want a release build with all optimizations active (LTO, PGO, etc),
please run ./configure --enable-optimizations
所以再次編譯的時候建議加上 --enable-loadable-sqlite-extensions
yun install sqlite-devel
./configure --prefix=/usr/local/python3 --enable-loadable-sqlite-extensions
make && make install
uwsgi 的問題
首先 uwsgi 可以在 python2 和 python3 中同時存在,所以安裝的時候可以使用
python2 -m pip install uwsgi
python3 -m pip install uwsgi
來安裝,安裝完成之后,可以使用 whereis uwsgi
來查看命令所在位置,我安裝(阿里雲的鏡像)后看到3個位置都有
/usr/sbin/uwsgi
/usr/bin/uwsgi
/usr/local/python3/bin/uwsgi #python3安裝路徑
比較坑爹的是這具有三個是完全不同的,由於 $PATH
路徑,默認是第一個,但是這個的 plugins 是不全的,執行基本的命令都會報錯
[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 ---
可以和正常的做對比
[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 運行 python3 的應用,客戶端會包 500 的錯誤。瀏覽器顯示:Internal Server Error
命名行可以看到錯誤如下
--- no python application found, check your startup logs for errors ---
當然運行 logs 中有對應的 Python version
作者:風Boy
鏈接:https://www.jianshu.com/p/e2fc97b452de
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。