eval() problems

Thread Solved

Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

eval() problems

 
0
  #1
Jun 15th, 2008
Hi, I was wondering how to get around a problem I was having with eval(). I have a list of strings and each one is the name of a python file found in the same directory as the current script. I was it to import these modules to use in the current script, but: eval( "import" + modname ), where 'modname' is the script's name, causes an error. Is there any way to have the script import any found scripts or would I have to explicitly add "import <name>" for each script? Thanks!
  1. names = [ "main", "secondary", "tertiary" ]
  2. for item in names:
  3. eval( "import " + item )
The code above makes sense, but python gives me this instead:
  1. import primary
  2. ^
  3. SyntaxError: invalid syntax
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: eval() problems

 
0
  #2
Jun 16th, 2008
Try ...
  1. names = [ "main", "secondary", "tertiary" ]
  2. for item in names:
  3. cmd = "__import __(%s)" % item
  4. eval( cmd )
Last edited by vegaseat; Jun 16th, 2008 at 12:25 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: eval() problems

 
0
  #3
Jun 16th, 2008
I no longer got an error about the import function, but now, however, when I call a function from the "primary" module, it gives me an error saying:
NameError: name 'primary' is not defined.
So when I call function "draw()" from module "primary", it gives me the above error. Any ideas?
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 63
Reputation: tzushky is an unknown quantity at this point 
Solved Threads: 4
tzushky's Avatar
tzushky tzushky is offline Offline
Junior Poster in Training

Re: eval() problems

 
0
  #4
Jun 16th, 2008
Try and see this... I've been trying similar things after I read it and I had some trouble solving my bugs too... I needed the name of a class in a module in a package to be imported ...I still didn't get there but maybe your case is easier

http://technogeek.org/2007/05/29/dyn...g-in-python-2/
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: eval() problems

 
1
  #5
Jun 16th, 2008
Besides, that you do not have a "primary" module in the code...
Eval evaluates an expression in his own namespace and returns the result.
What you really want imho is to inject code.

Something like this:
  1. for item in names:
  2. exec "import " + item
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: eval() problems

 
0
  #6
Jun 16th, 2008
Oh awesome - I got it working using exec . I never knew that it existed... Thanks for solving my problem!
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC