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:
C:\Users\renemolenaar\AppData\Local\Programs\Python\Python37-32\Scripts
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 1.1.1.1 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.
Conclusion
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.
Hi Rene
I am having issues importing parse from networkparse. Currently getting the following error:
I have added the path to python manually into Windows 10 but sadly that has not helped.
As an aside can I also ask you just to walk through the meaning and sy
... Continue reading in our forumHello Frank,
When you get an error like this:
It’s always related that Python is unable to find your module. In this case, it seems that the networkparse module is trying to import the pyparsing module. We can see it here:
When you look at the parse.py file, you can see this on line 23:
Try using pip to install pyparsing:
That shou
... Continue reading in our forumHi Rene,
That is much better, and I now get no errors, and all commands appear to run.
I just don’t get any print from the code in the last phase, using your configuration file and/or similar .conf .txt etc.
Thanks,
Frank
Hello Sushil
Your particular case seems to be different than that described by @faithfps in his post here. In the output you shared, there is a NameError that states that the running_config variable is not defined. Make sure that, as in the lesson, you have parsed the configuration and assigned it to this config variable. Take a look at this particular configuration and let us know if you were able to resolve it.
I hope this has been helpful!
Laz