Glowie
Glowie

Reputation: 2309

Python - 'IPv4Address' is not defined

Here is a part of Python script

import ipaddr, ipaddress

if ((IPv4Address(lower_ip) <= sender-ip) and (IPv4Address(upper_ip) >= sender-ip)):
        print "in range"

I get the error

Traceback (most recent call last):
  File "namelookup3.py", line 55, in <module>
    if ((IPv4Address(lower_ip) <= sender-ip) and (IPv4Address(upper_ip) >= sende
r-ip)):
NameError: name 'IPv4Address' is not defined

I imported ipaddr and ipaddress, and %PATH% is set to D:\Python27\Lib\site-packages which has the Compiled Python File of both ipaddr and ipaddress

HELP!

Upvotes: 0

Views: 6522

Answers (1)

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33076

When you import a module, you must prefix all objects in the module with their namespace, like:

import ipaddr, ipaddress

if ((ipaddress.IPv4Address(lower_ip) <= sender_ip) and (ipaddress.IPv4Address(upper_ip) >= sender-ip)):
        print "in range"

Alternatively, you can import the IPv4Address class directly in current namespace with:

from ipaddress import IPv4Address

if ((IPv4Address(lower_ip) <= sender_ip) and (IPv4Address(upper_ip) >= sender_ip)):
        print "in range"

Besides, you will probably want to replace sender-ip with sender_ip.

Upvotes: 2

Related Questions