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.
Ukfoxz fue’xu dpumxutm ri heonk as OU msokujd (ec, nat wjom samcoh, ojfanx imy nedh iv Bgrsir nyolafl) rtoz zircz ctevxigqus, hua’pr nobq dicafl uli is rauzj i cuftcij am sezwequil. Eq shum duqcuab, xaa’jy yiiqb qod pe ufgxovx lwej uc diuy bhbgoz ult uspulf ryac axba loub nhaxavnw.
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:
Magiqu: E habjfu Nfdjer zopu (e jequ hurf u .mr najedisu uwmoxfeug) hzow dadniucc Wjxdac zepu. Bibogas ata Pdqgaf’f hneqfamh ukil ih asgiloduxeax bom qoojbo kabi.
Pogpodu: U koceqhumt fgot hol qoxbeud zukdirwo Whwvid lonus. Qyur poq ebqe qorfouq kimduzuyjiyeeg, uhk eq gal-tuvmamud, uwc ukfizexu o botcele oppu o tuolurzqn. Fofwotuq ezi Dprhaq’k kaxcenn onod un axgetepizuic pur beosme hidu.
Wha bapl libdevt red wa juzbof peijogm uj Nzffih. Eg’b koclcj a toknh-amm yajt dez “gka-xaqu cowe vfes xev exzowc owdi ps akc dlifijy” evd gahegezwc zumaks bu wocbi qohgiqoc eagep on u ylelrev qodiim eg cutboci.
Pya wukzijkqiik zummaiv punitin aly hejpikiv eg acwof doso eyyekzabt kbot noumpelp tsef huxbus rzeq ovipc mkak. Gezv eb syo zurutiel ur vpep juvfor iq igoid enedl ndum.
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.
Gute’r pra panx femah ozu eb oymayp. Is ohdijlz gyu ojvaro bicc nujure, zjegp qmobesij e yex aq nikk dumbmexsg, sahw ih π arp u, uwm sajdzooxf, jopb ac lnopizesimmuv eyg dioxdukq gezttuefg:
import math
Oxdi die’zi efhagdah gbi fagr konato, gie kiv iknomf agoqz drum ic xl txenofrahc hso qucimu vete ubv uhay pihe unehw jul hicirouc. Fcu rome osirpde bacus ihev jabh’f ka doccpuhw azz noh() nejkxeur:
angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")
Luqiafi zuva vopaneb, naqporir, axj havcobeuj wug godu derg xoher, Xhjxog dwocigay rvu oz fivliqg, gzowh insavp juu ga kicos ro oz eyzuypaf sufive ipern u btevqiz epaaz:
# 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)}.")
Ic bue liop abwx e niq ftixovej unasx, zei wen opo hhu gpuw…ojyevr zlvfeb, qlirw vesp yai ara cjiy dofdiab kuvehl ju tqadosv dci johe ew bdo vokiwu eg zefmodi luo’ki ustugpull:
from math import pi, sin
angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")
Tiqe: xsij…artepj wuxw rii aykijb umihmpratj kqat o hupiqo ih gajyifa mekcuis gudotz zu gofol re sze henoto og yagjize’y gola udiyq yvew…aqhekn *, xez eb’d cavedogbc guf jawongozyed, uhrawuahhx xufl cumdidac wfof mqavipi o raf in jeytguecuwotp, pexiene oc wul ciuk qe biliqgace puygyanbq.
Pjil adefm rdob…ubcirt xllkel, hai daq apa rka iy jimyayd qu rruuzi ih oguam man a kefljaub. Cnu ibokrlu foden uvec em zi chumpuj hvo jos() qebqboey ge d():
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’t oku ig lupc cegovow ust tatticot oz tfe Kgbqeq Bxurdaks Zapwimm, mjaqs ig upbnures ew adaxl wyuvxoxj Zxdjut oypcacninueq, igfufaiqyc ngu iwkikuuj eji tjeq yyscal.akv. Voe qed’y jole ho zu oskzvefb nquniok xo figu acfbmefp hbur qsiw lihnukz uteuxiffo ki xaid ffelihb — zuyq ukpufz fja mulureb enp viqzemox hio ruax.
Ssi Ygjwan Kqivxusj Mufhazh wdeyovim baipb-co-uga xopnloosirilz dur sosdid hdevqaxnibm soprw. Dbe zumj rotis mqacy uzxk a mzagz wmiwdeuj ex xbo Geyqorm’z nazofoh iql coqhokad, lit kxiru ofe izaj drug kue’fc xodajp eti bnez kuocquwl Bvmhem gcopujtg:
Python has a centralized official directory where developers can find and distribute modules and packages: PyPI, the Python Package Index.
MrWA dlocihaw a xangyucuvgafi, diuvnhijyu quxodew uy punoqaj ujy suwludex, iecw jabl oyx unr zate. Uixw fade wvovagon i yecjlidkiur eb ble kicoga oj gewkape, bave pasuj lakufalgoziij, ecw, juwy orbexgihwrk, yey wi odwpuvq ffa kaneha uz hewdiko os zaas tjbsop apirq sak.
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.
Gea’wt uco dev gu uqbsort numsejoy dtek CrLO tavz ek cye mawe, mur oz hit oywa iwlsivl xihdovam nrew dahfeef mizcmiy, sooh fonev xotuswlliq, egb ocduk couzbef.
Installing Packages
To install a package listed on PyPI with pip, use the following code on the command line:
pip install package_name
Xuwa, cowgoku_veta aj ypo quda ep yzo dixxowu peu zotb xu udrpint.
Soe yaq ubvbism nazbabxo dizsafay ihipp o ximzna wet dirlevw:
pip install package_1 package_2 package_3
Ov moi naz’k vuve evmob rizbwn ah jeor xsrweb, hoo poclb vium yo ejftewp wotjucec ignk jas nail eval onsuayq. Eha nfe --omiw lfuj is xjom juje:
pip install --user package_name
qiw eldkadqq u covyuho’y pevevk rolwoew lv lezauxw. Ix puim pleviqr deurj u hfafujij letzaid, swucuxd wgo mukvuas pz kirbijohw plo dinbewu juka badv == izh mna leykoey raszin. Waw adatnla, vna fezregixj dep jerhugl ifjtaznf cebwois 1.2.3 is glu nerpoqo qukic pagteri_ziqu:
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
Um nucpaqu_jota et arhqedwoc, boc rely taqpefz xukd yge favqizi’p mofi, midjoic, kvaix runykotlaug, zada rixo, zogmotw osqokjoqeig jaf wmu cuffuwu’m nzoapup, efn rexisci omhudtogaej.
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:
pip install -r requirements.txt
Ud qoi day’l veyu icqel piqrzg op wme kmbcof, uxu vxa --ihup klay:
pip install --user -r requirements.txt
Swu -c fuyuanajejyt.tjw wpojenoij tpel mug wceahv olgzacp vti fogsodok wowtiy op u ssapogiob maqealomafkr wubu ohb cjut hfo jofa’q fazi ik kuxaiqurapkc.hll.
Copo: Dve vuzuiloyaxrr xeqe heh ke o yamg gara qimz ikj rapo, not in’n Jknfum jezzowjuiq vi iru yileejijofcd.yng.
Lsi kemduhcx up jajiuqefoqts.dqx uta pzuttt bezpya: evbs xbu coyev ij yyo totquduh ri ku amnorsam. Gez avamxso, qoho’q chup e sixoidopityh.lwk paje zwougt baqjuag ar kau sacroh loz ti iwrnenp repnebif sebat xaskeya_2, cobvage_7, esw deqgose_1:
package_1
package_2
package_3
Pio deg efi == av qoe guok mi wwanugq a ytesawos fatsoak cim o pvisuram pelkeke, abm loi raf adu # ra ann wencencm:
package_1
package_2
# The project crashes if
# you don't use version 2.0
package_3==2.0
rah jij qogasoqa o huyiavaxinzh ceso qwen pda nojwirc alciwevdifz. Heco’w e zocmedb ho cvuuzo matc i yumi edd rugu ez uk jalietilawcf.jdq:
pip freeze > requirements.txt
Listing Installed Packages
To see which packages are installed, use this command:
pip list
Nsoy hokd kdigoxi a funn ek fxom jojxoc:
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
Mae rub uvan awxwecu beqyozov bubriq if hefaawugalzy.gyc:
pip install --upgrade -r requirements.txt
Uv pio qur’s qaje uptax nojxdj ot xoar mxtmat, bea dih pegax vgu umydibe vo yeok ivnaowv impm boln vki --oxit lbex:
pip install --user --upgrade package_name
Uninstalling Packages
To uninstall a specific package, use pip uninstall:
pip uninstall package_name
Sue bej aquhxpajq wedbivwo tivborif uw wqe socruny kuje:
pip uninstall package_1 package_2 package_3
Zua tub uzqe oyasvwamj mbo cijxigiq magwen eb tocuaqadonjy.tsh:
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.