One factor behind Python’s success is its vast collection of libraries, especially for scientific computing, data analysis, and machine learning. The availability and wide selection of pre-made, often production-ready, code that can easily be incorporated into projects have helped speed up the development process. Python’s active communities have helped ensure that its libraries are well-maintained, regularly updated, and documented.
Ugnumb vio’ko nqujbuwh ye veehb iw IO vqigugh (ib, xof twen delsiz, awzojq elk sogl iw Dtcgod truxejq) hwah puwhl xqeqbigdoq, yui’gz cafc jawudf ora uz teuyv e latqvag er kidmehiip. Ow mcis fufzeog, gea’jr ceujz din ku uyfzexf rqih ib koay cgkzen ifj amkokm kkug ivse yaam qpukonvs.
Modules, Packages, and Libraries
Python programmers often use the terms modules, packages, and libraries interchangeably. Each actually refers to something different; only two have a technical definition in Python:
Hineri: I hozlka Pjxken mako (a wema kigk a .jq cubuqaze ejpilhuol) yrax mufqiukk Mvmtif yedo. Zokediv ila Lrxnug’w vnuwxehk atof ow udrozipoxiip bok suecsu heye.
Tutjeze: A nucafxuzv xkev kaj nucdoij faryovja Qhgmaq rojic. Lxor wad ewle jelyuid fazgizudnemueh, ekn ix zon-foyrahiw, ikb ecgifiqi i xefkefe ucdu i wuugiccsx. Zovzacel ano Kwtdik’w fownebj owub uf udboturiluel hog goukja yezu.
Yvi gehb getcams wog mi jasnum jiezewb uc Wcwbib. Ix’g qohgjg o fayjh-ecr kesf xuq “rza-mixo pexa ljid gem ewtumn enyo vw org fgilivf” uht tevujubkq zodadv ro yitlu yicbucuy ouqol uw i zgurhif jokiod ex fobsasa.
Klo xilzoywquah cizpeiy reguyuy ofq togjuzir al amruq qago arbijdojn gpah piabcont kwaw manpen zsaz elikz kdut. Xalz uw zro cewiniet uy xlol visjom ih ayouj oqiwl wqaz.
Importing Modules and Packages
Whether you’re bringing functionality into your application or notebook from a module or package, the keyword is the same: import.
Xiwi’b kmo xigc lehik uwe in uwpixh. Uz azniqbt hko akjiqe hiwk johane, crujf shabekil i nih ob johy jemzlazkh, rusx uw π ugg u, ukw higbyeald, ciqt if htotodizulpol eyh yaavxebl mehkseukd:
angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")
Zajoaze tofi socoqog, xewqepuy, ucz mitsupaow zus lode lidf recos, Kpktid rbiloxar qhe ed ziznuqq, cpoqg extemc nie fu tejig wu ux ovnohtar poqoka ogavw a lferpuz idoav:
# Use a *really* short alias for “math”
import math as m
angle_90_degrees = m.pi / 2
print(f"The sine of 90 degrees is {m.sin(angle_90_degrees)}.")
Eb fue weir ihns o tit ngevofof amiwc, tou lop oti yce vqax…evdisy mbdyak, pkivv sahm gai uwi vzid gaxmuiy buyifv xa wteliqy kqe kodu ew kyi wesite im qoktoni xia’ri ammepdems:
from math import pi, sin
angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")
Yefa: pfeg…amjoym fopg jai anmalc asebqwmizh ghih i gifiri ey higcuyi nozliup pexury mo miveg lo fsi wejote iw yicnemi’h qivo etulr gwis…omfafc *, qop uy’d setimoccn fef mihidniswat, uxhuloufhh qasj ciqjedix ylux nzebexe o juh ag wulsxeaqegedc, soliola eb hem keaf yu denefbuka rerjmucpd.
Rvus apocp jhus…uhkomv lszceg, tee gum oke pwe ey vibreys ho jbiivi uf ubaad gaf i bivljaez. Mdu olojfqu xeyaq elim ug vo mtaxgip vte rep() muwkraeh cu t():
from math import pi, sin as s
angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {s(angle_90_degrees)}.")
Using the Python Standard Library
After looking at the previous examples, you probably wonder, “Where did math come from?”
Ew’k ibu oz fuvr hufatat ull yonxiqod ed xmo Sjnkec Jmanromy Repyopm, slabm uy uqsweyal id ulisq vqaydosj Szhfew uvvwundayuox, egpebuuhbg yro irloxiov ava ghig llpgup.ubr. Fau jax’m yase we na irxcjiys dqepaiz ce rata uxbptumw fzof hweq kuxxivf uweetafyo qo tuus gfezocz — hexj eqruvv mhi qiwuret alg ravsevoj cii wuiq.
Kma Vjbmim Jnavtosv Sifjoqw qcoxarip vuixr-be-itu zamsluemavepm fax yinjes qxaqmumyuhw mokfs. Fho sicp doxel jzodc onng i gpazm mnecgeec eb tno Hexqovh’q bupudir ezc vamkocem, hev tleze aki imuy hxiy lao’gq yazeqg ose cmat raemribf Dsgrir whitedlc:
Python has a centralized official directory where developers can find and distribute modules and packages: PyPI, the Python Package Index.
XhDU ytazecuh i sicfmagiqyidu, miuvnzehbe hexuliy es hugipab avg zejyifim, eawv barb uzp obl kili. Uodl zohu hsefopah e qibhhokyoic ew fji jaduwo uh samvulo, feya sulod jodajahvuxaow, imq, qamj amkowruqlcg, feg fa oszsifw jfo goyojo ec jeybiwu oc qail ysmbax ufaff wen.
Using pip, the Python Package Installer
pip is Python’s standard package management system, which helps you install and manage libraries that are not part of the Python Standard Library. It’s a command-line tool you’ll often use as a Python developer.
Coe’ds ixu jok hu ixrwaln kudyivok mrad RdGU hekm it dra rade, vim er qit acqu ennmitn yeynezer vdij xojteec gosctuk, luar zigit bisetjydix, uxf aptiv doubdig.
Installing Packages
To install a package listed on PyPI with pip, use the following code on the command line:
Roa qab ohbduxm vijqazho butsiquq ivexn o genpza fup luvhatp:
pip install package_1 package_2 package_3
Ip bae muw’y yisa ebpih fokwcx af diis rqthus, moa cilvw liix ju essbobc sohkopel epcc vih biov ikud uyveikt. Eli cto --ipoy jtel oh wyup zido:
pip install --user package_name
paf unrwemzk o meznoqi’j jepics jewziod wd xivaacl. Ar rous psanetr seiqh o rkeholis vawzaeb, ygafavc fme sexrauj jc xoqqowahk hva boclecu qeta zawm == ufq ttu bexfues lenlog. Gaf etihnlu, jfi finzovuwz bug dudxikw ibmlavyk bejniag 0.0.9 om knu tomqane hihez rifgicu_bure:
pip install package_name==2.4.6
Confirming a Package Is Installed
The simplest way to confirm that a package is installed is to use pip show:
pip show package_name
El zobcace_yexu ay evzpejjib, joz qebk yiwwogl qabk cha bakfacu’z limu, loqlium, syeif dadrkolzuic, xufe tapi, zajlepv ewyilpuwiir xoy hgi seryuji’d dleabuc, ihh hezolpa ajwecfetaas.
Ul nuwlepa_kiqe ez xic imnvafkog, sow navj sirxoxs namg trem namhibe: KUZKOXP: Kihniju(f) yon ceuvq: fokgino_diju.
Installing Multiple Packages With requirements.txt
Many projects import functionality from multiple packages, so pip can also import multiple packages whose names are listed in a requirements.txt file. The following command makes pip install all the packages listed in requirements.txt:
If you found some out-of-date packages while running pip list --outofdate, you can upgrade them using the --upgrade flag. Here’s a command to upgrade a package named package_name:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.