IuAyala / Self-Driving-Cars-Course

This repository stores code for my Self-Driving Cars Course!
MIT License
6 stars 8 forks source link

ImportError: cannot import name 'Robot' from 'controller' #2

Closed CountryCousin closed 1 year ago

CountryCousin commented 1 year ago

I followed the installation process for linux, had webots installed but this imports don't work on vscode, is there something I have not done ??


Cell In[2], line 2
      1 from numpy.lib.function_base import average
----> 2 from controller import Robot
      3 from controller import Camera
      4 from controller import Display 

ImportError: cannot import name 'Robot' from 'controller' (/home/page/anaconda3/envs/sds_python/lib/python3.10/site-packages/controller/__init__.py)```
IuAyala commented 1 year ago

Hey @vikkydataseo,

The issue you're experiencing is likely related to the PYTHONPATH environment variable not being set correctly. The "controller" module is a part of the Webots package and the import error is indicating that Python is unable to find it.

You should double check that you have added the PYTHONPATH variable to your .bashrc file, as per the instructions in the repository's installation guide. The line should look like this:

echo "export PYTHONPATH=$PYTHONPATH:/usr/local/webots/lib/python3.10" >> ~/.bashrc This adds a line to the end of your "~/.bashrc" file.

First of all you need to find where is your controller folder is, you can find it with the following command locate robot.py | grep -i webots If you get an error saying that you don't have "locate", you need to install the package "mlocate" (which is very useful), you can do it with the following command (if in Ubuntu/Debian). sudo apt install mlocate

Just for reference, I get the following answer: /usr/local/webots/lib/controller/python/controller/robot.py Therefore the command I had to add to my "~/.bashrc" file was: export PYTHONPATH=$PYTHONPATH:/usr/local/webots/lib/controller/python/controller

I hope this helps, let me know if there is anything else that I can help you with!

CountryCousin commented 1 year ago

Hey @IuAyala

after I ran locate robot.py | grep -i webots, I got /usr/local/webots/lib/controller/python/controller/robot.py

Then I preceded with adding the following commands to my vscode terminal :

$ echo "export LD_LIBRARY_PATH=/usr/local/webots/lib/controller" >> ~/.bashrc $ echo "export PYTHONPATH=$PYTHONPATH:/usr/local/webots/lib/controller/python/controller" >> ~/.bashrc $ echo "export PYTHONIOENCODING=UTF-8" >> ~/.bashrc

The above commands did not fix the problem.

There is one more thing I'd like to tell you: I wanted to see if changing the file name extension might work...

so I saved one file as utils.py got the error below : Traceback (most recent call last): File "/home/page/anaconda3/envs/sds_python/Self Driving Cars/2. computer vision/2. camera pid.py", line 3, in <module> from controller import Display ModuleNotFoundError: No module named 'controller'

while the file I saved as utils.ipynb threw:


      1 from numpy.lib.function_base import average
----> 2 from controller import Robot
      3 from controller import Camera
      4 from controller import Display 

ImportError: cannot import name 'Robot' from 'controller' (/home/page/anaconda3/envs/sds_python/lib/python3.10/site-packages/controller/__init__.py) ```
IuAyala commented 1 year ago

It sounds like the problem may be related to the environment in which the code is being run. You have set the PYTHONPATH and LD_LIBRARY_PATH variables correctly, but the imports are still not working. One thing to check is that you have the correct version of Python installed, and that the code is being run with that version. Also, check that the Webots package is installed in the correct location and that the paths you added to the environment variables are correct.

Another thing to check is the permissions of the folder where the Webots package is installed. Make sure that you have read and execute permissions for that folder and the files in it.

Finally, it's worth checking if there is any other configuration that is being loaded before the environment variables are being set, this could be unsetting the variables that you just set.

Regarding the change of name, either the ".py" or ".ipynb" it should be the same. Just be careful that to load changes in another file from a notebook you need to refresh the kernel.

Let me know if you can't find the solution after addressing all these points, in that case we could consider a videocall.

Good luck!

CountryCousin commented 1 year ago

Hello @IuAyala,

