I'm not entirely clear what it is you are wanting to accomplish, but I'd
think a better option would be execfile()

At any rate, to answer your question more directly, you could do something
like:

*import* __main__ *as* m

*def* foo(modname):
    mod = __import__(modname)

    m.__setattr__(modname, mod)

foo('shutil')

help(shutil) *# now this name exists in this context*

On Fri, Aug 5, 2011 at 2:03 PM, Renato Polimeno <[email protected]>wrote:

> Thanks Adam,
> could you please send me an example about that ?
>
>
> On Fri, Aug 5, 2011 at 3:58 PM, Adam Mechtley <[email protected]>wrote:
>
>> You would need to add it as an attribute to __main__ for that to work
>>
>> On Fri, Aug 5, 2011 at 1:50 PM, Renato Polimeno <[email protected]>wrote:
>>
>>> I don´t know why my result "is not defined" by the way,
>>> I thought it would work using __import__ inside a def..
>>>
>>> def importModule(modules):
>>>     for mod in modules:
>>>         try:
>>>
>>>             module = __import__(mod)
>>>             for d in module.__dict__:
>>>                 print mod, d, '\n'
>>>
>>>         except ImportError:
>>>             print "Unable to import %s" % (mod)
>>>
>>> ##
>>> importModule(["maya.cmds"])
>>>
>>> maya.cmds.getAttr("someObj.tx")
>>> # Error: NameError: name 'maya' is not defined #
>>>
>>>
>>> On Fri, Aug 5, 2011 at 1:26 PM, Renato Polimeno 
>>> <[email protected]>wrote:
>>>
>>>> Hey guys, I´m starting digging out some python codes and I´m testing out
>>>> my own def to 'batch import' necessary modules... ie.
>>>>
>>>> def importModule(modules):
>>>>     try:
>>>>         for mod in modules:
>>>>             module = __import__(mod)
>>>>             for d in module.__dict__:
>>>>                 print 'module= ', mod, 'dict=', d, '\n'
>>>>     #
>>>>     except ImportError:
>>>>         print "Unable to import %s" % (modules)
>>>>     #
>>>>
>>>> Using importModule(["maya.cmds","pymel.core"]) works fine.. But how
>>>> could I implement a list of strings more efficient like "import maya.cmds 
>>>> as
>>>> cmds" or "from pymel.core import *" ?
>>>> Do you guys use something like this or is it better to keep it simple
>>>> and standard?
>>>>
>>>> Thanks,
>>>>
>>>> --
>>>> ______________________________
>>>>
>>>> -> Renato Polimeno
>>>> +55 11 7539 3615
>>>> renatopolimeno.com <http://www.renatopolimeno.com>
>>>>  
>>>> <http://www.renatopolimeno.com/>facebook.com/renato.polimeno<http://www.facebook.com/renato.polimeno>
>>>> @renatopolimeno <http://www.twitter.com/renatopolimeno>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> ______________________________
>>>
>>> -> Renato Polimeno
>>> +55 11 7539 3615
>>> renatopolimeno.com <http://www.renatopolimeno.com>
>>>  
>>> <http://www.renatopolimeno.com/>facebook.com/renato.polimeno<http://www.facebook.com/renato.polimeno>
>>> @renatopolimeno <http://www.twitter.com/renatopolimeno>
>>>
>>>
>>>  --
>>> view archives: http://groups.google.com/group/python_inside_maya
>>> change your subscription settings:
>>> http://groups.google.com/group/python_inside_maya/subscribe
>>>
>>
>>  --
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings:
>> http://groups.google.com/group/python_inside_maya/subscribe
>>
>
>
>
> --
> ______________________________
>
> -> Renato Polimeno
> +55 11 7539 3615
> renatopolimeno.com <http://www.renatopolimeno.com>
>  
> <http://www.renatopolimeno.com/>facebook.com/renato.polimeno<http://www.facebook.com/renato.polimeno>
> @renatopolimeno <http://www.twitter.com/renatopolimeno>
>
>
>  --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to