当前位置: 首页 > article >正文

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

http://www.kler.cn/a/372975.html

相关文章:

  • 使用python提取日志里面的role_id、vip字段的值,(vip字段可能为空或者缺失,此时需要给默认值0):
  • 数据库 示例解析
  • 大模型面试题63题(1-11)
  • 基于Matlab 火焰识别技术
  • 第三百零一节 Lucene教程 - Lucene索引文件
  • 华为云开源项目Sermant正式成为CNCF官方项目
  • C++设计模式创建型模式———原型模式
  • Java如何实现PDF转高质量图片
  • 400元左右的头戴式耳机什么牌子好用?2024推荐四款好用高性价比型号
  • wordpress的functions文件非常重要 一定要懂它
  • JavaScript 超大量if-else判断如何优化(设计思路)
  • C语言——八股文(笔试面试题)
  • 大模型中的token是什么;常见大语言模型的 token 情况
  • 基于 Gitee AI数据集实现弹幕不遮挡人像滚动
  • Leetcode224 -- 基本计算器及其拓展
  • Spring学习笔记_17——@Primary
  • 基于python的语音识别与蓝牙通信的温控系统毕设项目
  • 医学数据分析中的偏特征图可视化
  • 请详细介绍python三大神器:迭代器、生成器、装饰器
  • 数据结构练习题(链表)
  • 2024双11买什么东西比较好?双十一购物清单
  • 全面解读京东商品详情 API 接口:从功能到应用场景
  • 从0学习React(6)
  • k8s 1.28.2 集群部署 Thanos 对接 MinIO 实现 Prometheus 数据长期存储
  • GO语言微服务 服务注册与服务发现平台 - Nacos go sdk
  • 通过route访问Openshift上的HTTP request报错504 Gateway Time-out【已解决】