Python pip

Šajā apmācībā mēs uzzināsim, kā izmantot pip, lai instalētu un pārvaldītu Python paketes.

Kas ir pip?

pipir Python standarta pakotņu pārvaldnieks. Mēs varam izmantot, piplai instalētu papildu paketes, kas nav pieejamas Python standarta bibliotēkā. Piemēram,

 pip install numpy 

Ja mēs būtu instalējuši pipsavā sistēmā, šī komanda būtu instalējusi numpybibliotēku.

Kā instalēt pip?

pipir iepriekš instalēts Python 3.4 vai vecākās versijās. Mēs varam pārbaudīt, vai tas pipir instalēts, izmantojot konsolē šādu komandu:

 pip --version 

Ja pipsistēma jau ir pieejama, piptiek parādīta attiecīgā versija, piemēram:

 pip 19.3.1 no C: Python37 lib site-package pip (python 3.7) 

Ja mēs izmantojam vecāku Python versiju vai neesam pipinstalēti kāda cita iemesla dēļ, veiciet darbības, kas aprakstītas šajā saitē: pip instalēšana

Izmantojot pip

pipir komandrindas programma. Pēc tās instalēšanas piptiek pievienota komanda, kuru var izmantot kopā ar komandu uzvedni.

Pipa sintakse ir šāda:

 pip 

Pakotņu instalēšana ar pip

Neatkarīgi no standarta Python bibliotēkas, Python kopiena piedalās plašā skaitā paku, kas pielāgotas dažādām izstrādes sistēmām, rīkiem un bibliotēkām.

Lielākā daļa šo paku tiek oficiāli mitinātas un publicētas Python Package Index (PyPI). pipļauj mums lejupielādēt un instalēt šīs paketes.

Pamata pakotnes instalēšana

installKomandu izmanto, lai instalētu paketes, izmantojot pip. Ņemsim piemēru:

Pieņemsim, ka mēs vēlamies instalēt requestspopulāru Python HTTP bibliotēku. Mēs to varam izdarīt ar šādas komandas palīdzību.

 pip install requests 

Rezultāts

Pieprasījumu apkopošana, izmantojot kešatmiņā saglabāto https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-ach.whl 3.0.2 .pythonhosted.org / package / bc / a9 / 01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8 / chardet-3.0.4-py2.py3-none-any.whl Urllib3 apkopošana! = 1.25.0 = https:! //files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl Collecting idna = 2.5 Izmantojot cached https://files /2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl Collecting certificatei = = 2017.4.17 Lejupielāde https: //files.pythonhosted.org / paketes / b9 / 63 / df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99 / certificatei-2019.11.28-py2.py3-none-any.whl (156kB) Savākto pakotņu instalēšana: chardet, urilli3i 3.0.4 idna-2.8 pieprasījumi-2.22.0 urllib3-1.25.7

Šeit mēs varam redzēt, ka pipir izmantota installkomanda, kurai seko tās paketes nosaukums, kuru mēs vēlamies instalēt ( requests).

Visas citas atkarības, piemēram chardet, urllib3un certifinepieciešamas šai pakotnei, ir instalējušas arī pip.

Norādot pakotnes versiju

Ja pip installtiek izmantots minimālā formā, tiek piplejupielādēta jaunākā pakotnes versija.

Dažreiz tikai noteikta versija ir saderīga ar citām programmām. Tātad mēs varam definēt pakotnes versiju šādā veidā:

 pip instalēšanas pieprasījumi == 2.21.0 

Šeit mēs esam instalējuši requestsbibliotēkas versiju 2.11.0 .

Uzskaitīto instalēto paku saraksts ar pip

pip listKomandu var izmantot, lai uzskaitītu visus pieejamos paketes pašreizējā Python vidē.

 pip list 

Rezultāts

 Pakotnes versija ---------- ---------- certificatei 2019.11.28 chardet 3.0.4 idna 2.8 pip 19.3.1 pieprasa 2.22.0 setuptools 45.0.0 urllib3 1.25.7 ritenis 0.33 .6 

Informācija par iepakojumu ar pip show

pip showKomanda parāda informāciju par vienu vai vairākiem uzstādīta paketes. Apskatīsim piemēru:

 pip show requests 

Rezultāts

Nosaukums: pieprasījumi Versija: 2.22.0 Kopsavilkums: Python HTTP cilvēkiem. Mājas lapa: http://python-requests.org Autors: Kenneth Reitz Autors-e-pasts: [email protected] Licence: Apache 2.0 Atrašanās vieta: c: users dell desktop venv lib site-package Nepieciešama: certificatei, chardet, urllib3, idna Obligāti:

