python版本切换
在Linux下有时候需要使用多个不同版本的Python,然而在终端输入命令python时,不一定是所需要的版本。以下总结几种切换Python版本的方法。
安装最新python3.11
现在的最新版本是Python 3.10。你需要从源代码安装Python 3.11。
下面是安装步骤:
- 安装依赖项:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev liblzma-dev libffi-dev
- 下载源代码并解压缩:
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar -xf Python-3.11.0.tgz
cd Python-3.11.0
- 编译安装:
./configure --enable-shared --prefix=/usr/local LDFLAGS="-Wl,-rpath /usr/local/lib"
make -j4
sudo make altinstall
现在Python 3.11已经安装在你的树莓派上了。
你可以在终端中运行以下命令来确认安装是否成功:
python3.11 --version
1.安装库
1.1先安装必要库:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev
sudo apt-get install -y libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
sudo apt-get install -y libncurses5-dev libncursesw5-dev xz-utils tk-dev
1)下载文件:
https://www.python.org/ftp/python/
https://www.python.org/ftp/python/3.7.5/
2)用命令wget直接下载python3.7.5
wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz //直接在linux下下载省区了拖拽文件传输的繁琐步骤
3)用命令 tar -xzvf解压下载得到的压缩包
tar -zxvf Python-3.7.5.tgz
2. 安装文件
进入到解压缩后文件夹内,输入如下命令,这里会安装到指定的目录下
sudo ./configure --prefix=/home/pi/python3.7
依次执行下列命令
cd Python-3.7.5 //命令1,打开解压后的文件夹,回车结束
sudo ./configure --prefix=/home/pi/python3.7 //命令2,执行配置文件,回车结束
sudo make -j4
sudo make install //命令3,安装,回车结束
3.建立软连接,配置环境变量
~~```cpp
cd /usr/local/python3/bin/
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
echo ‘export PATH=/usr/local/python3/bin/:$PATH’ >>/etc/profile
. /etc/profile
```cpp
sudo ln -s /home/pi/python3.7/bin/python3 /usr/bin/python3
sudo ln -s /home/pi/python3.7/bin/pip3 /usr/bin/pip3
软连接连接异常:
查看现有的软连接:
pi@raspberrypi:~ $ cd /usr/bin
pi@raspberrypi:/usr/bin $ ls -al python
lrwxrwxrwx 1 root root 7 Mar 2 2021 python -> python3
报错信息如下:
ln: failed to create symbolic link ‘/usr/bin/python’: File exists
解决方法:
ln -sf 加一个 f 即可
ln -sf /usr/python/bin/python3(python 安装目录) /usr/bin/python
4.安装成功,查看python3版本以及运行python3
python3 --version
python3
使用python3.9 可以打开以前的python3.9版本
查看pip报错:
pi@raspberrypi:~ $ pip3 list
Traceback (most recent call last):
File "/home/pi/.local/bin/pip3", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip._internal.cli.main'
pi@raspberrypi:~ $ python -m pip install --upgrade pip
解决方案使用:
python -m pip install --upgrade pip更新pip
可能报错:
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
如下命令查找lsb_release.py
sudo find / -name 'lsb_release.py'
再执行如下命令:
sudo cp /usr/share/pyshared/lsb_release.py /home/pi/python3.7/lib/python3.7
此时pip3 list可以正常安装
python -m pip install --upgrade pip
pi@raspberrypi:~ $ python -m pip install --upgrade pip
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pip
Downloading https://files.pythonhosted.org/packages/96/2f/caec18213f6a67852f6997fb0673ae08d2e93d1b81573edb93ba4ef06970/pip-22.1.2-py3-none-any.whl (2.1MB)
|████████████████████████████████| 2.2MB 26kB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/home/pi/python3.7/bin/pip3'
Consider using the `--user` option or check the permissions.
WARNING: You are using pip version 19.2.3, however version 22.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pi@raspberrypi:~ $ python -m pip install --upgrade pip --user
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pip
Using cached https://files.pythonhosted.org/packages/96/2f/caec18213f6a67852f6997fb0673ae08d2e93d1b81573edb93ba4ef06970/pip-22.1.2-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-22.1.2
WARNING: You are using pip version 19.2.3, however version 22.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pi@raspberrypi:~ $ pip list
版本切换
方法1:打开具体的某个Python文件,在第一行处修改如下:将#!/usr/bin/python修改为#!/usr/bin/python2表示采用Python2来对该文件进行编译,改为python3亦然。
方法2:采用软链接的方式,指令如下:
$ whereis python
链接python命令链接
$ rm /usr/bin/python
$ ln -s /usr/bin/python3.9 /usr/bin/python // 修改软连接
链接pip3命令链接
rm /usr/bin/pip
sudo ln -s /usr/bin/pip3 /usr/bin/pip
参考链接:https://blog.csdn.net/jiugeshao/article/details/122649942
5. ModuleNotFoundError: No module named ‘apt_pkg’
更换完python版本之后使用virtualenv提示:
进入路径:/usr/lib/python3/dist-packages 下的文件 apt_pkg.cpython-39-aarch64-linux-gnu.so,文件名没有跟随 python 版本进行更改,正确做法应该是把文件名中的 39 更改为你更新后的 python 版本号,如我更新后的 python 版本为 python3.7 ,所以文件名应该更改为 apt_pkg.cpython-37-aarch64-linux-gnu.so 。
解决步骤:
1、 sudo apt-get remove --purge python-apt
2、 sudo apt-get install python-apt -f
3、 cd /usr/lib/python3/dist-packages/
4、 sudo cp apt_pkg.cpython-37m-x86_64-linux-gnu.so apt_pkg.cpython-36m-x86_64-linux-gnu.so