python的包

文章详细描述了在Ubuntu18.04系统中如何找到并使用自带的Python3,以及Python如何查找内置库和安装第三方库如numpy的过程。重点介绍了环境变量PATH的作用,以及pip的安装和使用,包括numpy的安装位置和缓存机制。
摘要由CSDN通过智能技术生成

目录

ubuntu-18自带的python

python自带的库

python安装需要的库

查看pip情况

再次查看pip

安装numpy

查看numpy安装信息

python中查看numpy版本

查看numpy库的位置

python-pip下载的安装包


实验机器:ubuntu-18

ubuntu-18自带的python

终端输入python3-进入python工作台

xx@xx:~$ python3
Python 3.6.9 (default, xx) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

思考一个问题:输入python3, 操作系统是如何找到 python3 软件的

查看系统是在哪里找到的python3命令

xx@xx:~$ whereis python3
python3: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3 /usr/lib/python3.7 /usr/lib/python3.8 /usr/lib/python3.6 /usr/lib/python3 /etc/python3.6 /etc/python3 /usr/local/lib/python3.6 /usr/share/python3 /usr/share/man/man1/python3.1.gz

发现最先找的是 /usr/bin/python3.6m

查看下 /usr/bin 下的东西

直接执行:ls /usr/bin 终端会打印很多东西

过滤下:ls /usr/bin | grep python

xx@xx:~$ ls /usr/bin | grep python
python3
python3.6
python3.6m
python3m

这4个文件有啥区别呢?

xx@xx:~$ ls -l /usr/bin | grep python
lrwxrwxrwx 1 root   root          pdb3.6 -> ../lib/python3.6/pdb.py
lrwxrwxrwx 1 root   root          py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root   root          python3 -> python3.6
-rwxr-xr-x 2 root   root          python3.6
-rwxr-xr-x 2 root   root          python3.6m
lrwxrwxrwx 1 root   root          python3m -> python3.6m
  • python3 指向 python3.6

  • python3m 指向 python3.6m

下面看下python3.6和python3.6m

xx@xx:~$ python3.6
Python 3.6.9 (default, ) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
xx@xx:~$ python3.6m
Python 3.6.9 (default, ) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

没有区别

系统是怎么知道到 /usr/bin下面 找python3的呢

看下系统的所有环境变量

xx@xx:~$ env
...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
...

在PATH这个变量里记录着 好几个 目录,其中就有 /usr/bin

知道了PATH这个变量记录着 路径,也可以单独 获取PATH的信息

xx@xx:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

python自带的库

xx@xx:~$ python3
Python 3.6.9 (default, xx) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.__file__
'/usr/lib/python3.6/os.py'

比如 import os 的时候,python是怎么找到os这个库/模块的 ?

