Search code examples
pythonpython-3.6webots

Trying to import python modules returns ModuleNotFound Error


Im attempting to use the numpy module in webots but whatever I try I always get

INFO: assignment_2_controller_2: Starting controller: python3.6 -u "assignment_2_controller_2.py" 
[assignment_2_controller_2] Traceback (most recent call last):
[assignment_2_controller_2]   File "assignment_2_controller_2.py", line 4, in <module>
[assignment_2_controller_2]     import numpy
[assignment_2_controller_2] ModuleNotFoundError: No module named 'numpy'
WARNING: 'assignment_2_controller_2' controller exited with status: 1.

I have installed numpy with both pip2 and pip3 and neither seem to have made any difference and the package shows up in ~/.local/lib/python3.6/site-packages. I can also confirm that this module works in the python editor and in other python files. Is there perhaps something I need to change in webots python Makefile or perhaps in my environment variables?

Edit: Printing sys.path shows

'/snap/webots/14/usr/share/webots/lib/controller/python36', '/snap/webots/14/usr/lib/python36.zip', '/snap/webots/14/usr/lib/python3.6', '/snap/webots/14/usr/lib/python3.6/lib-dynload', '/snap/webots/14/usr/lib/python3/dist-packages'

I installed webots using snap but why wouldnt it use the python dictated by the PATH environment variable?

Fix: By the looks of it snap creates a contained filesystem for webots to run in by design in which it installed its own python binaries. I therefore uninstalled webots with snap and then installed from the .deb file off their website and now it works fine.


Solution

  • Webots installed from the Snap package is running in a kind of sandbox, it is therefore using the version of Python distributed with the snap package (which you can't alter).

    As explained in the documentation, you might use external controllers to launch yourself the controller instead of letting Webots starting it. This way you can chose which version of Python you want to use: https://cyberbotics.com/doc/guide/installation-procedure#installing-the-snap-package

    Alternatively, you can use a different way to install Webots (from the Debian or archive package). The other installations are not coming with Python and are using the system Python.