I have tried changing to different paths of pythons available on my machine but without any success, I will take you up on the video-call offer, or perhaps a discord chat...

Thank you.

IuAyala commented 1 year ago

Hey @vikkydataseo,

I've been investigating more about the issue, and I've realised that the new version of Webots changes where the Python files are stored. I already updated the documentation of the repository. The mistake that I was doing was to tell you to add second "controller" in the PYTHONPATH.

But in short you can run the following commands and that's it (preferably remove the previous installation, by going to your ~/.bashrc and removing the lines that modify the PYTHONPATH, LD_ ...

echo "export WEBOTS_HOME=/usr/local/webots" >> ~/.bashrc
echo "WEBOTS_PYTHON=${WEBOTS_HOME}/lib/controller/python" >> ~/.bashrc
echo "export PYTHONPATH=${PYTHONPATH:+${PYTHONPATH}:}${WEBOTS_PYTHON}" >> ~/.bashrc
echo "export PYTHONIOENCODING=UTF-8" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${WEBOTS_HOME}/lib/controller" >> ~/.bashrc

I hope it helps!

CountryCousin commented 1 year ago

Hey @IuAyala,

Thanks so much for the efforts,

After I ran the above lines of commands, I still encounter the error below:

  File "/home/page/Desktop/Machine Learning/1/Self Driving Cars/2. computer vision/utils.py", line 2, in <module>
    from controller import Robot
ImportError: cannot import name 'Robot' from 'controller' (/home/page/anaconda/lib/python3.9/site-packages/controller/__init__.py)
IuAyala commented 1 year ago

Let's have a look at your environment variables that way we will see if that's solved. I need you to run the following commands and let me know the output:

echo PYTHONPATH
echo $PYTHONPATH
echo LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH
path_array=($(echo $PYTHONPATH | tr ":" " "))
last_element="${path_array[-1]}"
ls ${last_element}/controller/robot.py -l

Aside from that, the code is saying that it's finding the a module named controller, run the following command with conda environment that you are using. pip list or conda list

I feel that we are getting close to resolving the issue :)

CountryCousin commented 1 year ago

Hey @IuAyala , here they come, Thanks for the efforts

