Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
import from a string
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
iu2  
View profile  
 More options Nov 3, 11:45 pm
Newsgroups: comp.lang.python
From: iu2 <isra...@elbit.co.il>
Date: Tue, 3 Nov 2009 04:45:49 -0800 (PST)
Local: Tues, Nov 3 2009 11:45 pm
Subject: import from a string
Hi,

Having a file called funcs.py, I would like to read it into a string,
and then import from that string.
That is instead of importing from the fie system, I wonder if it's
possible to eval the text in the string and treat it as a module.

For example

with file('funcs.py') as f: txt = r.read()
string_import(txt, 'funcs')  # is string_import possible?

to have now a module called funcs with the functions defined in
funcs.py.

Thanks


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt McCredie  
View profile  
 More options Nov 4, 4:49 am
Newsgroups: comp.lang.python
From: Matt McCredie <mccre...@gmail.com>
Date: Tue, 3 Nov 2009 17:49:43 +0000 (UTC)
Local: Wed, Nov 4 2009 4:49 am
Subject: Re: import from a string
iu2 <israelu <at> elbit.co.il> writes:

> Hi,

> Having a file called funcs.py, I would like to read it into a string,
> and then import from that string.
> That is instead of importing from the fie system, I wonder if it's
> possible to eval the text in the string and treat it as a module.

> For example

> with file('funcs.py') as f: txt = r.read()
> string_import(txt, 'funcs')  # is string_import possible?

> to have now a module called funcs with the functions defined in
> funcs.py.

You can do something like this:

import types
import sys

mymodule = types.ModuleType("mymodule", "Optional Doc-String")

with file('funcs.py') as f:
    txt = f.read()
exec txt in globals(), mymodule.__dict__
sys.modules['mymodule'] = mymodule

Note that you shouldn't exec untrusted code.
You might also look at the __import__ funciton, which can import by python path.
You might also look at the imp module.

Matt


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
iu2  
View profile  
 More options Nov 4, 7:36 am
Newsgroups: comp.lang.python
From: iu2 <isra...@elbit.co.il>
Date: Tue, 3 Nov 2009 12:36:08 -0800 (PST)
Local: Wed, Nov 4 2009 7:36 am
Subject: Re: import from a string
On Nov 3, 7:49 pm, Matt McCredie <mccre...@gmail.com> wrote:

Thanks, it seems simpler than I thought.
I don't fully understand , though, the exec statement, how it causes
the string execute in the context of mymodule.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Nov 4, 12:10 pm
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Tue, 03 Nov 2009 22:10:51 -0300
Local: Wed, Nov 4 2009 12:10 pm
Subject: Re: import from a string
En Tue, 03 Nov 2009 17:36:08 -0300, iu2 <isra...@elbit.co.il> escribió:

Sometimes you don't even require a module, and this is simpler to  
understand. Suppose you have a string like this:

txt = """
def foo(x):
   print 'x=', x

def bar(x):
   return x + x
"""

you may execute it:

py> namespace = {}
py> exec txt in namespace

The resulting namespace contains the foo and bar functions, and you may  
call them:

py> namespace.keys()
['__builtins__', 'foo', 'bar']
py> namespace['foo']('hello')
x= hello

exec just executes the string using the given globals dictionary as its  
global namespace. Whatever is present in the dictionary is visible in the  
executed code as global variables (none in this example). The global names  
that the code creates become entries in the dictionary. (foo and bar;  
__builtins__ is an implementation detail of CPython). You may supply  
separate globals and locals dictionaries.

--
Gabriel Genellina


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
iu2  
View profile  
 More options Nov 4, 4:45 pm
Newsgroups: comp.lang.python
From: iu2 <isra...@elbit.co.il>
Date: Tue, 3 Nov 2009 21:45:23 -0800 (PST)
Local: Wed, Nov 4 2009 4:45 pm
Subject: Re: import from a string
On Nov 4, 3:10 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:

Thanks for the explanation.
What happens if both global and local dictionaries are supplied: where
are the newly created entities created? In the local dict?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Nov 4, 5:02 pm
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Wed, 04 Nov 2009 03:02:34 -0300
Local: Wed, Nov 4 2009 5:02 pm
Subject: Re: import from a string
En Wed, 04 Nov 2009 02:45:23 -0300, iu2 <isra...@elbit.co.il> escribió:

The amazing thing about Python is how easy is to experiment in the  
interpreter.
Just see it by yourself!

--
Gabriel Genellina


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Nov 5, 5:51 am
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Wed, 04 Nov 2009 13:51:02 -0500
Local: Thurs, Nov 5 2009 5:51 am
Subject: Re: import from a string

Hint: they are created in the same namespace they always are (ignoring
nested functions and nonlocal namespaces). But I agree with Gabriel:
just try it. n1,n2={},{}; exec....

Terry Jan Reedy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
iu2  
View profile  
 More options Nov 5, 4:53 pm
Newsgroups: comp.lang.python
From: iu2 <isra...@elbit.co.il>
Date: Wed, 4 Nov 2009 21:53:17 -0800 (PST)
Local: Thurs, Nov 5 2009 4:53 pm
Subject: Re: import from a string
On Nov 4, 8:51 pm, Terry Reedy <tjre...@udel.edu> wrote:

n2 :-)

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google