Python 30 天:第 20 天 -- PIP

<< 第 19 天 || 第 21 天 >>

第 20 天

Python PIP - Python 包管理器

什么是PIP?

PIP 代表首选安装程序。我们使用pip安装不同的 Python 包。包是一个 Python 模块,可以包含一个或多个模块或其他包。我们可以安装到我们的应用程序的一个或多个模块是一个包。在编程中,我们不必编写每个实用程序,而是安装包并将它们导入到我们的应用程序中。

安装PIP

如果您没有安装 pip,让我们现在安装它。转到您的终端或命令提示符并复制并粘贴以下内容:

asabeneh@Asabeneh:~$ pip install pip

通过写入检查是否安装了pip

pip --version
asabeneh@Asabeneh:~$ pip --version
pip 21.1.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.9.6)

如您所见,我使用的是 pip 版本 21.1.3,如果您看到某个数字略低于或高于该版本,则表示您已安装 pip。

让我们检查一下 Python 社区中出于不同目的使用的一些包。只是想让您知道,有许多软件包可用于不同的应用程序。

使用pip安装包

让我们尝试安装numpy,称为 numeric python。它是机器学习和数据科学社区中最受欢迎的软件包之一。

  • NumPy 是使用 Python 进行科学计算的基础包。其中包括:
    • 一个强大的 N 维数组对象
    • 复杂的(广播)功能
    • 用于集成 C/C++ 和 Fortran 代码的工具
    • 有用的线性代数、傅立叶变换和随机数功能
      asabeneh@Asabeneh:~$ pip install numpy

      让我们开始使用 numpy。打开你的 python 交互式 shell,编写 python 然后导入 numpy,如下所示:

      asabeneh@Asabeneh:~$ python
      Python 3.9.6 (default, Jun 28 2021, 15:26:21)
      [Clang 11.0.0 (clang-1100.0.33.8)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import numpy
      >>> numpy.version.version
      '1.20.1'
      >>> lst = [1, 2, 3,4, 5]
      >>> np_arr = numpy.array(lst)
      >>> np_arr
      array([1, 2, 3, 4, 5])
      >>> len(np_arr)
      5
      >>> np_arr * 2
      array([ 2,  4,  6,  8, 10])
      >>> np_arr  + 2
      array([3, 4, 5, 6, 7])
      >>>

      Pandas 是一个开源的、BSD 许可的库,为 Python 编程语言提供高性能、易于使用的数据结构和数据分析工具。让我们安装 numpy 的老大哥pandas

      asabeneh@Asabeneh:~$ pip install pandas
      asabeneh@Asabeneh:~$ python
      Python 3.9.6 (default, Jun 28 2021, 15:26:21)
      [Clang 11.0.0 (clang-1100.0.33.8)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import pandas

      本节不是关于 numpy 或 pandas 的,我们在这里尝试学习如何安装包以及如何导入它们。如果需要,我们将在其他部分讨论不同的包。

      让我们导入一个网络浏览器模块,它可以帮助我们打开任何网站。我们不需要安装这个模块,它已经默认安装在 Python 3 中。例如,如果你想随时打开任意数量的网站或者如果你想安排一些事情,可以使用这个 webbrowser模块

      import webbrowser # web browser module to open websites
      
      # list of urls: python
      url_lists = [
          'http://www.python.org',
          'https://www.linkedin.com/in/asabeneh/',
          'https://github.com/Asabeneh',
          'https://twitter.com/Asabeneh',
      ]
      
      # opens the above list of websites in a different tab
      for url in url_lists:
          webbrowser.open_new_tab(url)

      卸载软件包

    • 如果您不想保留已安装的软件包,可以使用以下命令将其删除。
      pip uninstall packagename

      包清单

    • 在我们的机器上查看已安装的包。我们可以使用 pip 后跟列表。
      pip list

      显示包

    • 显示有关包的信息
      pip show packagename
      asabeneh@Asabeneh:~$ pip show pandas
      Name: pandas
      Version: 1.2.3
      Summary: Powerful data structures for data analysis, time series, and statistics
      Home-page: http://pandas.pydata.org
      Author: None
      Author-email: None
      License: BSD
      Location: /usr/local/lib/python3.7/site-packages
      Requires: python-dateutil, pytz, numpy
      Required-by:

      如果我们想要更多细节,只需添加 --verbose

      asabeneh@Asabeneh:~$ pip show --verbose pandas
      Name: pandas
      Version: 1.2.3
      Summary: Powerful data structures for data analysis, time series, and statistics
      Home-page: http://pandas.pydata.org
      Author: None
      Author-email: None
      License: BSD
      Location: /usr/local/lib/python3.7/site-packages
      Requires: numpy, pytz, python-dateutil
      Required-by:
      Metadata-Version: 2.1
      Installer: pip
      Classifiers:
        Development Status :: 5 - Production/Stable
        Environment :: Console
        Operating System :: OS Independent
        Intended Audience :: Science/Research
        Programming Language :: Python
        Programming Language :: Python :: 3
        Programming Language :: Python :: 3.5
        Programming Language :: Python :: 3.6
        Programming Language :: Python :: 3.7
        Programming Language :: Python :: 3.8
        Programming Language :: Cython
        Topic :: Scientific/Engineering
      Entry-points:
        [pandas_plotting_backends]
        matplotlib = pandas:plotting._matplotlib

      PIP freeze

    • 生成安装的 Python 包及其版本,输出适合在需求文件中使用。requirements.txt 文件应该包含 Python 项目中所有已安装的 Python 包。
      asabeneh@Asabeneh:~$ pip freeze
      docutils==0.11
      Jinja2==2.7.2
      MarkupSafe==0.19
      Pygments==1.6
      Sphinx==1.2.2

      pip freeze 为我们提供了使用、安装的软件包及其版本。我们将它与 requirements.txt 文件一起用于部署。

从URL读取 

到目前为止,您已经熟悉如何读取或写入位于本地计算机上的文件。有时,我们想使用 url 或 API 从网站读取数据。API代表应用程序接口。它是一种在服务器之间主要以 json 数据形式交换结构化数据的方法。要打开网络连接,我们需要一个名为requests的包- 它允许打开网络连接并实现 CRUD(创建、读取、更新和删除)操作。在本节中,我们将仅介绍读取 ore 获取 CRUD 的一部分。

让我们安装请求

asabeneh@Asabeneh:~$ pip install requests

我们将在请求模块中看到getstatus_codeheaderstextjson方法:

  • get():打开网络并从 url 获取数据 - 它返回一个响应对象
  • status_code:获取数据后,我们可以检查操作的状态(成功、错误等)
  • headers:检查标题类型
  • text:从获取的响应对象中提取文本
  • json:提取 json 数据让我们从该网站读取一个 txt 文件,https://www.w3.org/TR/PNG/iso_8859-1.txt
    import requests # importing the request module
    
    url = 'https://www.w3.org/TR/PNG/iso_8859-1.txt' # text from a website
    
    response = requests.get(url) # opening a network and fetching a data
    print(response)
    print(response.status_code) # status code, success:200
    print(response.headers)     # headers information
    print(response.text) # gives all the text from the page
    <Response [200]>
    200
    {'date': 'Sun, 08 Dec 2019 18:00:31 GMT', 'last-modified': 'Fri, 07 Nov 2003 05:51:11 GMT', 'etag': '"17e9-3cb82080711c0;50c0b26855880-gzip"', 'accept-ranges': 'bytes', 'cache-control': 'max-age=31536000', 'expires': 'Mon, 07 Dec 2020 18:00:31 GMT', 'vary': 'Accept-Encoding', 'content-encoding': 'gzip', 'access-control-allow-origin': '*', 'content-length': '1616', 'content-type': 'text/plain', 'strict-transport-security': 'max-age=15552000; includeSubdomains; preload', 'content-security-policy': 'upgrade-insecure-requests'}

  • 让我们从 API 读取。API代表应用程序接口。它是一种在服务器之间交换结构数据的方法,主要是 json 数据。API 示例: https: //restcountries.eu/rest/v2/all。让我们使用requests模块来阅读这个 API。
    import requests
    url = 'https://restcountries.eu/rest/v2/all'  # countries api
    response = requests.get(url)  # opening a network and fetching a data
    print(response) # response object
    print(response.status_code)  # status code, success:200
    countries = response.json()
    print(countries[:1])  # we sliced only the first country, remove the slicing to see all countries
    <Response [200]>
    200
    [{'alpha2Code': 'AF',
      'alpha3Code': 'AFG',
      'altSpellings': ['AF', 'Afġānistān'],
      'area': 652230.0,
      'borders': ['IRN', 'PAK', 'TKM', 'UZB', 'TJK', 'CHN'],
      'callingCodes': ['93'],
      'capital': 'Kabul',
      'cioc': 'AFG',
      'currencies': [{'code': 'AFN', 'name': 'Afghan afghani', 'symbol': '؋'}],
      'demonym': 'Afghan',
      'flag': 'https://restcountries.eu/data/afg.svg',
      'gini': 27.8,
      'languages': [{'iso639_1': 'ps',
                     'iso639_2': 'pus',
                     'name': 'Pashto',
                     'nativeName': 'پښتو'},
                    {'iso639_1': 'uz',
                     'iso639_2': 'uzb',
                     'name': 'Uzbek',
                     'nativeName': 'Oʻzbek'},
                    {'iso639_1': 'tk',
                     'iso639_2': 'tuk',
                     'name': 'Turkmen',
                     'nativeName': 'Türkmen'}],
      'latlng': [33.0, 65.0],
      'name': 'Afghanistan',
      'nativeName': 'افغانستان',
      'numericCode': '004',
      'population': 27657145,
      'region': 'Asia',
      'regionalBlocs': [{'acronym': 'SAARC',
                         'name': 'South Asian Association for Regional Cooperation',
                         'otherAcronyms': [],
                         'otherNames': []}],
      'subregion': 'Southern Asia',
      'timezones': ['UTC+04:30'],
      'topLevelDomain': ['.af'],
      'translations': {'br': 'Afeganistão',
                       'de': 'Afghanistan',
                       'es': 'Afganistán',
                       'fa': 'افغانستان',
                       'fr': 'Afghanistan',
                       'hr': 'Afganistan',
                       'it': 'Afghanistan',
                       'ja': 'アフガニスタン',
                       'nl': 'Afghanistan',
                       'pt': 'Afeganistão'}}]

    如果我们正在获取 JSON 数据,我们使用来自响应对象的json()方法。对于 txt、html、xml 等文件格式我们可以使用text

创建包 

我们根据一些标准将大量文件组织在不同的文件夹和子文件夹中,以便我们可以轻松地查找和管理它们。如您所知,一个模块可以包含多个对象,例如类、函数等。一个包可以包含一个或多个相关模块。包实际上是一个包含一个或多个模块文件的文件夹。让我们使用以下步骤创建一个名为 mypackage 的包:

在 30DaysOfPython 文件夹中创建一个名为 mypacakge 的新文件夹在 mypackage 文件夹中创建一个空的init .py 文件。使用以下代码创建模块 arithmetic.py 和 greet.py:

# mypackage/arithmetics.py
# arithmetics.py
def add_numbers(*args):
    total = 0
    for num in args:
        total += num
    return total


def subtract(a, b):
    return (a - b)


def multiple(a, b):
    return a * b


def division(a, b):
    return a / b


def remainder(a, b):
    return a % b


def power(a, b):
    return a ** b
# mypackage/greet.py
# greet.py
def greet_person(firstname, lastname):
    return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!'

你的包的文件夹结构应该是这样的: 

─ mypackage
    ├── __init__.py
    ├── arithmetic.py
    └── greet.py

现在让我们打开 python 交互式 shell 并尝试我们创建的包:

asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mypackage import arithmetics
>>> arithmetics.add_numbers(1, 2, 3, 5)
11
>>> arithmetics.subtract(5, 3)
2
>>> arithmetics.multiple(5, 3)
15
>>> arithmetics.division(5, 3)
1.6666666666666667
>>> arithmetics.remainder(5, 3)
2
>>> arithmetics.power(5, 3)
125
>>> from mypackage import greet
>>> greet.greet_person('Asabeneh', 'Yetayeh')
'Asabeneh Yetayeh, welcome to 30DaysOfPython Challenge!'
>>>

如您所见,我们的软件包运行完美。包文件夹包含一个名为init.py的特殊文件- 它存储包的内容。如果我们将init.py放在包文件夹中,python start 会将其识别为一个包。init .py公开其模块中的指定资源,以导入到其他 python 文件中。空的init .py 文件使所有功能在导入包时可用。init .py 对于 Python 将文件夹识别为包是必不可少的

有关包的更多信息

  • 数据库

    • SQLAlchemy 或 SQLObject - 面向对象访问多个不同的数据库系统
      • pip 安装 SQLAlchemy
  • Web开发

    • Django - 高级网络框架。
      • pip 安装 django
    • Flask - 基于 Werkzeug、Jinja 2 的 Python 微框架。(已获得 BSD 许可)
      • pip 安装烧瓶
  • HTML 解析器

    • Beautiful Soup - 为屏幕抓取等快速周转项目设计的 HTML/XML 解析器,将接受错误的标记。
      • pip 安装 beautifulsoup4
    • PyQuery - 在 Python 中实现 jQuery;显然比 BeautifulSoup 快。
  • XML处理

    • ElementTree - Element 类型是一个简单但灵活的容器对象,旨在在内存中存储分层数据结构,例如简化的 XML 信息集。--注意:Python 2.5 及更高版本在标准库中有 ElementTree
  • 图形用户界面

    • PyQt - 跨平台 Qt 框架的绑定。
    • TkInter - 传统的 Python 用户界面工具包。
  • 数据分析、数据科学和机器学习

    • Numpy:Numpy(numeric python) 被称为 Python 中最流行的机器学习库之一。
    • Pandas:是 Python 中的数据分析、数据科学和机器学习库,提供高级数据结构和各种分析工具。
    • SciPy:SciPy 是面向应用程序开发人员和工程师的机器学习库。SciPy 库包含用于优化、线性代数、集成、图像处理和统计的模块。
    • Scikit-Learn:它是 NumPy 和 SciPy。它被认为是处理复杂数据的最佳库之一。
    • TensorFlow:是谷歌构建的机器学习库。
    • Keras:被认为是 Python 中最酷的机器学习库之一。它提供了一种更简单的机制来表达神经网络。Keras 还提供了一些用于编译模型、处理数据集、图形可视化等的最佳实用程序。
  • 网络:

    • requests:是一个我们可以用来向服务器发送请求的包(GET,POST,DELETE,PUT)
      • pip 安装请求

🌕你总是在进步,你是通往伟大之路的 20 步之首。现在为你的大脑和肌肉做一些练习。

练习: 第 20 天

  1. 阅读此网址并找到 10 个最常用的单词。romeo_and_juliet = ' http://www.gutenberg.org/files/1112/1112.txt '
  2. 阅读猫 API 和 cats_api = ' https://api.thecatapi.com/v1/breeds ' 并找到:
    1. 以公制单位表示的猫体重的最小值、最大值、平均值、中值、标准差。
    2. 猫寿命的最小值、最大值、平均值、中值、标准差(以年为单位)。
    3. 创建国家和猫品种的频率表
  3. 阅读国家 API并找到
    1. 10个最大的国家
    2. 10种最常用的语言
    3. 国家 API 中的语言总数
  4. UCI 是获取数据科学和机器学习数据集的最常见场所之一。阅读 UCL 的内容(UCI Machine Learning Repository: Data Sets)。没有额外的库会很困难,所以你可以用 BeautifulSoup4 试试

🎉恭喜!🎉

<< 第 19 天 || 第 21 天 >>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舍不得,放不下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值