1:ubuntu16.04安装python3.7

ubuntu16.04下python3.7安装步骤可参考如下链接:
Ubuntu16.04安装Python3.7及其pip3并切换为默认版本

2:理解python,pip的调用原理

安装好python3.7之后,进入到/opt/python3.7/bin 目录下,执行python3.7 和 pip3.7 list 都显示:No such file or directory。
那是由于这2个命令都不位于环境变量路径中,将其添加到 /usr/bin/ 目录下即可调用

koping@koping-HP:/opt/python3.7$ ll
total 24
drwxr-xr-x 6 root root 4096 910 16:27 ./
drwxr-xr-x 4 root root 4096 910 16:27 ../
drwxr-xr-x 3 root root 4096 910 16:51 bin/
drwxr-xr-x 3 root root 4096 910 16:27 include/
drwxr-xr-x 4 root root 4096 910 16:27 lib/
drwxr-xr-x 3 root root 4096 910 16:27 share/
koping@koping-HP:/opt/python3.7$ cd bin/
koping@koping-HP:/opt/python3.7/bin$ ll
total 29200
drwxr-xr-x 3 root root     4096 910 16:51 ./
drwxr-xr-x 6 root root     4096 910 16:27 ../
lrwxrwxrwx 1 root root        8 910 16:27 2to3 -> 2to3-3.7*
-rwxr-xr-x 1 root root      105 910 16:27 2to3-3.7*
-rwxr-xr-x 1 root root      229 910 16:50 automat-visualize*
-rwxr-xr-x 1 root root      235 910 16:50 cftp*
-rwxr-xr-x 1 root root      238 910 16:50 ckeygen*
-rwxr-xr-x 1 root root      236 910 16:50 conch*
-rwxr-xr-x 1 root root      246 910 16:27 easy_install-3.7*
lrwxrwxrwx 1 root root        7 910 16:27 idle3 -> idle3.7*
-rwxr-xr-x 1 root root      103 910 16:27 idle3.7*
-rwxr-xr-x 1 root root     1707 910 16:50 jp.py*
-rwxr-xr-x 1 root root      238 910 16:50 mailmail*
-rwxr-xr-x 1 root root      237 910 16:50 pip*
-rwxr-xr-x 1 root root      237 910 16:50 pip3*
-rwxr-xr-x 1 root root      237 910 16:50 pip3.7*
drwxr-xr-x 2 root root     4096 910 16:50 __pycache__/
lrwxrwxrwx 1 root root        8 910 16:27 pydoc3 -> pydoc3.7*
-rwxr-xr-x 1 root root       88 910 16:27 pydoc3.7*
-rwxr-xr-x 1 root root      233 910 16:50 pyhtmlizer*
lrwxrwxrwx 1 root root        9 910 16:27 python3 -> python3.7*
-rwxr-xr-x 2 root root 14898480 910 16:27 python3.7*
lrwxrwxrwx 1 root root       17 910 16:27 python3.7-config -> python3.7m-config*
-rwxr-xr-x 2 root root 14898480 910 16:27 python3.7m*
-rwxr-xr-x 1 root root     3101 910 16:27 python3.7m-config*
lrwxrwxrwx 1 root root       16 910 16:27 python3-config -> python3.7-config*
lrwxrwxrwx 1 root root       10 910 16:27 pyvenv -> pyvenv-3.7*
-rwxr-xr-x 1 root root      445 910 16:27 pyvenv-3.7*
-rwxr-xr-x 1 root root      231 910 16:51 scrapy*
-rwxr-xr-x 1 root root      238 910 16:50 tkconch*
-rwxr-xr-x 1 root root      230 910 16:50 trial*
-rwxr-xr-x 1 root root      250 910 16:50 twist*
-rwxr-xr-x 1 root root      231 910 16:50 twistd*
koping@koping-HP:/opt/python3.7/bin$ python3.7
bash: /usr/bin/python3.7: No such file or directory
koping@koping-HP:/opt/python3.7/bin$ pip3.7 list
bash: /usr/bin/pip3.7: No such file or directory

如果想要使用刚下载的python3.7的话,可以对python3.7, pip3.7 建立软链接到 /usr/bin 目录下。然后就可以调用了。
给python3.7和pip3.7新建软链接

sudo ln -s /opt/python3.7/bin/python3.7 /usr/bin/python3.7
sudo ln -s /opt/python3.7/bin/pip3.7 /usr/bin/pip3.7

由于我之前已经装过了scrapy包,所以我的pip3.7 list里面现在有很多包了。

koping@koping-HP:/opt/python3.7/bin$ python3.7
bash: /usr/bin/python3.7: No such file or directory
koping@koping-HP:/opt/python3.7/bin$ pip3.7 list
bash: /usr/bin/pip3.7: No such file or directory
koping@koping-HP:/opt/python3.7/bin$ sudo ln -s /opt/python3.7/bin/python3.7 /usr/bin/python3.7
[sudo] password for koping: 
koping@koping-HP:/opt/python3.7/bin$ sudo ln -s /opt/python3.7/bin/pip3.7 /usr/bin/pip3.7
koping@koping-HP:/opt/python3.7/bin$ python3.7
Python 3.7.1 (default, Sep 10 2021, 16:25:59) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
koping@koping-HP:/opt/python3.7/bin$ pip3.7 list
Package           Version
----------------- --------
attrs             21.2.0
Automat           20.2.0
cffi              1.14.6
constantly        15.1.0
cryptography      3.4.8
cssselect         1.1.0
h2                3.2.0
hpack             3.0.0
hyperframe        5.2.0
hyperlink         21.0.0
idna              3.2
incremental       21.3.0
itemadapter       0.4.0
itemloaders       1.0.4
jmespath          0.10.0
lxml              4.6.3
parsel            1.6.0
pip               21.2.4
priority          1.3.0
Protego           0.1.16
pyasn1            0.4.8
pyasn1-modules    0.2.8
pycparser         2.20
PyDispatcher      2.0.5
pyOpenSSL         20.0.1
queuelib          1.6.2
Scrapy            2.5.0
service-identity  21.1.0
setuptools        39.0.1
six               1.16.0
Twisted           21.7.0
typing-extensions 3.10.0.2
w3lib             1.22.0
zope.interface    5.4.0
koping@koping-HP:/opt/python3.7/bin$ 

通过上面的理解,如果想要将pip3,python3切换成python3.7版本的话,我们只需要将/usr/bin目录下原有的pip3,python3链接给删除掉,然后重新按照上面的样式重建链接即可。参考命令如下:

// 删除原链接
sudo rm -rf /usr/bin/python3
sudo rm -rf /usr/bin/pip3

// 新建新软链接
sudo ln -s /opt/python3.7/bin/python3.7 /usr/bin/python3
sudo ln -s /opt/python3.7/bin/pip3.7 /usr/bin/pip3

3: 如何将pip源更换为国内源

由于使用pip install 包名 的时候,是从python国外官方源地址下载包,将地址替换为国内源地址的话,可以在工作中大幅加速下载效率,因此下面记录下如何将pip源更换为国内源。

3.1 pip安装时临时换源(清华源)

sudo pip3.7 install -i https://pypi.tuna.tsinghua.edu.cn/simple scrapy

3.2 pip安装永久换源

cd ~     # 进入家目录
mkdir .pip     # 新建.pip隐藏文件夹
cd .pip     # 进入.pip文件夹
touch pip.conf     # 新建pip.conf文件
vim pip.conf     # 用vim编辑pip.conf文件

编辑的内容如下:

