在Linux环境下配置Python,通常需要以下几个步骤:
安装Python:
apt
(Debian/Ubuntu)、yum
(CentOS/RHEL)等。# Debian/Ubuntu
sudo apt update
sudo apt install python3
# CentOS/RHEL
sudo yum install python3
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
tar xvf Python-3.9.6.tgz
cd Python-3.9.6
./configure --prefix=/usr/local
make
sudo make install
配置环境变量:
PATH
环境变量:export PATH=/usr/local/bin:$PATH
~/.bashrc
或~/.profile
文件中,以便每次登录时自动设置:echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
安装虚拟环境(可选但推荐):
sudo apt install python3-venv # Debian/Ubuntu
sudo yum install python3-venv # CentOS/RHEL
# 创建虚拟环境
python3 -m venv myenv
# 激活虚拟环境
source myenv/bin/activate
安装常用的Python库:
pip
(Python包管理器)安装常用的库,如requests
、numpy
等。pip3 install requests numpy
验证Python安装:
python3 --version
echo "print('Hello, World!')" > hello.py
python3 hello.py
通过以上步骤,你应该能够在Linux环境下成功配置Python环境。如果有任何问题,请随时提问!