Blender3D:Python 脚本问题“ImportError:没有名为 MyModule 的模块”
您好,我正在尝试在 Blender3D 脚本中使用多个 python 文件。 (例如 MyScript.py 和 MyModule.py)。 MyScript.py 看起来像:
import bpy
import math
from add_utils import AddObjectHelper, add_object_data
import mathutils
# this line doesn't work
import MyModule
#### REGISTER ####
def register():
pass
def unregister():
print("Finished")
if __name__ == '__main__':
register()
在 Blender3D 中运行脚本,我收到错误:
"ImportError: No module named MyModule"
我已将 Blender3D 的“脚本”文件夹设置为指向包含我的脚本和 MyModule.py 的文件夹。
感谢您的任何帮助。
J
Hi I'm trying to use multiple python files in my Blender3D script. (eg. MyScript.py and MyModule.py). MyScript.py looks like:
import bpy
import math
from add_utils import AddObjectHelper, add_object_data
import mathutils
# this line doesn't work
import MyModule
#### REGISTER ####
def register():
pass
def unregister():
print("Finished")
if __name__ == '__main__':
register()
Running the script within Blender3D, I get the error:
"ImportError: No module named MyModule"
I have setup Blender3D's "script" folder to point at the folder containing my script and MyModule.py.
Thanks for any help.
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
必须向 sys.path 变量添加正确的路径。我不知道 Blender 的具体情况,但一般来说,您可以按如下方式添加它:
要使用的脚本取决于您运行的条件:如果您的脚本位于某些(对于 Blender)专用脚本文件夹中,您可以使用第一个。如果您从(例如)
Program Files
文件夹运行它,请使用绝对路径。Something has to add the proper path to the
sys.path
variable. I don't know it for Blender specifically, but in general, you could add it as follows:The one to use depends on the conditions you are running under: if your script is in some (for Blender) dedicated scripts folder, you could use the first one. If you run it from (for example) a
Program Files
folder, use the absolute path.另一件对我有用的事情是使用以下终端命令运行我的 python 脚本
Another thing that worked for me was running my python script with the following terminal command
当我的外部模块与 .blend 文件位于同一目录中时,我使用的另一个陈词滥调是:
我从 http://www.blender.org/documentation/blender_python_api_2_59_2/info_tips_and_tricks.html 实际上有一个错误,当您使用
os.path.basename
确实需要os.path.dirname
。Another cliche I use when I have external modules that live in the same directory with the .blend file is:
I derived it from http://www.blender.org/documentation/blender_python_api_2_59_2/info_tips_and_tricks.html which actually has a bug in that it uses
os.path.basename
when you really needos.path.dirname
.