[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
timeout = 6000
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

编辑结束后,先按esc键,然后输入’:wq’ 完成编辑

4: 装的包在哪里?

在第2步中,我已经安装了爬虫包scrapy,那我安装的包的位置一般在哪里呢?
安装的包一般位于所安装python路径的 /lib/python/site-packages 目录下,如下为我新装的python3.7中的scrapy包所在的位置:

koping@koping-HP:/opt/python3.7/lib/python3.7/site-packages$ pwd
/opt/python3.7/lib/python3.7/site-packages
koping@koping-HP:/opt/python3.7/lib/python3.7/site-packages$ ll
total 1296
drwxr-xr-x 70 root root   4096 910 16:51 ./
drwxr-xr-x 35 root root   4096 910 16:27 ../
drwxr-xr-x  3 root root   4096 910 16:50 attr/
drwxr-xr-x  2 root root   4096 910 16:50 attrs-21.2.0.dist-info/
drwxr-xr-x  4 root root   4096 910 16:50 automat/
drwxr-xr-x  2 root root   4096 910 16:50 Automat-20.2.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:49 cffi/
drwxr-xr-x  2 root root   4096 910 16:49 cffi-1.14.6.dist-info/
-rwxr-xr-x  1 root root 856392 910 16:49 _cffi_backend.cpython-37m-x86_64-linux-gnu.so*
drwxr-xr-x  2 root root   4096 910 16:49 cffi.libs/
drwxr-xr-x  3 root root   4096 910 16:50 constantly/
drwxr-xr-x  2 root root   4096 910 16:50 constantly-15.1.0.dist-info/
drwxr-xr-x  5 root root   4096 910 16:50 cryptography/
drwxr-xr-x  2 root root   4096 910 16:50 cryptography-3.4.8.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 cssselect/
drwxr-xr-x  2 root root   4096 910 16:50 cssselect-1.1.0.dist-info/
-rw-r--r--  1 root root    126 910 16:27 easy_install.py
drwxr-xr-x  3 root root   4096 910 16:50 h2/
drwxr-xr-x  2 root root   4096 910 16:50 h2-3.2.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 hpack/
drwxr-xr-x  2 root root   4096 910 16:50 hpack-3.0.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 hyperframe/
drwxr-xr-x  2 root root   4096 910 16:50 hyperframe-5.2.0.dist-info/
drwxr-xr-x  4 root root   4096 910 16:50 hyperlink/
drwxr-xr-x  2 root root   4096 910 16:50 hyperlink-21.0.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 idna/
drwxr-xr-x  2 root root   4096 910 16:50 idna-3.2.dist-info/
drwxr-xr-x  4 root root   4096 910 16:50 incremental/
drwxr-xr-x  2 root root   4096 910 16:50 incremental-21.3.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 itemadapter/
drwxr-xr-x  2 root root   4096 910 16:50 itemadapter-0.4.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:51 itemloaders/
drwxr-xr-x  2 root root   4096 910 16:51 itemloaders-1.0.4.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 jmespath/
drwxr-xr-x  2 root root   4096 910 16:50 jmespath-0.10.0.dist-info/
drwxr-xr-x  6 root root   4096 910 16:50 lxml/
drwxr-xr-x  2 root root   4096 910 16:50 lxml-4.6.3.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 OpenSSL/
drwxr-xr-x  3 root root   4096 910 16:50 parsel/
drwxr-xr-x  2 root root   4096 910 16:50 parsel-1.6.0.dist-info/
drwxr-xr-x  5 root root   4096 910 16:50 pip/
drwxr-xr-x  2 root root   4096 910 16:50 pip-21.2.4.dist-info/
drwxr-xr-x  5 root root   4096 910 16:27 pkg_resources/
drwxr-xr-x  3 root root   4096 910 16:50 priority/
drwxr-xr-x  2 root root   4096 910 16:50 priority-1.3.0.dist-info/
drwxr-xr-x  2 root root   4096 910 16:51 Protego-0.1.16-py3.7.egg-info/
-rw-r--r--  1 root root  17036 129  2019 protego.py
drwxr-xr-x  6 root root   4096 910 16:50 pyasn1/
drwxr-xr-x  2 root root   4096 910 16:50 pyasn1-0.4.8.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 pyasn1_modules/
drwxr-xr-x  2 root root   4096 910 16:50 pyasn1_modules-0.2.8.dist-info/
drwxr-xr-x  2 root root   4096 910 16:51 __pycache__/
drwxr-xr-x  4 root root   4096 910 16:49 pycparser/
drwxr-xr-x  2 root root   4096 910 16:49 pycparser-2.20.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 pydispatch/
drwxr-xr-x  2 root root   4096 910 16:50 PyDispatcher-2.0.5-py3.7.egg-info/
drwxr-xr-x  2 root root   4096 910 16:50 pyOpenSSL-20.0.1.dist-info/
drwxr-xr-x  4 root root   4096 910 16:50 queuelib/
drwxr-xr-x  2 root root   4096 910 16:50 queuelib-1.6.2.dist-info/
-rw-r--r--  1 root root    119 910 16:27 README.txt
drwxr-xr-x 18 root root   4096 910 16:51 scrapy/
drwxr-xr-x  2 root root   4096 910 16:51 Scrapy-2.5.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:50 service_identity/
drwxr-xr-x  2 root root   4096 910 16:50 service_identity-21.1.0.dist-info/
drwxr-xr-x  6 root root   4096 910 16:27 setuptools/
drwxr-xr-x  2 root root   4096 910 16:27 setuptools-39.0.1.dist-info/
drwxr-xr-x  2 root root   4096 910 16:50 six-1.16.0.dist-info/
-rw-r--r--  1 root root  34549 910 16:50 six.py
drwxr-xr-x 26 root root   4096 910 16:50 twisted/
drwxr-xr-x  2 root root   4096 910 16:50 Twisted-21.7.0.dist-info/
drwxr-xr-x  2 root root   4096 910 16:50 typing_extensions-3.10.0.2.dist-info/
-rw-r--r--  1 root root 109284 910 16:50 typing_extensions.py
drwxr-xr-x  3 root root   4096 910 16:50 w3lib/
drwxr-xr-x  2 root root   4096 910 16:50 w3lib-1.22.0.dist-info/
drwxr-xr-x  3 root root   4096 910 16:49 zope/
drwxr-xr-x  2 root root   4096 910 16:49 zope.interface-5.4.0.dist-info/
-rw-r--r--  1 root root    529 910 16:48 zope.interface-5.4.0-py3.7-nspkg.pth
koping@koping-HP:/opt/python3.7/lib/python3.7/site-packages$ 

5: 如何查找python

当在Linux环境上安装了多个版本的python时,可能需要查看python的安装路径?
这个时候可以使用Linux的命令:whereis name(全部匹配name)或者 locate name(部分匹配name).

whereis name  // 全部匹配name
locate name  // 部分匹配name,查找出来的东西比whereis要多很多

如下为我的系统上现在执行whereis python后的结果:

koping@koping-HP:~$ whereis python
python: /usr/bin/python3.5m /usr/bin/python2.7-config /usr/bin/python3.7 /usr/bin/python3.5 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python /usr/bin/python2.7 /usr/lib/python3.5 /usr/lib/python2.7 /etc/python3.5 /etc/python /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python3.5 /usr/include/python2.7 /usr/share/python /opt/python3.7/bin/python3.7 /opt/python3.7/bin/python3.7m /opt/python3.7/bin/python3.7-config /opt/python3.7/bin/python3.7m-config /usr/share/man/man1/python.1.gz
koping@koping-HP:~$

6:已安装了包,但是显示找不到命令怎么办?

如第3步中所介绍,已经在python3.7安装了scrapy包,但是在使用scrapy的时候,却显示找不到scrapy命令:

koping@koping-HP:~$ scrapy version
The program 'scrapy' is currently not installed. You can install it by typing:
sudo apt install python-scrapy

但是我们进入我们的python3.7中,却可以import scrapy

koping@koping-HP:/usr/bin$ python3.7
Python 3.7.1 (default, Sep 10 2021, 16:25:59) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import scrapy
>>> exit()

那为什么会显示找不到命令呢?原因和第2点,python,pip的调用原因一致。因为我们是在Linux系统上调用scrapy,现在scrapy已经装到了python3.7中,但是Linux怎么找到scrapy呢?所以需要将scrapy命令放入linux的环境变量中

6.1 方法一:

export PATH=$PATH:/opt/python3.7/bin

6.2 方法二

// STEP1 打开用户环境变量文件
koping@koping-HP:~$ vim ~/.bashrc
// STEP2 将scrapy命令的路径放入环境变量中,即在最后一行新增
 export PATH=$PATH:/opt/python3.7/bin
// STEP3 重启环境变量文件
koping@koping-HP:~$ source ~/.bashrc 

好了,这个时候就可以查看我们之前在python3.7里面安装好的scrapy了

koping@koping-HP:~$ scrapy version
Scrapy 2.5.0
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