您的位置:首页 > 编程语言 > Python开发

Openwrt下安装Python3.5运行出现异常:Fatal Python error: Py_Initialize: Unable to get the locale encoding

2015-10-22 13:39 447 查看
环境说明:

Openwrt + Python 3.5

依赖的库:

libbz2, expat, libopenssl, libpthread, zlib, libffi, libcrypto, libncurses, libpanel

使用说明:

之前已经安装过python2.7.3,运行很正常。后来需要使用Python3版本,所以需要升级。

经过一番移植、修改,终于安装成功了。结果运行在设备端运行python3命令时出现如下异常:

>>>:/# python3
Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/usr/lib/python3.5/encodings/__init__.py", line 34
_cache = {}
^
SyntaxError: invalid syntax

Current thread 0x2aab0310 (most recent call first):
Aborted


提示:无法找到locale encoding,解析语法错误

分析:

这个错误的原因如下:由于是交叉编译,在make过程中需要使用pgen和python文件,这时不能使用target环境中的pgen和python,而只能使用host环境中的pgen和python(这两个文件是在Host Install时安装的)。在Configure阶段,是在Makefile中,有从编译环境中拷贝文件的操作:

define Host/Install
$(INSTALL_DIR) $(STAGING_DIR_HOST)/bin/
$(MAKE) -C $(HOST_BUILD_DIR) HOSTPYTHON=$(HOST_BUILD_DIR)/python install
$(INSTALL_BIN) $(HOST_BUILD_DIR)/Parser/pgen $(STAGING_DIR_HOST)/bin/pgen3
$(INSTALL_BIN) $(HOST_BUILD_DIR)/Programs/_freeze_importlib $(STAGING_DIR_HOST)/bin/_freeze_importlib
endef
注意:此时在编译环境中安装的文件时pgen3。

define Build/Configure
-$(MAKE) -C $(PKG_BUILD_DIR) distclean
(cd $(PKG_BUILD_DIR); autoreconf --force --install || exit 0)
# The python executable needs to stay in the rootdir since its location will
# be used to compute the path of the config files.
@echo "alantest:STAGING_DIR_HOST=$(STAGING_DIR_HOST)"
$(CP) $(STAGING_DIR_HOST)/bin/<span style="color:#ff0000;">pgen </span>$(PKG_BUILD_DIR)/hostpgen
$(CP) $(STAGING_DIR_HOST)/bin/python$(PYTHON_VERSION) $(PKG_BUILD_DIR)/hostpython</span>
$(call Build/Configure/Default,$(CONFIGURE_ARGS))
endef
注意:此时从编译环境中拷贝的文件时pgen。

由于之前使用python2.7.3时,已经在$(STAGING_DIR_HOST)/bin/目录下安装了pgen和python文件,升级到python3.5时,就会拷贝python2.7.3的pgen文件。

而python2和python3版本存在兼容性问题,所有会出现如题所示的解析错误。

解决方案:

define Build/Configure
-$(MAKE) -C $(PKG_BUILD_DIR) distclean
(cd $(PKG_BUILD_DIR); autoreconf --force --install || exit 0)
# The python executable needs to stay in the rootdir since its location will
# be used to compute the path of the config files.
@echo "alantest:STAGING_DIR_HOST=$(STAGING_DIR_HOST)"
$(CP) $(STAGING_DIR_HOST)/bin/pgen3 $(PKG_BUILD_DIR)/hostpgen
$(CP) $(STAGING_DIR_HOST)/bin/python$(PYTHON_VERSION) $(PKG_BUILD_DIR)/hostpython</span>
$(call Build/Configure/Default,$(CONFIGURE_ARGS))
endef
注意:此时从编译环境中拷贝的文件时pgen3。

问题解决,可以再target端执行python3了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: