python3.x 和pip3的安装

时间:2022-11-30 20:26:34

python3.x 和pip3的安装

本人在学习python3的时候,视频中使用的是python3,在讲解到有些第三方库的时候,无法使用到pip3 install来安装所需的库。由于系统是centos6.x,自带的python是2.6.x版本的。
于是,参考网上的相关资料,结合实际,将详细步骤记录下来,供参考。

一、下载安装python3.6.4

cd /usr/local/src
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar.xz
cd Python-3.6.4
#注意,这里需要安装依赖包,请参考centos系统安装后,常用的依赖包安装推荐:
# http://www.cnblogs.com/bjx2020/p/8074896.html
./configure --prefix=/usr/local/python3 --enable-optimizations
make -j `grep processor /proc/cpuinfo | wc -l`
make -j `grep processor /proc/cpuinfo | wc -l`  install

# 做个软连接或者将其添加到环境变量中
ln -s /usr/local/python3/bin/python3 /usr/bin/python3

# 测试一下
[root@db01 Python-3.6.4]# python3
Python 3.6.4 (default, Jan 25 2018, 06:55:49)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

二.、install pip for python3.x

1.首先安装setuptools

cd /usr/local/src/
wget --no-check-certificate https://pypi.python.org/packages/72/c2/c09362ab29338413ab687b47dab03bab4a792e2bbb727a1eb5e0a88e3b86/setuptools-39.0.1.zip#md5=75310b72ca0ab4e673bf7679f69d7a62
unzip setuptools-39.0.1.zip
cd setuptools-39.0.1
python3 setup.py build
python3 setup.py install

2.然后直接安装pip就可以了

cd /usr/local/src/
wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
python3 setup.py build
python3 setup.py install

# 安装pip-9.0.3.tar.gz,遇到坑了

3. 检查一下

# 查看python3目录下是否多了pip3
[root@db01 pip-8.0.2]#  ls /usr/local/python3/bin/
2to3              idle3    pip3.6    python3.6          python3-config
2to3-3.6          idle3.6  pydoc3    python3.6-config   pyvenv
easy_install      pip      pydoc3.6  python3.6m         pyvenv-3.6
easy_install-3.6  pip3     python3   python3.6m-config
[root@db01 pip-8.0.2]# 


# 这里还是添加到环境变量中好点
[root@db01 pip-9.0.3]# ll -d /usr/local/python3/bin/ 
drwxr-xr-x 2 root root 4096 Mar 27 20:58 /usr/local/python3/bin/
[root@db01 pip-9.0.3]# 

vim /root/.bash_profile 
# 追加在最后
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
source  /root/.bash_profile 

4.安装前的错误信息

# 在安装chardet模块前的错误信息
[root@db01 local]# python3
Python 3.6.4 (default, Jan 25 2018, 06:55:49) 
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import chardet
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'chardet'
>>> 

# 安装第三方库
[root@db01 pip-8.0.2]# pip3 install chardet
Collecting chardet
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 135kB 730kB/s 
Installing collected packages: chardet
Successfully installed chardet-3.0.4
You are using pip version 8.0.2, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@db01 pip-8.0.2]# 

# 这里不要执行 pip install --upgrade pip ,会遇到坑的。

5.再来检查一下

[root@db01 pip-8.0.2]# python3
Python 3.6.4 (default, Jan 25 2018, 06:55:49) 
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import chardet
>>> 

#不报错了,大功告成!