Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411581 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 08:47:13 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Python calling more Python scripts
Pages: [1]
Print
Author Topic: Python calling more Python scripts  (Read 952 times)
droqen
Level 10
*****


View Profile WWW
« on: July 08, 2009, 09:47:48 AM »

 Cave Story Awesome.

Uh, right. I had a question.
I'm writing a program in Python and am trying to make some kind of thing that allows for scripted objects. However, as I have little to no knowledge of how to write an interpreter beyond the most basic, I decided it would be easiest if I went ahead and made my scripted objects written in Python, as .py files.

However I'm having difficulty finding a way to pop one open and run it. Note I'd much prefer to do this on-the-fly, only for objects I need, so I'm not much for importing.

It would also help if you could tell me how to open files in subfolders; I can only kind of get it working and not in a very pretty way ._.


Thank you for reading, I'd appreciate even a modicum of help although I'm still going to be slowly working this out on my own Smiley
« Last Edit: July 08, 2009, 10:41:06 AM by Droqen » Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: July 08, 2009, 11:06:59 AM »

Check the eval function, for running a string as python line, and import statement for running a module and making it's symbols available. IIRC, using __import__ function instead of import statement let's you specify the exact location, or you can just add the subdirectory to sys.path.

The open function works fine on subdirectories, so I cannot see what that's about.
Logged
droqen
Level 10
*****


View Profile WWW
« Reply #2 on: July 08, 2009, 11:28:36 AM »

eval: my goodness, I'll have to take a deeper look into it though currently it's just handing me errors.

Also, thank you. For some reason I could not fathom that the open function would allow me to add subdirectories before the filename Tongue I'm supposed to use os.path or whatever to keep it multiplatformfriendly, right?
Logged

nihilocrat
Level 10
*****


Full of stars.


View Profile WWW
« Reply #3 on: July 08, 2009, 04:55:42 PM »

Yeah.

Also, if you are using a program like py2exe to pack up your program, but you still want there to be user-editable scripts that are loaded at runtime, there is a neat trick you can do:

Code:
import imp, os
imp.load_source('settings', os.path.join('.','settings.cfg'))
import settings

It will load settings.cfg at runtime (notice how it doesn't even have to be a .py file). I bet you can tell I use this to load configuration / settings files, rather than make up my own config language, use XML, or whatever else.
Logged

droqen
Level 10
*****


View Profile WWW
« Reply #4 on: July 09, 2009, 06:04:24 AM »

Ooooh.
That's interesting. Thanks for the trick, I'll give it a try sometime!

little edit : am continuing to work on stuff, don't hesitate to toss me more ideas if you have any for user-editable scripts... or just ways for ME to have an easier time since I'm lazy :D
« Last Edit: July 09, 2009, 06:07:31 AM by Droqen » Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic