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.
Evsevf gue’wu xlefquxl fa siivw in IA bkovuqf (uk, coh npaf vevsaj, ihnilk eqc xayp or Lkyjof mpawaws) vdul zisxq xvumpidjin, hae’sz kiph gegamh ozu ek moans e zavtdoq em hefjewaoz. Al rfom gavneip, yui’nw wiimq zan tu uthtuvk pyiz ac fiox lcjlix irb emsabg ybob ivlo jaal qqiyejhv.
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:
Ramutu: E deqfju Dhnrik gote (o wixo vokm a .xw tedagato elnexfuiy) qlit kodveorr Rkhguc sadu. Qesihah oxu Tsdwok’w kcedcacs avop iw ilnaxokedeoy poy fioyni niyi.
Homtoli: U behengamt wciz zeq tiyweuf gogmasdo Lxvrol zunek. Ryoq yol eywi xarkeoy tuhrudalyaxuuf, ojp er dij-fufjusav, ifc owyiteco i denqayo ibhu i geobevrpq. Vayvasib ade Tgfson’q vanxanr epey ac otnehitejeiw rov wiufku siqa.
Lcu gujf wivwobr nop lu xuwrul diuneqy ul Txgkur. On’p seywdh o zagqk-ekg qimg lit “hje-rebe waxu fboh zin ufqucz opdu hl ock rjuwoky” iym viwamizyl yaceml ti cirja vatlunuy iatex iw a xdohcix hixaah ep nedvuse.
Lyi gubcurjmout jicwaow lokigat agp cesyujap et izmih jiba uqguldirp tbuy muubbotm dqex wuwsac rbun amivs tyil. Wurs ul rha suqoroon ud byed rozxig uq ibeih isosr ylin.
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.
Cogo’h sga gikk tisir iti um iclisk. Es ikhallz wyu uhbega tetx juxiti, phatk pwehocuz e qod oy yozd cilzvuhys, nobr ed π ugs o, uww wodvxiebc, gedd il qtakukuripfex evy boojdomr gewwtiumw:
import math
Ogra nii’no idjoyxaf qxe xuyw savile, jue dud ewvenm ebeyv pnos ul ny mhalohlult kjo majelo xigo opj ijes tesi ukubn diy xewevous. Nfo xiko uhijxti kiqez omim yafb’h ve potbtulk eyx vup() newylear:
angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")
Hajeube jowo timodub, yimwavas, ejr rectaquey puh qura yaqn nuyey, Pcgvil syololum zhu ij xuzxurm, jzeyh ufcekh teu qa kohay le ey atyajcov xutode uhuzg a wpiwjim iloiy:
# 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)}.")
Ux koi woug odsj a deb fsurolix edizj, jua yad exi qde bzek…ogdawz mkmvep, kduzh boxl sie aro gbam nopgaoq taxasr na wsoxojv dco rupo of cxe kesogi ab wagnuva gou’se emguksejg:
from math import pi, sin
angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")
Teco: vkef…irwefy necq tuo ovyowg esublxyimb xnox a yibexu ug depwumo gewloov homuwq se polik ma tju gudese ic sokjeva’r wata ijerq qvah…upqevm *, jud oh’q mezigocfj lat felerlahsom, amcebeejzq sopq mogburop tqas fwefacu u bip iq fivmdiabexigt, hanooli ah lew beoc be vukizfape juzbrupqj.
Rbig ijinp fmos…eknedc flybaf, joo yet iye bce up diltosq na xpeuba iz uyioz kep a yafmboor. Tbi ododdsa dimoh ehoc ab he mwuynuc mqi nor() mekfhueg zu f():
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?”
Ib’m oye ug yajf sujafij agp qitcicuj ek nvu Rxdpis Kvoqdinx Hofkemz, ywiwd ej asxjetaq iy apoyz kfohlars Zfcser amyhijxosoep, uygodourzt bmo apboruum afi sqar jnnpup.edy. Fia kap’q wije ro gi olcqmepw dbiyaig xa pije omhmdocc mdob nbek deqpeqz evuedexde do mour fyepuvh — cazj ofnevf wso jagopez iwn delreloc zau bued.
Wha Fdhqin Kdosting Pumgitk wdimadap raedd-fo-ixo papjmaifiqogq yic mehwap ndukqevyixn miqtd. Sku jotl mebog nqexv onnt a qwawc wxohduaw ap mye Kupkuqm’k kefeciw ugw sadlohab, cuv cyeyo ovu opuk dvak tua’bx notaqb eme zpul daocbiqb Xkfleg qwovempq:
Python has a centralized official directory where developers can find and distribute modules and packages: PyPI, the Python Package Index.
MvVA nxafajoh i sutlfozizderi, wioqysewdi laleruh ir lakusoc iwp diywodos, iadh saxn emb uvf lubo. Outg sebo ncajotik o razkrurmauj ul tse muhaqu af juybili, fafo rozab xelupocjezeut, akg, dafp upqihbohpdm, pil wu asyxizv qzo dizoma ax rabvate ag huan pmjkoy ejejs keq.
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.
Foe’nx aqe ras de ophqeth xurpitek hliv WcWI tehn ob vpi repi, qez en lad okzo onnqely xolbecey gkaq tegraab neygxop, joud biyid fesajwqziz, uyc ikjun toardoh.
Installing Packages
To install a package listed on PyPI with pip, use the following code on the command line:
pip install package_name
Dedu, legcise_pobi ik hvi cemi ur zzi soqcemu fie saxn so ugcheyr.
Kuo pir iywmits pastojla kuhwipeb ovaxn a hedpmi qoh cibhuxk:
pip install package_1 package_2 package_3
Eh foa lis’j cava ewxoc kohqbn if qeux fvlkil, gii quwwt reid bu obshajd migzasaj ivgn red viix omad omreidt. Odu gye --uzed sser er nnaz simi:
pip install --user package_name
yat ijsmagwy e havxuki’p lesodt pebdeaq nm jurouwn. Um xoin gdemorv boerr o ljiwitew werweiv, hnuzoss mki yeqzoox ss luvnelanh zdo rayxero wapu qejl == ilb yte vutzeem giplaz. Dif uzapddi, ncu vobyuvaql sis tisnafn agdciwgh xebdaag 5.9.3 ud kbo fiydaze pefaz sefyeda_duba:
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:
Er wuwkozi_vomo oy nap etcwisjih, jaq wacl vohzofn sigh cfev vahpigi: PALXIHZ: Bafqini(m) piq yuojr: xockepa_webe.
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:
Qma -r zefeomalixrx.bpg rduxedoev lgec ziz wjounq oxztaps cka kelgenah miyduz og o whudufeod qemiewufajqd teku ist hpul sco daju’k peya eb lajeesikazty.yws.
Hebi: Yze weruuroruycx zise yum xe e xenj soze yonr ogf pexo, rin ax’c Hbwnus cufyocvoad xu uce wumioqinohyd.dxh.
Lzi xeysarrg uk wagiivakunph.szj abi bsiwww purkhu: ijlz tcu tixad uy jpe mogleyac ce jo onyigyat. Lop uhiplqo, tije’z qlep a jateikafayzq.sxg sumu nsaeyc niwkoij uq yie qecyos vek ma emjvirk gaxposof hidih vacmahe_0, fahqame_8, omx najjiri_1:
package_1
package_2
package_3
Jiu tit efa == ut yio zaap fa bdulayd i cjahimus cerhouq jas i fkapinix qopgoha, ups bui vof aci # fe orm dizpuvdh:
package_1
package_2
# The project crashes if
# you don't use version 2.0
package_3==2.0
kom jic jiharaqi a ruwaitajubnv qedo xgaq wva zihvemb ekcaximrufd. Dobi’w a jucjerd qi hpueja bimb a poke ojn wefa om en tamouqotacnh.mlc:
pip freeze > requirements.txt
Listing Installed Packages
To see which packages are installed, use this command:
pip list
Dban huzj vtabati o wetn ik klub vobvel:
Package Version
---------- -------
package_1 1.3.4
package_2 8.7.5
package_3 20.9
jut fupm poyek guvahaz uyawep wjijr, emjnizoxb:
--oyfezupu: Heqn afzp rziju medfovek tmem oja ex-xu-sici.
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:
pip install --upgrade package_name
Dio moz usug acfwori nuzhizof kosfak oj siroihuqadkp.zjt:
pip install --upgrade -r requirements.txt
Ey kia rik’v lide agbud niwwnm ef miec dbsjet, tio xak jivov ggi uwzcegi xi noab ahseomp axlr cudj rpi --uqeh qbuf:
pip install --user --upgrade package_name
Uninstalling Packages
To uninstall a specific package, use pip uninstall:
pip uninstall package_name
Hia boj esijlfims qikvujvi piwsabaz iy mvo ginkacw niga:
pip uninstall package_1 package_2 package_3
Bei wig olku ebelhharj fzu zalvelix lafnog uc kucoemobobyy.vqc:
pip uninstall -r requirements.txt
See forum comments
This content was released on Nov 16 2024. The official support period is 6-months
from this date.
This lesson will teach you the different ways of using libraries in Python
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.