(base) page Jan 24 14:18 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo PYTHONPATH
PYTHONPATH
(base) page Jan 24 14:18 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo $PYTHONPATH
:/usr/local/webots/lib/python3.10:
(base) page Jan 24 14:19 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo LD_LIBRARY_PATH
LD_LIBRARY_PATH
(base) page Jan 24 14:19 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo $LD_LIBRARY_PATH
/usr/local/webots/lib/controller:/lib/controller
(base) page Jan 24 14:20 ~/.../Machine Learning/1/Self Driving Cars [main !] > path_array=($(echo $PYTHONPATH | tr ":" " "))
(base) page Jan 24 14:20 ~/.../Machine Learning/1/Self Driving Cars [main !] > last_element="${path_array[-1]}"
(base) page Jan 24 14:21 ~/.../Machine Learning/1/Self Driving Cars [main !] > ls ${last_element}/controller/robot.py -l
ls: cannot access '/usr/local/webots/lib/python3.10/controller/robot.py': No such file or directory
(base) page Jan 24 14:21 ~/.../Machine Learning/1/Self Driving Cars [main !] > pip list
Package                            Version
---------------------------------- --------------------
alabaster                          0.7.12
anaconda-client                    1.9.0
anaconda-navigator                 2.1.1
anaconda-project                   0.10.1
ansible                            7.1.0
ansible-core                       2.14.1
anyio                              2.2.0
appdirs                            1.4.4
argh                               0.26.2
argon2-cffi                        20.1.0
arrow                              0.13.1
asn1crypto                         1.4.0
astroid                            2.6.6
astropy                            4.3.1
async-generator                    1.10
atomicwrites                       1.4.0
attrs                              21.2.0
autopep8                           1.5.7
Babel                              2.9.1
backcall                           0.2.0
backports.functools-lru-cache      1.6.4
backports.shutil-get-terminal-size 1.0.0
backports.tempfile                 1.0
backports.weakref                  1.0.post1
beautifulsoup4                     4.10.0
bidict                             0.22.1
binaryornot                        0.4.4
bitarray                           2.3.0
bkcharts                           0.2
black                              19.10b0
bleach                             4.0.0
bokeh                              2.4.1
boto                               2.49.0
Bottleneck                         1.3.2
brotlipy                           0.7.0
cached-property                    1.5.2
certifi                            2021.10.8
cffi                               1.14.6
chardet                            4.0.0
charset-normalizer                 2.0.4
click                              8.0.3
cloudpickle                        2.0.0
clyent                             1.2.2
colorama                           0.4.4
conda                              4.10.3
conda-build                        3.21.5
conda-content-trust                0+unknown
conda-pack                         0.6.0
conda-package-handling             1.7.3
conda-repo-cli                     1.0.4
conda-token                        0.3.0
conda-verify                       3.4.2
contextlib2                        0.6.0.post1
controller                         0.1.0
cookiecutter                       1.7.2
cryptography                       3.4.8
cycler                             0.10.0
Cython                             0.29.24
cytoolz                            0.11.0
daal4py                            2021.3.0
dask                               2021.10.0
debugpy                            1.4.1
decorator                          5.1.0
defusedxml                         0.7.1
diff-match-patch                   20200713
distributed                        2021.10.0
docutils                           0.17.1
entrypoints                        0.3
et-xmlfile                         1.1.0
fastcache                          1.1.0
filelock                           3.3.1
flake8                             3.9.2
Flask                              1.1.2
fonttools                          4.25.0
fsspec                             2021.8.1
future                             0.18.2
gevent                             21.8.0
glob2                              0.7
gmpy2                              2.0.8
greenlet                           1.1.1
h5py                               3.3.0
HeapDict                           1.0.1
html5lib                           1.1
idna                               3.2
imagecodecs                        2021.8.26
imageio                            2.9.0
imagesize                          1.2.0
importlib-metadata                 4.8.1
inflection                         0.5.1
iniconfig                          1.1.1
intervaltree                       3.1.0
ipykernel                          6.4.1
ipython                            7.29.0
ipython-genutils                   0.2.0
ipywidgets                         7.6.5
isort                              5.9.3
itsdangerous                       2.0.1
jdcal                              1.4.1
jedi                               0.18.0
jeepney                            0.7.1
Jinja2                             3.1.2
jinja2-time                        0.2.0
joblib                             1.1.0
json5                              0.9.6
jsonschema                         3.2.0
jupyter                            1.0.0
jupyter-client                     6.1.12
jupyter-console                    6.4.0
jupyter-core                       4.8.1
jupyter-server                     1.4.1
jupyterlab                         3.2.1
jupyterlab-pygments                0.1.2
jupyterlab-server                  2.8.2
jupyterlab-widgets                 1.0.0
keyring                            23.1.0
kiwisolver                         1.3.1
lazy-object-proxy                  1.6.0
libarchive-c                       2.9
llvmlite                           0.37.0
locket                             0.2.1
lxml                               4.6.3
MarkupSafe                         2.1.1
matplotlib                         3.4.3
matplotlib-inline                  0.1.2
mccabe                             0.6.1
mistune                            0.8.4
mkl-fft                            1.3.1
mkl-random                         1.2.2
mkl-service                        2.4.0
mock                               4.0.3
more-itertools                     8.10.0
mpmath                             1.2.1
msgpack                            1.0.2
multipledispatch                   0.6.0
munkres                            1.1.4
mypy-extensions                    0.4.3
navigator-updater                  0.2.1
nbclassic                          0.2.6
nbclient                           0.5.3
nbconvert                          6.1.0
nbformat                           5.1.3
nest-asyncio                       1.5.1
networkx                           2.6.3
nltk                               3.6.5
nose                               1.3.7
notebook                           6.4.5
numba                              0.54.1
numexpr                            2.7.3
numpy                              1.20.3
numpydoc                           1.1.0
olefile                            0.46
opencv-python                      4.7.0.68
openpyxl                           3.0.9
packaging                          21.0
pandas                             1.3.4
pandocfilters                      1.4.3
parso                              0.8.2
partd                              1.2.0
path                               16.0.0
pathlib2                           2.3.6
pathspec                           0.7.0
patsy                              0.5.2
pep8                               1.7.1
pexpect                            4.8.0
pickleshare                        0.7.5
Pillow                             8.4.0
pip                                21.2.4
pkginfo                            1.7.1
pluggy                             0.13.1
ply                                3.11
poyo                               0.5.0
prometheus-client                  0.11.0
prompt-toolkit                     3.0.20
psutil                             5.8.0
ptyprocess                         0.7.0
py                                 1.10.0
pycodestyle                        2.7.0
pycosat                            0.6.3
pycparser                          2.20
pycurl                             7.44.1
pydocstyle                         6.1.1
pyerfa                             2.0.0
pyflakes                           2.3.1
Pygments                           2.10.0
PyJWT                              2.1.0
pylint                             2.9.6
pyls-spyder                        0.4.0
pyodbc                             4.0.0-unsupported
pyOpenSSL                          21.0.0
pyparsing                          3.0.4
pyrsistent                         0.18.0
PySocks                            1.7.1
pytest                             6.2.4
python-dateutil                    2.8.2
python-engineio                    4.3.4
python-lsp-black                   1.0.0
python-lsp-jsonrpc                 1.0.0
python-lsp-server                  1.2.4
python-slugify                     5.0.2
python-socketio                    5.7.2
pytz                               2021.3
PyWavelets                         1.1.1
pyxdg                              0.27
PyYAML                             6.0
pyzmq                              22.2.1
QDarkStyle                         3.0.2
qstylizer                          0.1.10
QtAwesome                          1.0.2
qtconsole                          5.1.1
QtPy                               1.10.0
regex                              2021.8.3
requests                           2.26.0
resolvelib                         0.8.1
rope                               0.19.0
Rtree                              0.9.7
ruamel-yaml-conda                  0.15.100
scikit-image                       0.18.3
scikit-learn                       0.24.2
scikit-learn-intelex               2021.20210714.170444
scipy                              1.7.1
seaborn                            0.11.2
SecretStorage                      3.3.1
Send2Trash                         1.8.0
setuptools                         58.0.4
simplegeneric                      0.8.1
singledispatch                     3.7.0
sip                                4.19.13
six                                1.16.0
sniffio                            1.2.0
snowballstemmer                    2.1.0
sortedcollections                  2.1.0
sortedcontainers                   2.4.0
soupsieve                          2.2.1
Sphinx                             4.2.0
sphinxcontrib-applehelp            1.0.2
sphinxcontrib-devhelp              1.0.2
sphinxcontrib-htmlhelp             2.0.0
sphinxcontrib-jsmath               1.0.1
sphinxcontrib-qthelp               1.0.3
sphinxcontrib-serializinghtml      1.1.5
sphinxcontrib-websupport           1.2.4
spyder                             5.1.5
spyder-kernels                     2.1.3
SQLAlchemy                         1.4.22
statsmodels                        0.12.2
sympy                              1.9
tables                             3.6.1
TBB                                0.2
tblib                              1.7.0
terminado                          0.9.4
testpath                           0.5.0
text-unidecode                     1.3
textdistance                       4.2.1
threadpoolctl                      2.2.0
three-merge                        0.1.1
tifffile                           2021.7.2
tinycss                            0.4
toml                               0.10.2
toolz                              0.11.1
tornado                            6.1
tqdm                               4.62.3
traitlets                          5.1.0
typed-ast                          1.4.3
typing-extensions                  3.10.0.2
ujson                              4.0.2
unicodecsv                         0.14.1
Unidecode                          1.2.0
urllib3                            1.26.7
watchdog                           2.1.3
wcwidth                            0.2.5
webencodings                       0.5.1
Werkzeug                           2.0.2
wheel                              0.37.0
whichcraft                         0.6.1
widgetsnbextension                 3.5.1
wrapt                              1.12.1
wurlitzer                          2.1.1
xlrd                               2.0.1
XlsxWriter                         3.0.1
xlwt                               1.3.0
xmltodict                          0.12.0
yapf                               0.31.0
zict                               2.0.0
zipp                               3.6.0
zope.event                         4.5.0
zope.interface                     5.4.0
IuAyala commented 1 year ago

