Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function to do dynamic import?

Reply
Thread Tools

function to do dynamic import?

 
 
bambam
Guest
Posts: n/a
 
      09-11-2007
import works in the main section of the module, but does
not work as I hoped when run inside a function.

That is, the modules import correctly, but are not visible to
the enclosing (global) scope.

Questions:
(1) Where can I read an explanation of this?
(2) Is there a work around?

BTW, sys.modules("filename") shows that the module is
loaded, I just don't know how to use it when loaded that
way. Also, if I import again at the global scope, the module
name becomes available.

Steve.

---
>>> def gim():

.... exec "import gamel"
....
>>> gim()
>>> sys.modules["gamel"]

<module 'gamel' from 'c:\gamel.pyc'>
>>>gamel

NameError: name 'gamel' is not defined
>>>exec "import gamel"
>>>gamel

<module 'gamel' from 'c:\gamel.pyc'>


 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      09-11-2007
On Sep 10, 10:52 pm, "bambam" <(E-Mail Removed)> wrote:
> import works in the main section of the module, but does
> not work as I hoped when run inside a function.
>
> That is, the modules import correctly, but are not visible to
> the enclosing (global) scope.
>
> Questions:
> (1) Where can I read an explanation of this?
> (2) Is there a work around?
>
> BTW, sys.modules("filename") shows that the module is
> loaded, I just don't know how to use it when loaded that
> way. Also, if I import again at the global scope, the module
> name becomes available.
>
> Steve.




(snipped)

This was recently discussed:

http://groups.google.com/group/comp....fcdf49710cb833

--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
 
J. Cliff Dyer
Guest
Posts: n/a
 
      09-11-2007
bambam wrote:
> import works in the main section of the module, but does
> not work as I hoped when run inside a function.
>
> That is, the modules import correctly, but are not visible to
> the enclosing (global) scope.
>
> Questions:
> (1) Where can I read an explanation of this?
> (2) Is there a work around?
>
> BTW, sys.modules("filename") shows that the module is
> loaded, I just don't know how to use it when loaded that
> way. Also, if I import again at the global scope, the module
> name becomes available.
>
> Steve.
>
> ---
>
>>>> def gim():
>>>>

> ... exec "import gamel"
> ...
>

All you have done in this function is bind the module to the name gamel
within the scope of the function. As soon as the function exits, the
module goes out of scope. If you want to use it externally, return the
module.

def: gim():
import gamel
return gamel
>>>> gim()
>>>>

This will have to change to

gamel = gim()

and the rest should work as expected.
>>>> sys.modules["gamel"]
>>>>

> <module 'gamel' from 'c:\gamel.pyc'>
>
>>>> gamel
>>>>

> NameError: name 'gamel' is not defined
>
>>>> exec "import gamel"
>>>> gamel
>>>>

> <module 'gamel' from 'c:\gamel.pyc'>
>
>
>


 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-11-2007
bambam wrote:
> import works in the main section of the module, but does
> not work as I hoped when run inside a function.
>
> That is, the modules import correctly, but are not visible to
> the enclosing (global) scope.
>
> Questions:
> (1) Where can I read an explanation of this?
> (2) Is there a work around?
>
> BTW, sys.modules("filename") shows that the module is
> loaded, I just don't know how to use it when loaded that
> way. Also, if I import again at the global scope, the module
> name becomes available.
>

There's not much wrong with doing this, since it gives you the best of
both worlds. But you mean sys.modules["filename"], don't you?

>>>> def gim():

> ... exec "import gamel"
> ...
>>>> gim()
>>>> sys.modules["gamel"]

> <module 'gamel' from 'c:\gamel.pyc'>
>>>> gamel

> NameError: name 'gamel' is not defined
>>>> exec "import gamel"
>>>> gamel

> <module 'gamel' from 'c:\gamel.pyc'>
>
>

Whoa there! There's a lot of difference between "importing a module
inside a function" and "executing an import statement inside a function".

If you want to do dynamic imports then the __import__ function is what
you need. Trying to use exec like that is a bad idea unless you clearly
understand the relationship between the different namespaces involved.
In fact, trying to use exec at all is a bad idea until you understand
Python better, and even then it's not often a terrific idea.

Think of exec more as a hack of last resort than the first tool to reach
for to solve a problem.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

 
Reply With Quote
 
bambam
Guest
Posts: n/a
 
      09-12-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Sep 10, 10:52 pm, "bambam" <(E-Mail Removed)> wrote:
>> import works in the main section of the module, but does
>> not work as I hoped when run inside a function.
>>
>> That is, the modules import correctly, but are not visible to
>> the enclosing (global) scope.
>>
>> Questions:
>> (1) Where can I read an explanation of this?
>> (2) Is there a work around?
>>
>> BTW, sys.modules("filename") shows that the module is
>> loaded, I just don't know how to use it when loaded that
>> way. Also, if I import again at the global scope, the module
>> name becomes available.
>>
>> Steve.

