| | |
eval() problems
Thread Solved |
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!
The code above makes sense, but python gives me this instead:
Python Syntax (Toggle Plain Text)
names = [ "main", "secondary", "tertiary" ] for item in names: eval( "import " + item )
Python Syntax (Toggle Plain Text)
import primary ^ SyntaxError: invalid syntax
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Try ...
python Syntax (Toggle Plain Text)
names = [ "main", "secondary", "tertiary" ] for item in names: cmd = "__import __(%s)" % item eval( cmd )
Last edited by vegaseat; Jun 16th, 2008 at 12:25 am.
May 'the Google' be with you!
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:
So when I call function "draw()" from module "primary", it gives me the above error. Any ideas?
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
- Hunter S. Thompson
my photography
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/
http://technogeek.org/2007/05/29/dyn...g-in-python-2/
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
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:
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)
for item in names: exec "import " + item
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
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- PhpBB vs VBulletin (Growing an Online Community)
- Using "variable variables" in Javascript (JavaScript / DHTML / AJAX)
- Dynamicallu Resizeing IFRAME based on content (JavaScript / DHTML / AJAX)
- Problems with my bus ticket program. (Python)
- problems writing multiple files (Perl)
- HijackThis Logfile-Probs after XP SP2 download (Viruses, Spyware and other Nasties)
- Help with Trojan.bookmarker.gen (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: Please !!
- Next Thread: new Python user has Export Problems
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






