943,978 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3555
  • Python RSS
Jun 15th, 2008
0

eval() problems

Expand Post »
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!
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  1. import primary
  2. ^
  3. SyntaxError: invalid syntax
Similar Threads
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 16th, 2008
0

Re: eval() problems

Try ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 16th, 2008
0

Re: eval() problems

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?
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 16th, 2008
0

Re: eval() problems

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/
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
tzushky is offline Offline
96 posts
since Jun 2008
Jun 16th, 2008
1

Re: eval() problems

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:
python Syntax (Toggle Plain Text)
  1. for item in names:
  2. exec "import " + item
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Jun 16th, 2008
0

Re: eval() problems

Oh awesome - I got it working using exec . I never knew that it existed... Thanks for solving my problem!
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Please !!
Next Thread in Python Forum Timeline: new Python user has Export Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC