python2升python3踩坑纪
1.yum问题
在centos7.9中,yum和python2作为镜像源自带包本身互相依存,安装python3修改环境变量后会对yum运行产生极大影响,,所以想在centos中用python命令来指代python3比较困难。
理论来说,如果非要做这样的操作,步骤是:
在设置里更改解释器设置,即修改/usr/bin/yum文件,指定使用python2.7作为yum的解释器:
将第一行"#!/usr/bin/python" 改为 “#!/usr/bin/python2”
然而并没有完: 安装软件时又ERR0R 会出现另一种报错
问题与前面一个一样,因此解决办法也是一样的:
vim /usr/libexec/urlgrabber-ext-down
将第一行"#!/usr/bin/python" 改为 “#!/usr/bin/python2”
理论上到这就可以成功安装了,原理是python软链接是 python 3.6,yum 使用得还是原来的python2.7,但是试过了,忘记哪步出了问题,yum崩了。
此外还有两种办法。
一是升级yum,直接使用python3以上版本(网上说有,我没找到)
二是修改yum的解释器为旧版本python2.7,即将连接文件/usr/bin/python 软连接回 /usr/bin/python2.7版本,然后你的python3想要连接到python的梦想又破灭了。
2.python源码编译问题
python源码在编译的时候有众多问题,这里只提供一条建议
make clean
make && make install
3.SyntaxError: Missing parentheses in call to ‘print’. Did you mean print()
原因:python2.X版本与python3.X版本输出方式不同
那为什么会报这个错呢?对了!又是你的软连接把本应该连接的python3变成了python2!
处理方法是给python3增加临时的环境变量,并取消python2的软连接(用mv,复制成.bak,不然就要重装python2了)
4.TypeError: Object type <class ‘str‘> cannot be passed to C code
Windows使用Crypto.Cipher.AES加密需要传递bytes类型,而Linux传递str类型不会报错
复现方案
from Crypto.Cipher import AES
AES.new(b'zXP5GtJMmtpw2pIiwCpFcg7L', AES.MODE_CFB, b'zXP5GtJMmtpw2pIi').encrypt("plaintext".encode())
AES.new(b'zXP5GtJMmtpw2pIiwCpFcg7L', AES.MODE_CFB, b'zXP5GtJMmtpw2pIi').encrypt("plaintext")
归根到底是因为使用的不是同一个 AES类,windows报这个错时候记得加.encode()
5.编译安装的时候Failed to import the site module
编译的时候去掉–enable-optimizations选项
6.ImportError: No module named site
这是gcc把python3当成python2拿来了,把环境变量改回去
site 是 Python 自带模块, 报错是因为配置路径有问题。
通过命令 python -v 可以看出来:
ImportError: No module named site
clear __builtin__._
clear sys.path
clear sys.argv
clear sys.ps1
看起来导入 site 时, 还需要预编译的 site.pyc 文件。基本认定是模块路径问题, 导致找不到导入库模块。
解决:
查看 Python 路径相关环境变量的意义。
PYTHONHOME: 指定 Python 的标准库。
PYTHONPATH: 用于扩充 Python 查找模块文件的路径。
这里是导入 site 模块时出错, 所以应该在 PYTHONPATH 上增加 site 模块的路径:
export PYTHONHOME=/opt/python27
export PYTHONPATH=$PYTHONPATH:/opt/python27/Lib
7.Python build finished, but the necessary bits to build these modules were not found:
_bsddb _tkinter bsddb185
dl imageop sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.
出现上述现象是因为编译的时候,系统没有办法找到对应的模块信息,为了解决这些报错,我们就需要提前安装依赖包,这些依赖包对应列表:
8.error: db5 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages index using db5 - Resource temporarily unavailable (11)
error: cannot open Packages database in /var/lib/rpm
CRITICAL:yum.main:
原因 :
yum的数据库缓存没了 重新建立
解决:
rpmdb --rebuilddb
9.ImportError: No module named _struct
好像是python2编译通病,bug详见:https://bugs.python.org/issue28444,建议直接用rpm装python2,不要自行编译
10.Fatal Python error: Py_Initialize: Unable to get the locale encoding … SyntaxError: invalid syntax Aborted (core dumped)
同样的软连接问题
执行
unset PYTHONPATH
11.File “/usr/local/lib/python3.12/site.py”, line 94
该用2的时候用了3,重新配一下PYTHONPATH
#12.No module named _ssl
guricorn遇到的问题,搜了一下都说要执行
sudo yum install -y gcc openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel sqlite-devel
[root@server01 Python-3.12.7]# yum install -y openssl
File "/bin/yum", line 30
except KeyboardInterrupt, e:
^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
[root@server01 Python-3.12.7]# pip install ssl
WARNING: Disabling truststore since ssl support is missing
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting ssl
Downloading http://mirrors.aliyun.com/pypi/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz (33 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [22 lines of output]
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-jsrgv6yc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 333, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-jsrgv6yc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 303, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-jsrgv6yc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 521, in run_setup
super().run_setup(setup_script=setup_script)
File "/tmp/pip-build-env-jsrgv6yc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 319, in run_setup
exec(code, locals())
File "<string>", line 33
print 'looking for', f
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
WARNING: Disabling truststore since ssl support is missing
[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: pip install --upgrade pip
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
但是没用,python3.7.12和openssl有些许孽缘,装了也没用,还有说libressl替代openssl的,一样没用,还把我python2搞崩了。
[root@server01 src]# yum install libressl
File "/usr/local/lib/python3.12/site.py", line 94
print(message, file=sys.stderr)
^
SyntaxError: invalid syntax
[root@server01 src]# vim /usr/bin/yum
[root@server01 src]# yum install libressl
File "/bin/yum", line 30
except KeyboardInterrupt, e:
^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
[root@server01 src]# vim /usr/bin/yum
[root@server01 src]# yum install libressl
File "/usr/local/lib/python3.12/site.py", line 94
print(message, file=sys.stderr)
^
SyntaxError: invalid syntax
建议是降级python,yum装一下3.6刚刚好
13.yum崩了
[root@server01 lib64]# yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
修复过程
#删掉新装的python3
sudo rm -rf /usr/local/bin/python3*
sudo rm -rf /usr/local/lib/python3*
sudo rm -rf /usr/local/include/python3*
sudo rm -rf /usr/local/bin/pip3*
sudo rm -rf /usr/local/lib/python3*/site-packages
# 下载并安装 Python 2 及相关依赖包
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-libs-2.7.5-89.el7.x86_64.rpm
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-devel-2.7.5-89.el7.x86_64.rpm
# 下载 yum 的核心包和依赖
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm
#强装python
sudo rpm -Uvh --replacepkgs --force python-2.7.5-89.el7.x86_64.rpm python-libs-2.7.5-89.el7.x86_64.rpm python-devel-2.7.5-89.el7.x86_64.rpm
sudo rpm -e --nodeps python-urlgrabber
#强装yum
sudo rpm -Uvh --replacepkgs yum-3.4.3-167.el7.centos.noarch.rpm yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm yum-metadata-parser-1.1.4-10.el7.x86_64.rpm python-urlgrabber-3.10-10.el7.noarch.rpm
sudo rpm -e --nodeps mlnx_en-4.4-2.0.7.0.gee7aa0e.kver.3.18.6_2.el7.centos.x86_64.x86_64
#修复path
# 将 /usr/bin 置于 PATH 优先位置,确保使用 /usr/bin/python2.7
export PATH="/usr/bin:$PATH"
# 设置 PYTHONHOME 为 Python 2 的主目录
export PYTHONHOME="/usr"
# 设置 PYTHONPATH 指向 Python 2 的标准库目录
export PYTHONPATH="/usr/lib/python2.7:/usr/lib64/python2.7:/usr/lib/python2.7/site-packages"
#python3安装
yum install python3
#python3的软连接
sudo cp /usr/local/lib/python3.12/bin/python3.12 /usr/local/bin/
sudo cp /usr/local/lib/python3.12/bin/pip3.12 /usr/local/bin/
sudo ln -sf /usr/local/bin/python3.12 /usr/local/bin/python3
sudo ln -sf /usr/local/bin/pip3.12 /usr/local/bin/pip3