touilleMan / godot-python

Python support for Godot 🐍🐍🐍
Other
1.86k stars 140 forks source link

Trouble importing a module: import does not throw an error, but "attribute not defined" when trying to use #128

Closed whogben closed 5 years ago

whogben commented 5 years ago

First, thank you so much for making Godot Python, this is really exciting and powerful!

I don't know much about python, but I hope to use it as a bridge to let me control robots w/ Godot. For this am using godot-python to interface with modules that control the servos and interface with the sensors. I have been successful with this simple motor controller but now I am stuck trying to incorporate this more complex vision module.

I will first describe what worked with the motor controller and then where I'm stuck with the vision module.

motor controller (working) With the motor controller, I downloaded the Maestro.py file and included it in the project. I created a new Node script in python and with "import Maestro" I was able to access and use the code in the Maestro.py file. The next problem was the lack of a serial library, which I downloaded and built for my OS with brew/macports. After locating the resulting library folder ("serial") I copied it from my system python to the game python, putting the entire folder into game/pythonscript/osx-64-cpython/lib/python3.6/site-packages/. This all works - I am able to use game logic to move the robot successfully.

vision sensor (not working) With the Pixy vision sensor I attempted the same process: I downloaded the pixy code and built the library's python examples following these instructions. The build process was/is wonky, but the result is working with the sensor: I can use python3 on the command line to run the built examples which confirms that my system python is successfully interacting with the sensor.

I then copied the entire resulting folder into the game directory, examples and all, and confirmed that the same examples run on the terminal in the new directory.

Next I tried to import and use the library from a new Node script, and while the "import pixy" does not cause an error or warning, nothing is accessible, for example:

pixy.init() -> AttributeError: module 'pixy' has no attribute 'init'

The overall start of the console log reads: Pythonscript version: 0.11.1 Pythonscript backend: cpython 3.6.3 (v3.6.3:2c5fed8, May 1 2018, 11:03:45) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] PYTHONPATH: ['/Users/williamhogben/Dropbox/Godot3/BeeBoop/pythonscript/osx-64-cpython/lib/python36.zip', '/Users/williamhogben/Dropbox/Godot3/BeeBoop/pythonscript/osx-64-cpython/lib/python3.6', '/Users/williamhogben/Dropbox/Godot3/BeeBoop/pythonscript/osx-64-cpython/lib/python3.6/lib-dynload', '/Users/williamhogben/Dropbox/Godot3/BeeBoop/pythonscript/osx-64-cpython/lib/python3.6/site-packages', '/Users/williamhogben/Dropbox/Godot3/BeeBoop/', '/Users/williamhogben/Dropbox/Godot3/BeeBoop/lib'] From cffi callback <function pybind_instance_call_method at 0x11a0440d0>: Traceback (most recent call last): File "/Users/williamhogben/Dropbox/Godot3/BeeBoop/pythonscript/osx-64-cpython/lib/python3.6/site-packages/godot/hazmat/ffi/instance.py", line 83, in pybind_instance_call_method pyret = meth(*pyargs) File "/Users/williamhogben/Dropbox/Godot3/BeeBoop/PixyController.py", line 22, in _ready pixy.init() AttributeError: module 'pixy' has no attribute 'init'

I'm in over my head trying to figure this out - python's brand new to me and I hope that someone with more experience will know what direction to look. Thank you for looking at this!

touilleMan commented 5 years ago

Hi @whogben, You should try to do dir(pixy) once the import is done This should show you the attributes of your imported pixy module, typically you should have a __file__ and/or a __package__ attribute that should give you the folder from where you module comes from (maybe the folder is wrong and doesn't contain the full library...)

whogben commented 5 years ago

Thank you @touilleMan I appreciate it - that helped me narrow it down to trouble with the module itself - I am closing this issue.