python更换版本

简介: python更换版本

python更换版本

windows用安装包卸载python

查看你的python版本

然后去这里查找https://www.python.org/downloads/windows/

下载安装包里面有卸载的功能

linux卸载旧版本python(如果是linux自带的python,直接下载新版python即可)

卸载python3的方法https://www.cnblogs.com/bxhsdy/p/13298621.html

# 1、卸载python3
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps 
# 2、 删除所有残余文件
whereis python3 |xargs rm -frv
find / -type d -name "python3"

windows安装

把安装包留着,卸载会用到

linux安装python3.10

下载python3.10和需要的包

自带的python版本

https://www.python.org/

右键复制链接即可

wget https://www.python.org/ftp/python/3.10.12/Python-3.10.12.tgz

下载的慢就用淘宝的源,其他版本直接换版本号就好

wget https://npm.taobao.org/mirrors/python/3.10.12/Python-3.10.12.tar.xz

解压

[root@localhost develop]# ls
Python-3.10.12.tar.xz
[root@localhost develop]# tar -xf Python-3.10.12.tar.xz
[root@localhost develop]# ls
Python-3.10.12  Python-3.10.12.tar.xz

安装python相关的包

yum install -y gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

升级OpenSSL

在后续安装某些软件时(如Python3.10),要求更高版本的OpenSSL,需要手动升级OpenSSL。

yum默认的openssl好像是1.0.2

[root@localhost Python-3.10.12]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017
cd /opt
# 一,下载必要的依赖包,使用root权限:
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel
# 在主目录新建目录openssl:
mkdir openssl
cd openssl
# 下载最新版本的源码包:https://www.openssl.org/source/openssl-1.1.1q.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz
# 解压缩并进入目录:
tar xzf openssl-1.1.1q.tar.gz 
cd openssl-1.1.1q
# 编译安装:
./config --prefix=/usr/local/openssl
make && make install
# 默认配置如果你没指定安装路径,安装完之后可执行文件的位置可能在/usr/local/bin下
# 配置lib库:
# echo '/usr/lib64' >> /etc/ld.so.conf
# echo "/usr/local/lib64/" >> /etc/ld.so.conf
# ldconfig
# 备份旧版本openssl:
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old
# 将新版的openssl链接到/usr/bin:
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig
[root@localhost ~]# openssl version
OpenSSL 1.1.1q  5 Jul 2022

编译

说明:python3.6后部分安装包貌似没有--with-ssl命令了

python---3.10.12文档环境介绍

https://docs.python.org/zh-cn/3.10/using/configure.html

注意--with-openssl参数:看清楚是有bin文件夹的openssl路径!!!!!一定要先看一下,否则后面import ssl会失败

cd Python-3.10.12
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make && make install

尝试python3,发现可以使用,但是默认版本没变,

[root@localhost Python-3.10.12]# python -V
Python 2.7.5
[root@localhost Python-3.10.12]# python3 -V
Python 3.10.12
[root@localhost Python-3.10.12]# python3
Python 3.10.12 (main, Aug 22 2023, 18:12:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

这时测试一下ssl是否可用,不可用就几个地方

  • python3后面的高版本需要openssl高版本
  • python的环境变量编译前的是--with-ssl还是--with-openssl,高版本python是后者

设置环境变量和软连接---推荐每个项目都用自己的虚拟环境

添加环境变量

vim /etc/profile
#python
PATH=/usr/local/python3/bin:$PATH

刷新配置

source /etc/profile

在这个后面python3 -V就可以执行了

修改一些配置文件

因为之前的yum和urlgrabber-ext-down都是用的

#! /usr/bin/python

我们修改了python默认是python3,导致语法错误

修改

vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down

都是添加版本号,如果你python2.7可以调用,那么就是改成2.7,一般是python+tab查看最小版本

#! /usr/bin/python2.7

添加软连接

#这里我们将原先的python 改个名
[root@localhost bin]# mv /usr/bin/python /usr/bin/python.bak
[root@localhost bin]# ll | grep python
lrwxrwxrwx.   1 root root          9 8月  11 12:09 python2 -> python2.7
-rwxr-xr-x.   1 root root       7144 6月  20 19:37 python2.7
-rwxr-xr-x.   1 root root       1835 6月  20 19:36 python2.7-config
lrwxrwxrwx.   1 root root         16 8月  11 12:09 python2-config -> python2.7-config
lrwxrwxrwx.   1 root root          9 8月  11 11:37 python3 -> python3.6
-rwxr-xr-x.   2 root root      11336 6月  20 19:55 python3.6
-rwxr-xr-x.   2 root root      11336 6月  20 19:55 python3.6m
lrwxrwxrwx.   1 root root          7 8月  11 12:09 python.bak -> python2
lrwxrwxrwx.   1 root root         14 8月  11 12:09 python-config -> python2-config
#再创建软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

查看版本,成功

[root@localhost bin]# python -V
Python 3.10.12
[root@localhost bin]# python
Python 3.10.12 (main, Aug 11 2023, 12:12:20) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

还需要修改一个地方,否则你yum不能正常使用

因为默认的yum脚本执行的是python,但是你把版本更改了,他解析不了

vim /usr/bin/yum

把最上面的python后加上你的版本,在python后加上2.7,这个2.7是你系统默认的python版本,使用python+tab找最小的版本

#!/usr/bin/python

虚拟环境

windows下

python在window不能进入虚拟环境的问题

第一次需要在管理员权限下进入powershell执行策略更改,后面就可以直接进入虚拟环境了

Set-ExecutionPolicy RemoteSigned

关于该命令介绍https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.3

linux下

这里的python3要看自己的情况

当前目录下创建虚拟环境,这个类似git文件夹是用来管理的,到时候需要放在项目根路径下

python3 -m venv test-env
 source test-env/bin/activate

然后发现处于环境中

[root@localhost Python-3.10.12]# python3 -m venv test-env
[root@localhost Python-3.10.12]# source test-env/bin/activate
(tutorial-env) [root@localhost Python-3.10.12]# python -V
Python 3.10.12
(tutorial-env) [root@localhost Python-3.10.12]# python
Python 3.10.12 (main, Aug 11 2023, 17:12:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

退出环境的方法

deactivate

完整流程

[root@localhost stable-diffusion-webui-master]# ls
CHANGELOG.md  environment-wsl2.yaml  javascript     models          README.md                  screenshot.png  test                         webui.py
CODEOWNERS    extensions             launch.py      modules         requirements-test.txt      script.js       textual_inversion_templates  webui.sh
configs       extensions-builtin     LICENSE.txt    package.json    requirements.txt           scripts         webui.bat                    webui-user.bat
embeddings    html                   localizations  pyproject.toml  requirements_versions.txt  style.css       webui-macos-env.sh           webui-user.sh
[root@localhost stable-diffusion-webui-master]# python3 -m venv test-env
[root@localhost stable-diffusion-webui-master]# source test-env/bin/activate
(test-env) [root@localhost stable-diffusion-webui-master]# deactivate
[root@localhost stable-diffusion-webui-master]# ls
CHANGELOG.md           extensions          LICENSE.txt    pyproject.toml             screenshot.png  test-env                     webui.sh
CODEOWNERS             extensions-builtin  localizations  README.md                  script.js       textual_inversion_templates  webui-user.bat
configs                html                models         requirements-test.txt      scripts         webui.bat                    webui-user.sh
embeddings             javascript          modules        requirements.txt           style.css       webui-macos-env.sh
environment-wsl2.yaml  launch.py           package.json   requirements_versions.txt  test            webui.py

pip

添加源

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

查看当前镜像源

pip3 config list

删除源

pip config unset global.index-url
目录
相关文章
|
2月前
|
Linux 数据库管理 Python
CentOS7编译安装Python3.10(含OpenSSL1.1.1安装),创建虚拟环境,运行Django项目(含sqlite版本报错)
CentOS7编译安装Python3.10(含OpenSSL1.1.1安装),创建虚拟环境,运行Django项目(含sqlite版本报错)
368 4
|
2月前
|
Linux iOS开发 MacOS
Python如何版本切换
【6月更文挑战第14天】Python如何版本切换
38 2
|
10天前
|
存储 数据可视化 Python
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
|
15天前
|
Ubuntu Linux iOS开发
如何实现多个Python环境的Python版本切换
【8月更文挑战第4天】如何实现多个Python环境的Python版本切换
61 5
|
8天前
|
存储 数据库连接 数据库
【Python】python员工信息管理系统(数据库版本)(GUI界面+数据库文件+源码)【独一无二】
【Python】python员工信息管理系统(数据库版本)(GUI界面+数据库文件+源码)【独一无二】
|
15天前
|
算法 测试技术 API
【Leetcode刷题Python】278. 第一个错误的版本
本文提供了使用二分查找算法解决LeetCode "第一个错误的版本" 问题的Python实现,通过递归地缩小搜索范围来查找导致之后所有版本出错的第一个错误版本。
11 0
|
1月前
|
Linux iOS开发 MacOS
如何查看你的Python版本?
在命令行中查看Python版本很简单。在Windows上按Win+R,输入powershell;在macOS上通过Finder→Applications→Utilities→Terminal;在Linux上打开终端。然后输入`python --version`或`python -V`。输出显示如"Python 3.8.3"。使用`python -VV`可获取更多详细信息。在Python脚本中,可通过`sys.version`或`platform.python_version()`检查版本。确保使用Python 3,因为Python 2自2020年起已停止更新和支持。
|
1月前
|
iOS开发 MacOS Python
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
38 0
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
|
1月前
|
达摩院 语音技术 异构计算
语音识别-免费开源的语音转文本软件Whisper的本地搭建详细教程,python版本是3.805,ffmpeg是专门处理音视频的,ffmpeg的下载链接,现在要求安装python和ffmpeg
语音识别-免费开源的语音转文本软件Whisper的本地搭建详细教程,python版本是3.805,ffmpeg是专门处理音视频的,ffmpeg的下载链接,现在要求安装python和ffmpeg
|
2月前
|
Python
如何查询Python包的所有历史版本
如何查询Python包的所有历史版本
96 5