Šeit showkomanda parāda informāciju par requestsbibliotēku. Ievērojiet kolonnu Requires and Required-by iepriekšminētajā izvadā.

Slejā Nepieciešams ir norādītas requestsbibliotēkas nepieciešamās atkarības . Un, Nepieciešama pa kolonnu rāda pakotnes, kas prasa requests.

Pakotnes atinstalēšana ar pip

Mēs varam atinstalēt paketi, izmantojot pipkopā ar pip uninstallkomandu.

Pieņemsim, ka mēs vēlamies noņemt requestsbibliotēku no pašreizējās Python vides. Mēs to varam izdarīt šādi:

 pip uninstall requests 

Rezultāts

Atinstalējot pieprasījumus-2.22.0: tiktu noņemts: C: Python37 lib site-package pieprasījumi-2.22.0.dist-info * C: Python37 lib site-package pieprasījumi * Turpināt (y / n)? y Veiksmīgi atinstalēti pieprasījumi-2.22.0

Kā redzam, requestspakete tiek noņemta pēc pēdējās uzvednes.

Note: Even though the specified package is removed, the packages that were installed as dependencies are not removed. In this case, the dependencies (chardet, urllib3, and certifi) of the requests library aren't uninstalled.

If we need to remove the dependencies of a package as well, we can use the pip show command to view installed packages and remove them manually.

Using Requirement Files

A file containing all the package names can also be used to install Python packages in batches.

Let's take a look at an example:

Suppose we have a file requirements.txt which has the following entries:

 numpy Pillow pygame 

We can install all these packages and their dependencies by using a single command in pip.

 pip install -r requirements.txt 

Output

 Collecting numpy Using cached https://files.pythonhosted.org/packages/a9/38/f6d6d8635d496d6b4ed5d8ca4b9f193d0edc59999c3a63779cbc38aa650f/numpy-1.18.1-cp37-cp37m-win_amd64.whl Collecting Pillow Using cached https://files.pythonhosted.org/packages/88/6b/66f502b5ea615f69433ae1e23ec786b2cdadbe41a5cfb1e1fabb4f9c6ce9/Pillow-7.0.0-cp37-cp37m-win_amd64.whl Collecting pygame Using cached https://files.pythonhosted.org/packages/ed/56/b63ab3724acff69f4080e54c4bc5f55d1fbdeeb19b92b70acf45e88a5908/pygame-1.9.6-cp37-cp37m-win_amd64.whl Installing collected packages: numpy, Pillow, pygame Successfully installed Pillow-7.0.0 numpy-1.18.1 pygame-1.9.6 

Here, we have used the same install command with pip.

However, the additional argument -r specifies pip that we are passing a requirements file rather than a package name.

Creating Requirements File

As an alternative to manually creating the requirements file, pip offers the freeze command. Let's look at how to use this command.

Suppose our current Python environment has the following packages. It can be displayed using pip list.

 Package Version ---------- ------- numpy 1.17.0 Pillow 6.1.0 pip 19.3.1 pygame 1.9.6 setuptools 45.0.0 wheel 0.33.6 

The packages that don't come preinstalled will Python are listed using the freeze command.

 pip freeze 

Output

 numpy==1.17.0 Pillow==6.1.0 pygame==1.9.6 

The pip freeze command displays the packages and their version in the format of the requirements file.

So this output can be redirected to create a requirements file using the following command:

 pip freeze> requirements.txt 

A new requirements.txt file is created in the working directory. It can later be used in other Python environments to install specific versions of packages.

Search packages in pip

The search command is used to search for packages in the command prompt. Let's look at an example:

 pip search pygame 

Output

 pygame-anisprite (1.0.0) - Animated sprites for PyGame! pygame-ai (0.1.2) - Videogame AI package for PyGame pygame-engine (0.0.6) - Simple pygame game engine. pygame-assets (0.1) - Assets manager for Pygame apps pygame-gui (0.4.2) - A GUI module for pygame 2 pygame-spritesheet (0.2.0) - Python pygame extension that provides SpriteSheet class. pygame-minesweeper (1.0) - Minesweeper game implemented in python using pygame pygame-menu (2.1.0) - A menu for pygame, simple, lightweight and easy to use pygame-plot (0.1) - Quick visualization of data using pygame with a matplotlib style pygame (1.9.6) - Python Game Development… 

Here, we have searched for a library called pygame. All other packages that match the keyword are displayed. This command is helpful for finding related packages.

Lai uzzinātu vairāk par to pip, apmeklējiet: Python pip (oficiālā dokumentācija)

Interesanti raksti...