I can see quite a few issues: Your PYTHONPATH has the value :/usr/local/webots/lib/python3.10: it shouldn't have the : before and after the actual path. Then the actual path is not correct, it should be: /usr/local/webots/lib/controller/python.

I would recommend checking your "~/.bashrc" removing everything there that we added, then use the commands in the readme (main page instructions) of this repo.

Additionally you have a module named "controller" in your libraries, if you are not using it you can remove it pip uninstall controller. But if you use it, then you will have to rename the controller folder (used by Webots).

CountryCousin commented 1 year ago

Hello @IuAyala , I tried updating the python path :/usr/local/webots/lib/python3.10: to the one you recommended /usr/local/webots/lib/controller/python but it didn't change.

can you please tell me how to update it as I am not so proficient with twisting paths in linux

below is what I tried:

(base) page Jan 24 15:46 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo "export PYTHONPATH=$PYTHONPATH:/usr/local/webots/lib/controller/python" >> ~/.bashrc
(base) page Jan 24 15:46 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo $PYTHONPATH
:/usr/local/webots/lib/python3.10:

I ran the following command below in the vscode environment and got this:

(base) page Jan 25 9:37 ~/.../Machine Learning/1/Self Driving Cars [main !] > echo $PYTHONPATH
:/usr/local/webots/lib/python3.10::/usr/local/webots/lib/controller/python
(base) page Jan 25 9:37 ~/.../Machine Learning/1/Self Driving Cars [main !] > 

