Python 3.1 - 在Blender中添加库时出错

1 投票
1 回答
1460 浏览
提问于 2025-04-16 06:35

对于这个问题(stackoverflow.com/questions/4086435/),我尝试制作一个适用于Python 3的python-websocket库(github.com/mtah/python-websocket/),这是我的代码:https://gist.github.com/663175

Blender自带了一个Python 3.1的包,所以我直接把我的文件放在它的«site-packages»文件夹里。现在我遇到了这个错误:

Traceback (most recent call last):
  File "websocket.py", line 6, in 
AttributeError: 'module' object has no attribute 'WebSocket'

在Blender中运行这段代码时:


import sys, os, asyncore, websocket

def msg_handler(msg):
print(msg)

socket = websocket.WebSocket('ws://localhost:8080/', onmessage=msg_handler)
socket.onopen = lambda: socket.send('Hello world!')

try:
asyncore.loop()
except KeyboardInterrupt:
socket.close()

我发现需要一个__init__.py文件,所以我加上了,但这并没有解决问题……我哪里做错了呢?谢谢你的帮助。

1 个回答

3

看起来你把你的脚本命名为 websocket.py,所以当你尝试导入 websocket 时,它找到了你自己写的这个脚本,而不是安装好的同名模块。你可以把这个脚本改个名字(如果它生成了 websocket.pyc 文件,也要把那个删掉)。

撰写回答