Blender scripting cannot find my module

Hey guys, i was following a old blender scripting book using blender 2.78c.
everything works fine for me, and today i switched to blender 2.93. then my scripts won’t work anymore,
basically I have 2 python files (a.py, b.py) in my workspace folder, I imported module ‘a.py’ into ‘b.py’, then it crashed with “no module named a”
my a.py and b.py were in same folder, do i have to put all code into blender 2.93 root folder to make it work? what make me feel confused is i didn;t modified anything, folder structure are the same. the only change is i switched to 2.93///

here is code

                               SelectAndTransform.py

import ut
import importlib
importlib.reload(ut)

import bpy

#Will fail is scene is empty
bpy.ops.object.mode_set(mode=‘OBJECT’)
bpy.ops.object.select_all(action=‘SELECT’)
bpy.ops.object.delete()

bpy.ops.mesh.primitive_uv_sphere_add(size=0.5, location=(0,0,0))
bpy.ops.transform.resize(value=(5,5,5))
bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.mesh.select_all(action=‘DESELECT’)

                                      ut.py

import bpy
import bmesh
if name == “main”:
pass

I’m not sure why, but in blender you need to use a relative import:
from . import ut

Also, while a scripting book for 2.78 might be useful in some cases, the API has changed a lot since then, and you would probably be better finding something from more recently if you want to learn blender scripting…

This is a good series to get started with:

And this one’s good once you’ve found your feet a bit:

Hope that helps!

thanks, i searched a lot about the new video, but mostly were developed within blender, i prefer to use vs code. do you have any resource for that?

Most of the same concepts apply when using VS Code. The scripting for artists series starts in blender, but then moves to VS Code layer on.
There is also a very good VS Code extension called Blender development by Jacques Lucke, that lets you auto reload your scripts.
Either way, there are some things that are easier in blender’s text editor, so it’s not a bad idea to start off using it.

1 Like

Thanks, i’ll take a look at it

1 Like
1 Like