Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

1.2. Python 安装

1.2.1. Rocky Linux / AlmaLinux 安装 Python

系统默认 python3.9 安装 python3.13

		
[root@development ~]# dnf search python | grep "^python3.13"
Last metadata expiration check: 0:15:05 ago on Thu 24 Jul 2025 08:43:36 AM CST.
python3.13.x86_64 : Version 3.13 of the Python interpreter
python3.13-debug.x86_64 : Debug version of the Python runtime
python3.13-devel.x86_64 : Libraries and header files needed for Python development
python3.13-freethreading.x86_64 : Free Threading (PEP 703) version of the Python runtime
python3.13-freethreading-debug.x86_64 : Free Threading (PEP 703) version of the Python runtime (debug build)
python3.13-idle.x86_64 : A basic graphical development environment for Python
python3.13-libs.x86_64 : Python runtime libraries
python3.13-packaging.noarch : Core utilities for Python packages
python3.13-pip.noarch : A tool for installing and managing Python packages
python3.13-rpm.x86_64 : Python 3.13 bindings for apps which will manipulate RPM packages
python3.13-setuptools.noarch : Easily build and distribute Python packages
python3.13-test.x86_64 : The self-test suite for the main python3 package
python3.13-tkinter.x86_64 : A GUI toolkit for Python
python3.13-flit-core.noarch : PEP 517 build backend for packages using Flit
python3.13-pip-wheel.noarch : The pip wheel
python3.13-setuptools-wheel.noarch : The setuptools wheel
		
		

		
[root@development ~]# dnf install python3.13 python3.13-pip
		
		

切换默认版本

		
[root@development ~]# ls -l /usr/bin/python{,3} 
lrwxrwxrwx 1 root root 9 2025-07-02 08:44 /usr/bin/python -> ./python3
lrwxrwxrwx 1 root root 9 2025-07-02 08:44 /usr/bin/python3 -> python3.9		
		
		

手工修改,创建符号链接,因为 python 指向 python3,所以我门只处理 python3 即可

		
# 先删除现有链接
sudo rm -f /usr/bin/python3

# 创建指向 Python 3.13 的新链接
sudo ln -s /usr/bin/python3.13 /usr/bin/python3

# 验证版本
python3 --version
		
		

alternatives 方案

		
# 查看当前 Python 方案
sudo alternatives --config python3

# 如果没有 Python 3.13 的选项,手动添加
sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.13 2
sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1

# 再次配置默认版本
sudo alternatives --config python3
# 然后输入对应 Python 3.13 的编号并回车		
		
		

推荐安装

		
[root@development ~]# dnf install python3.13-setuptools python3.13-freethreading		
		
		

1.2.2. Docker 安装

		
$ docker run --name python --rm -it python
Python 3.10.1 (main, Dec 21 2021, 09:01:08) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>		
		
		

Dockerfile 制作自己的镜像

		
FROM python:latest

MAINTAINER Netkiller <netkiller@msn.com>

RUN pip install pymysql sqlalchemy openpyxl pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN mkdir /data
ADD export.py /srv
VOLUME ["/tmp"]
WORKDIR /data
#EXPOSE 80 443
ENTRYPOINT python3 /srv/export.py
		
		

1.2.3. dnf 安装 python3.11

安装 Python 3.11 适用鱼 Rocky Linux 9.2 和 AlmaLinux 9.2

		
dnf install -y python3.11 python3.11-pip		
		
		

系统默认是 python3.9,将其切换到 python3.11

		

update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
		
		

切换 pip 到 3.11

		
[root@iZwz9cug5b7jbx1dc4nwniZ ~]# mv /usr/bin/pip{,.backup}
[root@iZwz9cug5b7jbx1dc4nwniZ ~]# mv /usr/bin/pip3{,.backup}
[root@iZwz9cug5b7jbx1dc4nwniZ ~]# alternatives --install /usr/bin/pip pip /usr/bin/pip3.11 1
[root@iZwz9cug5b7jbx1dc4nwniZ ~]# alternatives --install /usr/bin/pip3 pip3 /usr/bin/pip3.11 2
[root@iZwz9cug5b7jbx1dc4nwniZ ~]# pip -V
pip 22.3.1 from /usr/lib/python3.11/site-packages/pip (python 3.11)
		
		

检查 python 是否工作正常

		
[root@iZwz9cug5b7jbx1dc4nwniZ srv]# alternatives --list
libnssckbi.so.x86_64  	auto  	/usr/lib64/pkcs11/p11-kit-trust.so
soelim                	auto  	/usr/bin/soelim.groff
cifs-idmap-plugin     	auto  	/usr/lib64/cifs-utils/cifs_idmap_sss.so
iptables              	auto  	/usr/sbin/iptables-nft
ebtables              	auto  	/usr/sbin/ebtables-nft
arptables             	auto  	/usr/sbin/arptables-nft
ld                    	auto  	/usr/bin/ld.bfd
man                   	auto  	/usr/bin/man.man-db
nc                    	auto  	/usr/bin/ncat
man.7.gz              	auto  	/usr/share/man/man7/man.man-pages.7.gz
libwbclient.so.0.15-64	auto  	/usr/lib64/samba/wbclient/libwbclient.so.0.15
python                	auto  	/usr/bin/python3.11

[root@iZwz9cug5b7jbx1dc4nwniZ srv]# alternatives --display python
python - status is auto.
 link currently points to /usr/bin/python3.11
/usr/bin/python3.11 - priority 1
Current `best' version is /usr/bin/python3.11.

[root@iZwz9cug5b7jbx1dc4nwniZ srv]# python
Python 3.11.2 (main, May 24 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
		
		

1.2.4. 编译安装 Python3.x

			
./configure --prefix=/usr/local/python-3.1.2
make && make install
			
		

1.2.5. Ubunut 13.04 环境安装python3

$ sudo apt-get install python3
			

python 3.3

$ sudo apt-get install python3.3
			

1.2.6. pypy - fast alternative implementation of Python - PyPy interpreter

http://pypy.org/

1.2.6.1. Ubuntu 环境安装

		
$ apt-cache search pypy | grep pypy
pypy - fast alternative implementation of Python - PyPy interpreter
pypy-dev - header files for PyPy (an alternative Python interpreter)
pypy-doc - developer Documentation for PyPy (an alternative Python interpreter)
pypy-lib - standard library for PyPy (an alternative Python interpreter)
python-pypy.translator.sandbox - sandboxed PyPy interpreter
		
			
		
$ sudo apt-get install pypy
		
			

1.2.6.2. CentOS 环境安装

		
# yum install pypy
		
			

1.2.7. Eric Python IDE

apt-get install eric
		

1.2.8. python to exe

1.2.8.1. pyinstaller

http://www.pyinstaller.org/

Linux

安装

pip install pyinstaller
			

制作EXE文件

$ pyinstaller test.py
			

dist目录下会生成可执行文件

1.2.8.2. py2exe

http://www.py2exe.org/

此软件 2008-11-16 后不再更新

1.2.9. Python2.x

1.2.9.1. 编译安装

Python2.x

wget http://www.python.org/ftp/python/2.x.x/Python-2.x.x.tgz
tar zxvf Python-2.x.x.tgz
cd Python-2.x.x
./configure --prefix=/usr/local/python2.x
make
make install

ln -s /usr/local/python/bin/python2.5 /usr/bin/
ln -s /usr/local/python/bin/* /usr/local/bin/
			

1.2.9.2. Ubuntu 安装

sudo apt-get install python
sudo apt-get install python-setuptools