pip en modules installeren op Linux voor Python
In Python kun je een boel doen. Dit wordt nog verder verruimt met extra modules die je kunt installeren.
Denk hierbij aan
- Het kunnen sturen van mail.
- Tekst kopieren en plakken.
- Gebruik maken van hardware componenten zoals
- WiFi
- Bluetooth
- Webcam
Je zult ziet dat bijna in alle python script gebruik gemaakt wordt van modules. Deze worden altijd boven aan het script geplaatst en zo als eerste opgehaald en ingelezen, zodat deze in de rest van het script gebruikt kan worden.
- import os
- import sys
- import csv
Een boel modules zijn standaard ook al geinstalleerd samen met de installatie van Python zelf, echter daarnaast zijn er via pip een grote lijst aan extra modules toe te voegen. Om dit soort modules te kunnen gebruiken, moet je eerst pip zelf installeren.
Via een terminal omgeving kun je pip installeren met de volgende regel:
sudo apt install python3-pip
Als pip succesvol geinstalleerd is, kun je modules toevoegen.
Om een kleine impressie te krijgen van wat er te installeren is aan extra modules kun je in de terminal invoeren:
pip3 list
Resticties kunnen er voor zorgen dat je niet op een normale manier modules kunt installeren. Hiervoor kun je de module als een script runnen, waardoor je als nog de module geinstalleerd krijgt.
sudo pip3 install <modulenaam>
Soms kan het zijn in verband met restricties op je computer dat het installeren van modules niet lukt. Mogelijk lukt het wel op de volgende manier:
sudo python3 -m pip install <modulenaam>
Nu dat pip geinstalleerd is, en je een module hebt geinstalleerd, kun je die op meerdere manieren toepassen in je script. Modules kunnen klein zijn, maar ook bijzonder groot. Dan is het beter voor de prestaties van het script om alleen een onderdeel van een module te gebruiken.
Om als voorbeeld te nemen het eerdere genoemde ‘os’ module. Je kunt de gehele module inladen:
import os
os.system("ls")
Als je maar één onderdeel nodig hebt van de ‘os’ module importeer je alleen dat wat je nodig hebt.
from os import system
system("ls")
In het genoemde voorbeeld kun je misschien de behoefte voelen om de module of het onderdeel van de module een aparte naam te willen geven. Dit is mogelijk en kan ook soms handig zijn om lange namen te vereenvoudigen.
from os import system as var
var("ls")
Bij het gebruiken van modules is het altijd raadzaam om de documentatie goed te lezen. Python is altijd hetzelfde echter een module niet altijd. Een website die in documentatie heeft over modules is pypi.org. Deze is echter wel in het engels.
Reacties
pip en modules installeren op Linux voor Python — Geen reacties
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>