so here's what I want to do:

import module

mvar = module()

I've seen it done with the PyQt modules,
but I don't feel like researching a complex code just to figure out a simple usage. :P

anyone know how this works??

Normally, you can't do that and you should not be doing that. However this is python and most things happen to be possible. A trick is to replace the module in sys.modules by a pseudo module object with the same dict and a __call__() method. Here is module lemodule.py in python 2.7:

# -*- coding: utf-8 -*-
"""
    lemodule.py
"""

if __name__ != '__main__':
    import sys
    class PseudoModule(object):
        def __init__(self, dic):
            self.__dict__ = dic

        def __call__(self):
            return 3.14159

    sys.modules[__name__] = PseudoModule(
                    sys.modules[__name__].__dict__)

Now here is the program main.py which does what you want

# -*- coding: utf-8 -*-
"""
    main.py
"""
import lemodule
var = lemodule()
print(var)

"""my output -->
3.14159
"""

Edited 2 Years Ago by Gribouillis

lol IKR :P

yours is slightly better though for the class init WITH the module dict.
very nice :)

This question has already been answered. Start a new discussion instead.