How can I open another python script with python?

Can I call another python script through python which I have in Blender?
I have several scripts for level layouts.
Can I read those while I’m in a different script?
What I mean is that I have the main script that creates the level based on a reference. I would have this reference in another script.
Can I combine those two?

Yes

simply import the script like:

import scriptname as script

now you imported a script and converted it’s name within the script to script(any name you like can be used, but don’t use python reserved words), now you can do:

script.some_function(cont)
#or
variable = script.function()

where function is the piece of script that handles or returns the result.

Or incase of normal script without functions:

import scriptname

you can just call the variables you need(make sure you do not use duplicated names in both scripts, they will overwrite each other)

Oh and the scriptname in texteditor needs to end with .py

1 Like

You can load a string from disk and use exec against it*

https://docs.python.org/2.0/ref/exec.html

1 Like

You’ll also need to either put the script in your blend file’s local folder or put it in your blender’s version site-packages folder to register the custom module, otherwise Blender won’t recognize it due to faulty file paths.

There is nothing faulty about it. It works exactly how it’s intended to work.

If you want to load scripts from other folders, then add the other folder paths to sys.path

1 Like

i believe you could simply do:

import /folder/scriptname

Never tested it but the path will grab .blend so if you have a folder at the blend location, just call the folder as well. If it’s loading from a total other path lets say from an other H.D.D then you need to mess with the path indeed.

1 Like

python uses dots import folder.module then folder.module.function() (im gonna guess thats a typo though)

or you can get fancy from package import module then module.function(). this might need to be setup as a package though

Ok, well as i said never tested it because i didn’t need it.

I simply use a python controller to load a script from a folder ‘scripts’ like ‘//scripts/scriptname.py’ and the scripts can just use the normal import option ‘import scriptname’. Thanks for clearing that up.

Thank you for your help.
I can import the script fine but I don’t know how to read the variables that are stored in that script.
I have used this to read from txt files:

with open(filePath, "r") as openedFile:
                
    data = literal_eval(openedFile.read())
                
    own["X_GRID"] = data["X_GRID"]
    own["Y_GRID"] = data["Y_GRID"]

This doesn’t seem to work.
The script itself looks like this for example:

{'X_GRID': 20, 'Y_GRID': 20, 'Stored_FINAL': {0: ['GL01-01', 'BLANK_2'], 1: ['GL01-01', 'BLANK_2']}}

Anyway, how can I read those variables?

I think you’re confusing “script” with a “file”. A script contains programming code. A file (can) contain data for reading/writing.

What is the actual problem you’re having? Is there an error? Is something not as you expect it?

Yes, there is an error. And you are right, I’ve used the script as a file and that’s the problem.
I wanted to have the data for levels stored in a separate script in Blender and just read variables from it.
I can make it so it would read the data from a text file but I wanted this data to be stored in Blender.
Is there a way how to do it?

Depends. What’s the error?

The error is:

TypeError: invalid file

This obviously doesn’t work as a file.

A quick 5min Google suggests you’re not supplying it a valid path.

Is there more to that error that you cut off?
What is “filePath” set to? Please print it with print(filePath) and put the result in here.

It’s this:

TypeError: invalid file: <module 'LEVEL_MAP_Test_001' from 'D:\\PROJEKTY\\Blender GAME PROJECT\\Fork Drifter PROTOTYPE\\ForkLift_001.blend\\LEVEL_MAP_Test_001.py'>

I guess it can’t read from a python script, unlike a txt document.

Your variable is not a String. A String path looks like this when printed:
“C:\my_game\my_file.txt”

Judging by the path in your error, it looks like you’re trying to open a script that’s already inside the blend file? If so, why not just use an actual variable instead? Like, put it in bge.logic or something…

The method you’re trying to use is for files that are NOT already in the blend.

If the variable is embedded in your blend file you can use the global statement to reference it in another internal script.

Example.

import bge

# Script 1
def Func_One(self):
  global var
  var = str("This is a global string test.")

# Script 2
def Func_Two(self):
  print(var)

Put Func_One() and Func_Two() lines in separate scripts.

Don’t make it hard for yourself, use the easy way out… or the way it’s meant to.

file_1.py:

#so file_1 got this dict ready to go
data = {'X_GRID': 20, 'Y_GRID': 20, 'Stored_FINAL': {0: ['GL01-01', 'BLANK_2'], 1: ['GL01-01', 'BLANK_2']}}

main blend:

#import the file_1.py to use the dict called data
import file_1

#use the data from the dict
own["X_GRID"] = data["X_GRID"]
own["Y_GRID"] = data["Y_GRID"]
1 Like

Thank you, this is what I was looking for.

1 Like

Ast.literal_eval can load a dict from a string also.

if 'data' not in own:
    data = (pickle or sjon)
    data = ast.literal_eval(data)
    own['data' ] = data
1 Like