>
>
>
> (snipped)
>
> This was recently discussed:
>
> http://groups.google.com/group/comp....fcdf49710cb833
>
> --
> Hope this helps,
> Steven
>


def gim():
exec "global gamel"
exec "import gamel"

Unfortunately, does not have the desired effect.
Steve.


 
Reply With Quote
 
bambam
Guest
Posts: n/a
 
      09-12-2007

"J. Cliff Dyer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> bambam wrote:
>> import works in the main section of the module, but does
>> not work as I hoped when run inside a function.
>>
>> That is, the modules import correctly, but are not visible to
>> the enclosing (global) scope.
>>
>> Questions:
>> (1) Where can I read an explanation of this?
>> (2) Is there a work around?
>>
>> BTW, sys.modules("filename") shows that the module is
>> loaded, I just don't know how to use it when loaded that
>> way. Also, if I import again at the global scope, the module
>> name becomes available.
>>
>> Steve.
>>
>> ---
>>
>>>>> def gim():
>>>>>

>> ... exec "import gamel"
>> ...
>>

> All you have done in this function is bind the module to the name gamel
> within the scope of the function. As soon as the function exits, the
> module goes out of scope. If you want to use it externally, return the
> module.
>
> def: gim():
> import gamel
> return gamel
>>>>> gim()
>>>>>

> This will have to change to
>
> gamel = gim()
>
> and the rest should work as expected.
>>>>> sys.modules["gamel"]
>>>>>

>> <module 'gamel' from 'c:\gamel.pyc'>
>>
>>>>> gamel
>>>>>

>> NameError: name 'gamel' is not defined
>>
>>>>> exec "import gamel"
>>>>> gamel
>>>>>

>> <module 'gamel' from 'c:\gamel.pyc'>
>>
>>
>>

>


def: gim():
import gamel
return gamel

Unfortunately, it needs to do dynamic import: I can't list
all of the possible import modules because they are unknown
until runtime.

Steve.


 
Reply With Quote
 
bambam
Guest
Posts: n/a
 
      09-12-2007

"Steve Holden" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> bambam wrote:
>> import works in the main section of the module, but does
>> not work as I hoped when run inside a function.
>>
>> That is, the modules import correctly, but are not visible to
>> the enclosing (global) scope.
>>
>> Questions:
>> (1) Where can I read an explanation of this?
>> (2) Is there a work around?
>>
>> BTW, sys.modules("filename") shows that the module is
>> loaded, I just don't know how to use it when loaded that
>> way. Also, if I import again at the global scope, the module
>> name becomes available.
>>

> There's not much wrong with doing this, since it gives you the best of
> both worlds. But you mean sys.modules["filename"], don't you?
>
>>>>> def gim():

>> ... exec "import gamel"
>> ...
>>>>> gim()
>>>>> sys.modules["gamel"]

>> <module 'gamel' from 'c:\gamel.pyc'>
>>>>> gamel

>> NameError: name 'gamel' is not defined
>>>>> exec "import gamel"
>>>>> gamel

>> <module 'gamel' from 'c:\gamel.pyc'>

> Whoa there! There's a lot of difference between "importing a module inside
> a function" and "executing an import statement inside a function".
>
> If you want to do dynamic imports then the __import__ function is what you
> need. Trying to use exec like that is a bad idea unless you clearly
> understand the relationship between the different namespaces involved. In
> fact, trying to use exec at all is a bad idea until you understand Python
> better, and even then it's not often a terrific idea.
>
> Think of exec more as a hack of last resort than the first tool to reach
> for to solve a problem.
>
> regards
> Steve
> --
> Steve Holden +1 571 484 6266 +1 800 494 3119
> Holden Web LLC/Ltd http://www.holdenweb.com
> Skype: holdenweb http://del.icio.us/steve.holden
> --------------- Asciimercial ------------------
> Get on the web: Blog, lens and tag the Internet
> Many services currently offer free registration
> ----------- Thank You for Reading -------------
>


Yes, sys.modules["filename"], unfortunately, same mistake
made already 4 or 5 times before I typed this, and still hadn't
learned...many years working in an environment where the
distinction was not important. Sorry.

def gim(self):
for gamel in self.gamel_list:
__import__(gamel['file'])

Works as hoped for. I did a web search for 'dynamic import' and
the only examples I found used exec.

Thanks

Steve.


 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-12-2007
bambam wrote:
[...]
> def gim(self):
> for gamel in self.gamel_list:
> __import__(gamel['file'])
>
> Works as hoped for. I did a web search for 'dynamic import' and
> the only examples I found used exec.
>
> Thanks
>

Cool. You're getting there!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-12-2007
Am Wed, 12 Sep 2007 11:54:51 +1000 schrieb bambam:

> def gim():
> exec "global gamel"
> exec "import gamel"
>
> Unfortunately, does not have the desired effect.
> Steve.


Both statements have to be part of a single exec:

def gim():
modulename = "gamel" # determined at runtime
exec "global %s; import %s" % (modulename, modulename)

It may work, but it is still a bad idea to create global variables with a
name not known until runtime.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments