why do I get a NameError: name 'AF_XBEE' is not defined

I am trying to setup a local Digi Python environment to run my unit tests locally. Here is the quick summary of my steps:

$ virtualenv digiTestEnv
$ source digiTestEnv/bin/activate
$ digiTestEnv/bin/pip install xbee

everything good so far…

$ python
>>> import xbee
>>> from socket import *
>>> sd = socket(AF_XBEE, SOCK_DGRAM, XBS_PROT_TRANSPORT)

here I get…

Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘AF_XBEE’ is not defined

1 Like

I’m just setting up a similar project for the first time and am getting the same thing. Did you figure it out?

Yes, here are the updated steps that worked for me to get past this error (NOTE: you will need to download a copy of the py-for-PC source code):

  1. From the command line (myDigiPython can be whatever you want):

$ virtualenv myDigiPython
$ source myDigiPython/bin/activate
$ pip install xbee

  1. Once the XBee module is successfully installed, replace the zigbee.py in myDigiPython/lib/python2.7/site-packages/xbee with the one from zigbee_py-for-PC

  2. In order for the Digi socket extensions to work, you will need to modify the socket.py that comes with standard Python2.7 library. Open socket.py and change the following lines:

from:

family = property(lambda self: self._sock.family, doc="the socket family")
type = property(lambda self: self._sock.type, doc="the socket type")
proto = property(lambda self: self._sock.proto, doc="the socket protocol")

to:

family = property(lambda self: self._sock.family, lambda self, value: setattr(self, '_socketobject._sock.family', value), doc="the socket family")
type = property(lambda self: self._sock.type, lambda self, value: setattr(self, '_socketobject._sock.type', value), doc="the socket type")
proto = property(lambda self: self._sock.proto, lambda self, value: setattr(self, '_socketobject._sock.proto', value), doc="the socket protocol")

Hi Mishka,
I’m trying to run xbee programmes by local python too, and I face the same problem with you. I’ve followed your step, but the problem hasn’t fixed. It still shows: NameError: name ‘AF_XBEE’ is not defined.:frowning:
Can I ask what’s the “py-for-PC source code”? and the Xbee file which install in virtualenv is download through python.com?
Thank you so much.