>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
>>> from pprint import pprint
>>> pprint(sys.path)
['',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/lib/python3/dist-packages']

看下/usr/lib/python3.6, /usr/lib/python3.6/lib-dynload, /usr/local/lib/python3.6/dist-packages, /usr/lib/python3/dist-packages, 下面都有什么

xx@xx:~$ ls /usr/lib/python3.6
abc.py                        dbm                 LICENSE.txt        py_compile.py     symbol.py
aifc.py                       decimal.py          linecache.py       _pydecimal.py     symtable.py
antigravity.py                difflib.py          locale.py          pydoc_data        _sysconfigdata_m_linux_x86_64-linux-gnu.py
argparse.py                   dis.py              logging            pydoc.py          sysconfig.py
ast.py                        distutils           lzma.py            _pyio.py          tabnanny.py
asynchat.py                   doctest.py          macpath.py         queue.py          tarfile.py
asyncio                       dummy_threading.py  macurl2path.py     quopri.py         telnetlib.py
asyncore.py                   _dummy_thread.py    mailbox.py         random.py         tempfile.py
base64.py                     email               mailcap.py         reprlib.py        
bdb.py                        encodings           _markupbase.py     re.py             textwrap.py
binhex.py                     enum.py             mimetypes.py       rlcompleter.py    this.py
bisect.py                     filecmp.py          modulefinder.py    runpy.py          _threading_local.py
_bootlocale.py                fileinput.py        multiprocessing    sched.py          threading.py
bz2.py                        fnmatch.py          netrc.py           secrets.py        timeit.py
calendar.py                   formatter.py        nntplib.py         selectors.py      tokenize.py
cgi.py                        fractions.py        ntpath.py          shelve.py         token.py
cgitb.py                      ftplib.py           nturl2path.py      shlex.py          traceback.py
chunk.py                      functools.py        numbers.py         shutil.py         tracemalloc.py
cmd.py                        __future__.py       opcode.py          signal.py         trace.py
codecs.py                     genericpath.py      operator.py        _sitebuiltins.py  tty.py
codeop.py                     getopt.py           optparse.py        sitecustomize.py  turtle.py
code.py                       getpass.py          os.py              site.py           types.py
xx@xx:~$ ls /usr/lib/python3.6/lib-dynload
_asyncio.cpython-36m-x86_64-linux-gnu.so         _lsprof.cpython-36m-x86_64-linux-gnu.so
audioop.cpython-36m-x86_64-linux-gnu.so          _lzma.cpython-36m-x86_64-linux-gnu.so
_bz2.cpython-36m-x86_64-linux-gnu.so             mmap.cpython-36m-x86_64-linux-gnu.so
_codecs_cn.cpython-36m-x86_64-linux-gnu.so       _multibytecodec.cpython-36m-x86_64-linux-gnu.so
_codecs_hk.cpython-36m-x86_64-linux-gnu.so       _multiprocessing.cpython-36m-x86_64-linux-gnu.so
_codecs_iso2022.cpython-36m-x86_64-linux-gnu.so  nis.cpython-36m-x86_64-linux-gnu.so
_codecs_jp.cpython-36m-x86_64-linux-gnu.so       _opcode.cpython-36m-x86_64-linux-gnu.so
_codecs_kr.cpython-36m-x86_64-linux-gnu.so       ossaudiodev.cpython-36m-x86_64-linux-gnu.so
_codecs_tw.cpython-36m-x86_64-linux-gnu.so       parser.cpython-36m-x86_64-linux-gnu.so
_crypt.cpython-36m-x86_64-linux-gnu.so           readline.cpython-36m-x86_64-linux-gnu.so
_csv.cpython-36m-x86_64-linux-gnu.so             resource.cpython-36m-x86_64-linux-gnu.so
_ctypes.cpython-36m-x86_64-linux-gnu.so          _sqlite3.cpython-36m-x86_64-linux-gnu.so
_ctypes_test.cpython-36m-x86_64-linux-gnu.so     _ssl.cpython-36m-x86_64-linux-gnu.so

看来python自己也记录着去哪里找自己带的标准库

python安装需要的库

以安装numpy为例

xx@xx:~$ python3
Python 3.6.9 (default, xx) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
查看pip情况
xx@xx:~$ pip -V

Command 'pip' not found, but can be installed with:

sudo apt install python-pip

xx@xx:~$ pip3 -V

Command 'pip3' not found, but can be installed with:

sudo apt install python3-pip

安装pip

sudo apt install python3-pip
再次查看pip
xx@xx:~$ pip -V

Command 'pip' not found, but can be installed with:

sudo apt install python-pip

xx@xx:~$ pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

查看在apt install python3-pip安装的过程中 pip3 为 python安装了哪些包

安装numpy
xx@xx:~$ pip3 install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/xx/numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl (13.4MB)
    100% |████████████████████████████████| 13.4MB 102kB/s 
Installing collected packages: numpy
Successfully installed numpy-1.19.5
查看numpy安装信息
 pip3 list | grep numpy
python中查看numpy版本
xx@xx:~$ python3
Python 3.6.9 (default, xx) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.19.5'
查看numpy库的位置
>>> numpy.__file__
'/home/xx/.local/lib/python3.6/site-packages/numpy/__init__.py'

对比下安装numpy之前python3查找库的路径

安装pip3 和 numpy 后

xx@xx:~$ python3
Python 3.6.9 (default, xx) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from pprint import pprint
>>> pprint(sys.path)
['',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/xx/.local/lib/python3.6/site-packages',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/lib/python3/dist-packages']

比之前多了 /home/xx/.local/lib/python3.6/site-packages 路径

查看 这个路径下的东西

xx@xx:~$ ls /home/xx/.local/lib/python3.6/site-packages
numpy  numpy-1.19.5.dist-info  numpy.libs

通过pip3 show 查看numpy的安装信息

xx@xx:~$ pip3 show numpy
Name: numpy
Version: 1.19.5
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /home/xx/.local/lib/python3.6/site-packages
Requires: 

确定numpy安装在了:/home/xx/.local/lib/python3.6/site-packages

python-pip下载的安装包

pip3 uninstall numpy

再安装numpy

xx@xx:~$ pip3 install numpy
Collecting numpy
  Using cached https://files.pythonhosted.org/packages/xx/numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.19.5

第一次是下载numpy的安装包,然后再安装

上面卸载numpy,再重新安装,没有重新下载numpy安装包,而是使用了缓存的numpy安装包

我们查找下 numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl 这个文件

xx@xx:~$ sudo find / -name 'numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl'
xx@xx:~$

发现电脑里没有 numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl 这个文件

那这个文件 在哪呢

我们看下这个

xx@xx:~$ ls ~/.cache/pip/
http

删除 ~/.cache/pip/

xx@xx:~$ rm -rf ~/.cache/pip/

卸载、再安装numpy,发现重新下载numpy的安装包

这时再查看 ~/.cache/pip

之所以没找到 numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl 这个文件,是因为被改名了,

然后 缓存到 ~/.cache/pip 路径下,当再去下载numpy时,发现要下载的安装包已经在本地缓存了,也就是之前安装过,不必再次下载,但是这里要注意,随着安装的包越来越多,缓存的文件也会越来越大,影响ubuntu系统盘的空间

  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值