also when I tried using the automated process of setting up the path using the repo ie [./install_linux.sh], I got this error:

(base) page Jan 24 15:52 ~/.../1/Self Driving Cars/0_install [main !] > ./install_linux.sh
This installation file should work for:
  - Debian based distros (Ubuntu, Pop_OS!, ...)
  - Arch-based distros (Arch, Manjaro, ...)
  - Fedora
Using package manager: apt-get
sudo apt-get install ansible
[sudo] password for page: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ansible is already the newest version (2.9.6+dfsg-1).
0 upgraded, 0 newly installed, 0 to remove and 18 not upgraded.
BECOME password: 
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
ERROR! couldn't resolve module/action 'yay'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/page/Desktop/Machine Learning/1/Self Driving Cars/0_install/general.yml': line 100, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      - name: Install code (Archlinux)
        ^ here
IuAyala commented 1 year ago

First of all, you should remove the "controller" library from your environment, the name is colliding with the Webots module, you can do that by running pip uninstall controller. If you need it for another project I recommend that you can at how to create multiple environments with anaconda.

Fix the paths: You need to have a look at your ~/.bashrc, at the end of the file there should quite a few commands that we added, you can recognise them because they will mention PYTHONPATHor LD_LIBRARY_PATH.

Then you should run:

echo "export WEBOTS_HOME=/usr/local/webots" >> ~/.bashrc
echo "WEBOTS_PYTHON=${WEBOTS_HOME}/lib/controller/python" >> ~/.bashrc
echo "export PYTHONPATH=${PYTHONPATH:+${PYTHONPATH}:}${WEBOTS_PYTHON}" >> ~/.bashrc
echo "export PYTHONIOENCODING=UTF-8" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${WEBOTS_HOME}/lib/controller" >> ~/.bashrc

At this point you should have the right PYTHONPATH and LD_LIBRARY_PATH.

Thank you for taking the time to complete this course. We hope that you found it informative and enjoyable. If you have a moment, we would greatly appreciate it if you could leave us a review of the course. Your feedback will help us to continue to improve and create even better content for you in the future. Thank you for your support!

CountryCousin commented 1 year ago

hey @IuAyala ,

The imports works great now, its time to continue the journey . and yes you got my massive support. Thank you.