PIP is a package management system that installs and maintains Python software packages. Since Python version 3.4, PIP is included by default.
On Windows, you can find PIP in the following folder:
PIP looks for packages on the Python Package Index (PyPI), a repository for Python packages.
Let’s look at a package. Since we are network people, let’s check out the networkparse package. Networkparse makes it easy to work with hierarchical network configuration files. This is the project page:
Networkparse is well maintained and has good documentation. We can install a package with the
pip install command:
pip install networkparse Collecting networkparse Downloading networkparse-1.8.0-py3-none-any.whl (13 kB) Collecting dataclasses<0.7.0,>=0.6.0 Downloading dataclasses-0.6-py3-none-any.whl (14 kB) Installing collected packages: dataclasses, networkparse Successfully installed dataclasses-0.6 networkparse-1.8.0
I’ll use one of the examples from their documentation. First, we import the package in our code:
from networkparse import parse
We’ll create a variable with a string which represents the running configuration of a Cisco IOS router:
running_configuration = """ hostname R1 ! boot-start-marker boot-end-marker ! no aaa new-model ! mmi polling-interval 60 no mmi auto-configure no mmi pvc mmi snmp-timeout 180 ! ip cef no ipv6 cef ! multilink bundle-name authenticated ! redundancy ! interface GigabitEthernet0/1 ip address 192.168.12.1 255.255.255.0 duplex auto speed auto media-type rj45 ! interface GigabitEthernet0/2 ip address 192.168.13.1 255.255.255.0 duplex auto speed auto media-type rj45 ! router bgp 1 bgp log-neighbor-changes network 22.214.171.124 mask 255.255.255.255 neighbor 192.168.12.2 remote-as 2 neighbor 192.168.13.3 remote-as 3 ! ip forward-protocol nd ! no ip http server no ip http secure-server ! ipv6 ioam timestamp ! control-plane ! line con 0 line aux 0 line vty 0 4 login transport input none ! no scheduler allocate ! end """
We need to parse the configuration and assign it to the “config” variable:
>>> config = parse.ConfigIOS(running_configuration)
Now we can do some cool things. For example, show the IP addresses on all interfaces:
>>> interfaces = config.filter("interface .+") >>> for interface in interfaces: ip_address = interface.children.filter("ip address .*").one() print(interface) print(ip_address)
The above code prints the following messages:
interface FastEthernet0/0 ip address 172.16.2.1 255.255.255.0 interface FastEthernet0/1 ip address 172.16.3.1 255.255.255.0 interface FastEthernet1/0 ip address 172.16.4.1 255.255.255.0
This is a simple example but now you know how you can install packages with PIP.
You have now learned how to use PIP to install packages for Python:
- How to verify where PIP is located on your computer.
- How to look for packages on PyPI.
- How to install packages with PIP.
- How to use packages in your Python code.
I hope you enjoyed this lesson. If you have any questions, please leave a comment.