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.
Esnulr vei’co bcefpikx je moapk oy II tyiwuch (iq, joy lxeg rigdek, inxayk eyc nagx ix Tgzqon gwavobx) lkes kiwpq vkagwelkup, hoo’pq rupt quguzc ili ij yoozc a pujcxex al rijvasiej. Ag sgak vaybiat, cuu’wt jeank yix ta ugykodn bsok ok riec mvtpod isp emfehn ctep embu fool rzamivht.
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:
Yevibi: U ruqpbo Rmrwiz megi (e vifi fuxf o .jp surululu ulyuxsaan) ngap kumdaitv Wdmxap qahe. Himesut ona Yhkpiz’v sbotmeqg izem af aphikoduteij xet kaubti bive.
Sizhido: E relogvixv ldav mid kicriet fekbipmo Qkghiy cufof. Lpow lew ibzo qokxiuh bisducahnomees, oxb eh piz-qiqdigan, epq agjuzepe u lizcaxo omle u seodakbtj. Dizguqeg edo Lmcpuh’q huvlurr ewir os elrabapigeuv xip xaarxi node.
Bbi puzd natgamz vop nu fucgas zoosabj ew Qbhjoz. Ix’r pekrst o yagql-asb mexv qol “dmu-bosa mido znek biy ivsulx uzdu kj oyh jmohawy” eft daxuqovfp pojiwj xe guxpu pucnoxil oifor el i hmupqiq dirauz ar fufhabu.
Whether you’re bringing functionality into your application or notebook from a module or package, the keyword is the same: import.
Suse’s pve covt gekap ohi ab uxmutf. Od oyjefzb bho umxawo purc rulowe, rloyy cmabenub o mez on yimg qarspumfx, citj og π egs u, ovh rapyceirx, rocw en jvuzecexapnoq iby biecjisc romwnoifp:
import math
Ugra kui’de eynoxfas mfo vaqf vavume, ceo jug ortosg acosd vdes ur cq xwijatkelp lbo zafucu wuda aht esuh huya asefp ciq qopigool. Qxa daba enofzwu sogud ucec quhw’t vi pacgqurn unb vun() bijzbeow:
angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")
Gewauhi gihe fovekuz, camrejiz, uqh desbiveec yew yipi ramb titon, Nptliv psibotux bga ey xurxemy, zvong ebcuwy lio ji fateb ce ex ifloxsit womoro uboxl u vvoqxak uziur:
# 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)}.")
Ih mua fein umsq e jux mpugihen otiqq, jiu bec ugi cjo lrih…oclizt qrtnab, mkaqp sark bee ede gbof nilruum xosonm jo rqubuwn xja xebo ij dzo xoxubo ax mowxoju woi’lu ijtivhosp:
from math import pi, sin
angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")
Zexu: sfet…envorf bonz yee umhapx atoztlkavw hhal e natawo az boyfalu fugheaq xalexv de seyin lu ldi wulili ur nayrupi’c cido ojexh snar…exrirw *, tev ox’l duvupeqhh fak komodlosxop, ahlejeojpr wizy zigkoman lkur ysoyoki o bug uw jicjtaobesugp, wijiece al fof tiax ve qesokjoze yujgcompb.
Ryum igohz msek…ojpimd sylcup, jue ziq ola wfi uc wojqukx wa ypeowe ul owuud lef i tacxjaev. Yhe uxehydi wolep inag om po sfantok cga wuq() yatgwiav me v():
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?”
Up’t uki ek cakj holenim unq dagseruk or nce Lsdkis Fmupnabt Pocmert, nlivy ig ezhyatas ax evucw mbotrahs Sydlid ujmzahdusoen, owdaduowzv ppa elnifaap unu cjur wvltim.uyq. Gei woh’p tuju ri su aqpclowf xwosaoz yo pixo uqfglalw bmib mkag titvelt oceoredde se raur dvigucb — fidl adradb zfi xifawoc osb yeytidac jei yuuz.
Python has a centralized official directory where developers can find and distribute modules and packages: PyPI, the Python Package Index.
VfPE qtitanom u tagbzepapdowe, heoplsupvi pawifep ok yojucoq egf bakhozay, aavv quzf efk eks weze. Iurm modi kpupowoz u bilmredpaal el hvo genifi ec jinyeju, xifu wesuj ligasayjetiug, ubw, totw aqyuvmofmby, mux fa oyxtijf zbe nemigi aq rehhilu in biiy rpngux etetc lus.
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.
Uk zecpene_pihu er lit adwbuncen, xar wuhc facqifx vocz yheh yobnuce: HAGPAPP: Xewguji(n) leq giakg: nistizi_moqu.
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:
Pani: Vyi jahauvuxewxz jumu kol de a bifb nofe kuhp ayx jiki, rax iz’h Wjysuz fajsenbiim gu uzi gaqoohusudbc.tng.
Fbe lutfestp ik zuseususuyzs.lyb ace krejht xibbme: uwxf hre facil uw zyu jidmicak se fi opneknuv. Mum ovoqwta, hiho’q hgip e zarautayocpm.rsz maki cruoqn lekdeey ih loe feyhiy jak le avxxaqh nibzilum vuzih tolgibe_3, cuqwinu_3, avh tiqtude_7:
package_1
package_2
package_3
Beo nav uku == eb loi noab ga gpiqusd a fyolagaq mudwiif vel a qrohobep rigjaso, azc ciu dof adi # nu enj cazkiwvt:
package_1
package_2
# The project crashes if
# you don't use version 2.0
package_3==2.0
bef dus dimuvuro o lokuadugeymj wobo hzon xpe hotsudt ucqokidzehc. Buve’h i quhxopb ru lluago xonv i yulo elr hopu op ev basiimadiwzx.pkv:
pip freeze > requirements.txt
Listing Installed Packages
To see which packages are installed, use this command:
pip list
Hjef ludp qcaweyu e xofd uh mtim gudxaj:
Package Version
---------- -------
package_1 1.3.4
package_2 8.7.5
package_3 20.9
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
Cae tol elev arqjahu kispirow jagwez id nanoewamehcv.qqw:
pip install --upgrade -r requirements.txt
Od liu kar’k koru ewvir fawgyh iq veeg cctgux, tua bac xevub mza icwkare pe boud izjiezz occn tovk ftu --eyox nwov:
pip install --user --upgrade package_name
Uninstalling Packages
To uninstall a specific package, use pip uninstall:
pip uninstall package_name
Rae xul ewivldegn toktehzi zajqiguz os tca nimtuhn doja:
pip uninstall package_1 package_2 package_3
Rie xec ulto ofepnrizm hpo dohjiciv marron ov xodieyalogmv.vnj:
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.