Linux下安装Python3 和 pip: command not found



现Linux自带Python版本基本为2.x,如何升级为3.x?

Centos

安装python3
1.准备编译环境

yum groupinstall 'Development Tools' 
yum install zlib-devel bzip2-devel openssl-devel ncurese-devel 

2. 下载python3.5包

wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tar.xz

3. 解压,编译

tar Jxvf Python-3.5.1.tar.xz
cd Python-3.5.1
./configure --prefix=/usr/local/python3
make && make install

这里注意这个 –prefix选项,将python3安装在/usr/local/python3目录下,而之前下载的压缩包和安装包就都可以直接删除了。
4. 更换系统默认的python和pip版本
备份系统旧的python版本

mv /usr/bin/python /usr/bin/python2.6

建立指向新python3和pip3的软链接

ln -s /usr/local/python3/bin/python3.5 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

之前出错就是把pip的软链接路径写错导致找不到pip3

检查python和pip版本

python -V
pip -V

5. 更新yum相关设置
因yum依赖python2,故修改文件

vim /usr/bin/yum

将第一行改为

#!/usr/bin/python2.6
pip: command not found
出现这个的原因一般有两个:
1. 未安装pip
2. pip安装了,但是没有配置$PATH环境变量

如果是第二个原因,此时echo $PATH 查看pip的安装目录是否在PATH中,如果没有,在~/.bash_profile中添加export PATH=$PATH:/usr/local/bin(假设pip的安装目录为/usr/local/bin)然后source ~/.bash_profile使之生效。

Ubuntu

安装python3

1.准备编译环境

sudo apt-get -y update 
sudo apt install -y libssl-dev
./configure
make
sudo make install
sudo apt install -y zlib1g-dev libffi-dev 

2. 下载python3.7包

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz && \

3. 解压,编译

tar -xvf Python-3.7.1.tar.xz && \
cd Python-3.7.1 && \
./configure && make && sudo make altinstall

4.安装Python编译外部模块文件使用的Python-dev

sudo apt-get install python-dev

5.安装包管理PIP

sudo apt-get install python-pip

6.升级PIP

sudo pip3 install --upgrade pip

Debian

安装python3

1.准备编译环境

sudo apt-get -y update 
sudo apt-get install build-essential -y
sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev -y
sudo apt-get install libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev -y
sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev -y
sudo apt-get install libssl-dev openssl -y

2. 下载python3.5包

wget --no-check-certificate https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

3. 解压,编译

tar xzvf Python-3.5.1.tgz
cd Python-3.5.1
./configure --prefix=/usr/local/python35
make all
sudo make install
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注