Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python imported module is None

Tags:

python

I have a module that imports fine (i print it at the top of the module that uses it)

from authorize import cim
print cim

Which produces:

<module 'authorize.cim' from '.../dist-packages/authorize/cim.pyc'>

However later in a method call, it has mysteriously turned to None

class MyClass(object):
    def download(self):
        print cim

which when run show that cim is None. The module isn't ever directly assigned to None anywhere in this module.

Any ideas how this can happen?

like image 486
leech Avatar asked Nov 07 '12 17:11

leech


People also ask

Why is Python not importing module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do I fix Python import error?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

How do I resolve ImportError no module name?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

What does __ init __ py do in Python?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


1 Answers

As you comment it youself - it is likely some code is attributing None to the "cim" name on your module itself - the way for checking for this is if your large module would be made "read only" for other modules -- I think Python allows for this --

(20 min. hacking ) --

Here -- just put this snippet in a "protect_module.py" file, import it, and call "ProtectdedModule()" at the end of your module in which the name "cim" is vanishing - it should give you the culprit:

"""
Protects a Module against naive monkey patching  -
may be usefull for debugging large projects where global
variables change without notice.

Just call the "ProtectedModule"  class, with no parameters from the end of 
the module definition you want to protect, and subsequent assignments to it
should fail.

"""

from types import ModuleType
from inspect import currentframe, getmodule
import sys

class ProtectedModule(ModuleType):
    def __init__(self, module=None):
        if module is None:
            module = getmodule(currentframe(1))
        ModuleType.__init__(self, module.__name__, module.__doc__)
        self.__dict__.update(module.__dict__)
        sys.modules[self.__name__] = self

    def __setattr__(self, attr, value):
        frame = currentframe(1)
        raise ValueError("Attempt to monkey patch module %s from %s, line %d" % 
            (self.__name__, frame.f_code.co_filename, frame.f_lineno))        

if __name__ == "__main__":
    from xml.etree import ElementTree as ET
    ET = ProtectedModule(ET)
    print dir(ET)
    ET.bla = 10
    print ET.bla
like image 80
jsbueno Avatar answered Nov 10 '22 08:11

jsbueno