calling external Python scripts from internal script

Hello all,

In the German Forum was the question how to call external Python scripts. Simple said, you can’t. You can only exec modules. This means you need to write a module and import it.
Module files have the extention .py (scripts can have any name).

Have a Python module with functions and data stored within your blender directory.
e.g.

example.py


info = { 
   0: ["data1", "blahbla"], 
   1: ["data2", "more"],       
   999: ["the", "end"]    
} 
def abcd(x):
    return x+x

now call it from an internal script or module as follows:


from example import info, abcd
 
try: 
     print info[1][0] 
except: 
     print "Error - Missing Info"
 
print abcd( 10 )  

The imported module can be internal as well. If internal and external er present, the external will be used.

I hope you find this useful.
Monster

How very cool!!! thank you for this :slight_smile:

do you know how to make a command line call from a blender script?

for one of my projects I want a.m3u or a .PLS file to be executed, so that the users music player will tune into a internet radio stream.

Try this:


import os
os.system('yourcommand')
# Don't forget the quotes. (Double quotes will work too)

p00f, what Social said will work, but remember that on Windows systems that don’t have a full Python install, you’ll need to ship the os.py file with your .blend (I know you’re both on Linux, so you generally don’t have to worry about these things). If you want, you can just throw the Python24.zip folder and zlib.dll (both in the main Blender directory) in the same directory as your .blend or runtime and you should be set.

I have run into problems with this in the past, though, and I haven’t been able to figure it out. Some Windows computers will recognize the os.py file when you included it (zipped or separate), and others will not. I haven’t been able to do enough testing to trace it down yet.

I’m pretty sure that when you open a .blend file with Blender, your python scripts will look for things in the directory where Blender itself is installed, as opposed to the directory where the .blend actually resides. (or so I think)

Other than that I have no idea, but it does sound like a path problem of some sort.

PS: Hey blendenzo, are you crossing over to *nix any time soon?

@social
quote:
import os
os.system(‘yourcommand’)
unquote:

dude, thanks :slight_smile:

ok im gonna try this
thankyou.

@blendenzo

ok will do, thank you :slight_smile: