0

I was hoping I could get some assistance with my python module installations, specifically around the ipaddress module. This issue is driving me crazy...

In short, I wrote a python3 script on my Windows machine that utilises the ipaddress module. This works absolutely fine.

I've copied this to the Linux box (Ubuntu 18.04) that I want to run it on, but when I run it, I get the following error:

File "/opt/netbox-2.6.7/netbox/reports/address-parents.py", line 82, in test_aci_endpoints
  if endpoint.subnet_of(summary):
AttributeError: 'IPv4Network' object has no attribute 'subnet_of'

When I query the module, I get the following:

nbxla01lv:/opt/netbox/netbox$ pip3 show ipaddress
Name: ipaddress
Version: 1.0.23
Summary: IPv4/IPv6 manipulation library
Home-page: https://github.com/phihag/ipaddress
Author: Philipp Hagemeister
Author-email: phihag@phihag.de
License: Python Software Foundation License
Location: /home/andy/.local/lib/python3.6/site-packages
Requires: 

This is slightly confusing to me as looking at that homepage for the module, it seems to be a port of the 3.3+ ipaddress module for python 2.7. Regardless, version 1.0.23 is the latest release and contains the function 'subnet_of'.

Also, if I look to the actual code itself in /home/andy/.local/lib/python3.6/site-packages/ipaddress.py, I can see the actual function in the code:

nbxla01lv:/home/andy/.local/lib/python3.6/site-packages$ cat ipaddress.py | grep subnet_of
        if not other.subnet_of(self):
            if other.subnet_of(s1):
            elif other.subnet_of(s2):
    def _is_subnet_of(a, b):
    def subnet_of(self, other):
        return self._is_subnet_of(self, other)
        return self._is_subnet_of(other, self)

I'm sure this is something simple, but any help would be greatly appreciated.

Thanks!

Edit - sample code

# Query APIC for all endpoint IPs. 
endpointQuery = '/api/node/class/fvIp.json'
resp = requests.get(aciBaseURL + endpointQuery, cookies=cookie, verify=False).json()

ipAddressCount = int(resp["totalCount"])
aciIPs = []
counter = 0
summary = ipaddress.ip_network(inputSummary)

while counter < ipAddressCount:
    endpoint = ipaddress.ip_network(resp['imdata'][counter]["fvIp"]["attributes"]["addr"])
    if endpoint.subnet_of(summary):
        aciIPs.append(str(endpoint))
    counter+=1
AndyB
  • 29
  • 7
  • A) Could you write a short bit of code to reproduce the problem? B) Have you considered using pipenv or similar so that you get exactly the same versions of packages on both installs? – FiddleStix Jan 17 '20 at 11:18
  • I've edited my original post with the sample code, thanks :) – AndyB Jan 17 '20 at 15:10

2 Answers2

1

The ipaddress module is part of the standard library so I guess you're importing that version.

You can also verify which module you're actually importing

>>> import ipaddress
>>> ipaddress.__file__
'/Users/rickard/.pyenv/versions/3.7.4/lib/python3.7/ipaddress.py'

Most likely the subnet_of method is missing from the ipaddress module in your current installation of Python (looks like 3.6)

Rickard Körkkö
  • 548
  • 3
  • 10
  • Okay, looks like we're getting somewhere... `>>> import ipaddress >>> ipaddress.__file__ '/usr/lib/python3.6/ipaddress.py'` Which I just checked, and doesn't contain the 'subnet_of' method. So next question, how am I best fixing this? Upgrading python on the Ubuntu box? – AndyB Jan 17 '20 at 16:06
  • I would recommend upgrading Python. – Rickard Körkkö Jan 17 '20 at 16:11
  • Thanks so much for your help, just wanted to say that the upgrade to python 3.7 fixed the issue. – AndyB Jan 18 '20 at 12:30
0

If you don't want to (or can't) upgrade Python, you can follow the concepts in How to import a module given the full path?

Rather than importing via:

import ipaddress

you can choose a different version using:

import importlib.util
spec = importlib.util.spec_from_file_location("module.name", "./virtualenvs/dev/lib/python3.6/site-packages/ipaddress.py")
ipaddress = importlib.util.module_from_spec(spec)
spec.loader.exec_module(ipaddress)

Just change the path to reflect the correct module location.

Scott
  • 113
  • 6