This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
(Yikes, I ask a lot of questions!)
Let's pretend I was writing a code that used the scene module. This code had a list of pictures that were only used 1/10 of the time. Instead of having all of that code "cluttering up" my main code I would want it in a seperate file that could be called upon when needed. I've done stuff like this before ("execfile()"), but that was without the scene module. Would it be that the second file should be an entirely new class? Or would it be that it would be another MyScene class? Or am I completely missing the ball here and it is something very different? or is it simply not possible (if so, why)?
eliskan175 last edited by
This is very easy to implement and I will walk you through the steps.
Step 1) Make a new file with some functions you want to use in your script, but that you want out of your way. For my example, I will make a very simple example, so my file will only have 1 function and 1 value:
<pre>#A variable we can call from our other script.
test_var = 100
#Print whatever the input is
print "Input is: "+str(input)
Now that this file is created, save it as "print_functions" or another appropriate name that is easy for you to remember. You will need to know this name for importing.
Next, create a new Python file. This file HAS to be in the same directory as the other one, or you won't be able to import the other file. This new Python file can be your active MyScene script, or anything you want really. At the top of the new file, put this line:
<pre>from print_functions import *
What this says is to import everything in print_functions. So you can now call the print_input function or find the value of test_var from your other script, after you have imported it:
If you wanted to keep your print_functions OBVIOUS they're in the print_functions section of your code, you could import your module differently:
<pre>import print_functions as pf
If you only need a specific function for your current script and you don't need all the functions available, it's a good idea to implicitly state which functions/values you need. This way, your program doesn't have to compile a lot of useless 'garbage' before it runs:
<pre>from print_functions import print_input, test_var
Personally I suggest the first method. It imports everything. You only need to import a script once, then the functions will be useable throughout the rest of the script - regardless of what your script is doing, it will work.
Didn't know that import worked with scripts too! This has many uses...
Thanks Eliskan! Helped a lot. Now have to twiddle with another idea, possibly fail, and then (if the previous step proves true) post question #11. :D
P.S. you did a good job explaining the method!
eliskan175 last edited by
Nothing wrong with asking questions. The only stupid question is the one not asked.
One more simple import method that I never really used, but might be useful:
<pre>from print_functions import print_input as p
Good way to shorten the code if you use a function a thousand times. You can shorten the functions name or give it a name you better understand. "p" could be anything you want it to be.
Oh goodie, I don't like asking stupid questions :P
And that is interesting. Can you do the following then?
<pre>from scene import *
from scene import image as i
from time import time, localtime as ti, loc
from other_module import functiona, functionb as ,b</pre>
The first two are simple, it basically redefines image. The third line does it in bulk while line imports two commands, but only shortens one of them. Which of these (besides line one...) work? I just realized I can check myself... So I will. In a moment. I'll post what I find when I have Internet again (couple hours, out of house).
PyLot213 last edited by
Also FYI: If you want to structure your stuff even more, then you can also have your file in a different folders (since the import follows the python import rules). The only thing you have to do is create an (even empty) init file in the directory and then you can import it like
(you can read more about it here: http://docs.python.org/2/tutorial/modules.html#packages )
Wow, thanks! that really helps keep things a bit more organized :)
I owe ya' one.