简体   繁体   中英

Python treat module name as 'NoneType'

I have a piece of code that behaves strangely.

At the beginning, I import a module, which is a python binding for a C library.

try:
    import pyccn
except:
    print "ERROR: PyCCN is not found"
    exit(1)

Later in my code, I use pyccn module to do quite a lot stuff, and it was working as expected (almost). Now after working correctly for quite a while, it gives me the error:

 Traceback (most recent call last):
   File "./ndn-ls-keys.py", line 185, in upcall
     if kind == pyccn.UPCALL_CONTENT_UNVERIFIED:
 AttributeError: 'NoneType' object has no attribute 'UPCALL_CONTENT_UNVERIFIED'

So it say 'pyccn' is NoneType!! But it was working, I mean the same function that includes line 185 was called multiple times before the error happens. And the error happens consistently. I didn't redefine 'pyccn', was just using 'pyccn.foo(), pyccn.bar(), etc'.

What are the possible reasons that this could happen?

PS The error happens at the end of my script. If I put a time.sleep(10) there, then it happens after the sleeping...

Thanks!

I somehow solved this problem. Originally, immediately below the import for PyCCN, I have two other imports:

    import xml.etree.ElementTree as ET
    import time

So the imports are global to this file.

Once I moved these two imports inside the function where they are actually used, the problem went away!! The move is the only change and I don't know the reason behind this fix.

Perhaps there is some conflicts between the imports due to some problem in pyccn module? (I assume the standard libraries are not responsible for this problem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM