Reputation: 79
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: [email protected]
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
Upvotes: 0
Views: 3049
Reputation: 121
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.
Upvotes: 0
Reputation: 558
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)
Upvotes: 1