Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'Cryptodome' #583

Closed
ndfred opened this issue Apr 10, 2020 · 24 comments
Closed

ModuleNotFoundError: No module named 'Cryptodome' #583

ndfred opened this issue Apr 10, 2020 · 24 comments

Comments

@ndfred
Copy link

ndfred commented Apr 10, 2020

When flashing a new device with

Starting smart config pairing procedure
Waiting for the device to install the intermediate firmware
Traceback (most recent call last):
  File "./smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/fred/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/fred/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'

This is with the latest checkout (release v2.4.3) on my Raspberry Pi 4. I'll try and fix the issue and report back.

@ndfred
Copy link
Author

ndfred commented Apr 10, 2020

It looks like install_prereq.sh is installing pycryptodomex but not pycryptodome, I'll try and install that and re-run the process:

$ sudo -H python3 -m pip install --upgrade pycryptodome
$ ./start_flash.sh

Edit: still not working, same message as before plus running the tests manually fails import, I'll keep digging:

$ python -m Cryptodome.SelfTest
/usr/bin/python: No module named Cryptodome
$ python3 -m Cryptodome.SelfTest
Skipping AESNI tests
Skipping test of PCLMULDQD in AES GCM
[...]

Suspecting the script is running python expecting python 3, but on my system it is python 2.

Very odd, if I run main.py directly it also fails to import:

$ ./scripts/smartconfig/main.py
Traceback (most recent call last):
  File "./scripts/smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/fred/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/fred/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'
$ python3 ./scripts/smartconfig/main.py
Traceback (most recent call last):
  File "./scripts/smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/fred/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/fred/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'

@ndfred
Copy link
Author

ndfred commented Apr 10, 2020

I was actually in a python virtual env which is why python wasn't seeing the packages 🤦‍♂️

It might be best to install the tuya-convert python packages in a virtual env to make it cleaner, and that may avoid issues like this in the future. I'll close this as this was really my mistake.

@ndfred ndfred closed this as completed Apr 10, 2020
@kueblc
Copy link
Collaborator

kueblc commented Apr 10, 2020

It looks like install_prereq.sh is installing pycryptodomex but not pycryptodome

pycryptodomex is intentional. pycryptodomex and pycryptodome have an important distinction, while pycryptodome strives to be a drop in replacement for the old Crypto package, pycryptodomex uses the Cryptodome name. This ensures that if you have the deprecated Crypto package installed, we use the correct package.

Might your issues be at all related to #575? Or rather that the issue presented in #575 is related to the python virtual env?

It might be best to install the tuya-convert python packages in a virtual env to make it cleaner, and that may avoid issues like this in the future. I'll close this as this was really my mistake.

Feel free to submit a PR :)

@ministryofsillywalks
Copy link

I'm having the same issue however I didn't use any virtual environment to install tuya-convert.
Sadly I have no clue what the hell is going on with python and whatnot ;)
Any nudge in the right direction would be great.

@micooke
Copy link

micooke commented Jun 23, 2020

I had this same issue, which was a result of installing the prerequisites as sudo - and then running start_flash.sh without sudo.

So to reword this - manually installing the prerequisites from install_prereq.sh without sudo fixed my issue (or running start_flash.sh with sudo).

@RolandTalstra
Copy link

I'm experiencing the same problem, however sudo ./start_flash.sh doesn't work for me, the error remains the same

I ran the prereq's again, and re-installed tuya_convert, but to no avail.

Any more tips?

@abdalians
Copy link

getting the same error, any idea how to fix this?

@JohannCR
Copy link

Same error. Any news ?

@KDMcMullan
Copy link

Oooh, this is odd. I've not had this error before, but I have it now. I haven't used tuya-convert in a few months and I can't imagine what I might've done in the meantime!

@KDMcMullan
Copy link

I've just done a complete fresh install on a "virgin" Raspberry Pi and got the same error message. Are there any clues what might be going on? There are still a remarkable number of devices (and users) around which can really benefit from this method, if it's still supported. Thanks.

@TungstenE2
Copy link

TungstenE2 commented Apr 9, 2021

same here, using Pi 3+ running Buster


1. Connect any other device (a smartphone or something) to the WIFI vtrust-flash
   This step is IMPORTANT otherwise the smartconfig may not work!
2. Put your IoT device in autoconfig/smartconfig/pairing mode (LED will blink fast). This is usually done by pressing and holding the primary button of the device
   Make sure nothing else is plugged into your IoT device while attempting to flash.
3. Press ENTER to continue


======================================================
Starting smart config pairing procedure
Waiting for the device to install the intermediate firmware
Traceback (most recent call last):
  File "./smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/pi/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/pi/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'
........................................................................................................................
Timed out while waiting for the device to (re)connect
======================================================
Attempting to diagnose the issue...
No ESP82xx based devices connected according to your wifi log.
Here is a list of all the MAC addresses that connected:
0c:8f:ff:4xxxxxxxxxxx

If you see your IoT device in this list, it is not an ESP82xx based device.
Otherwise, another issue may be preventing it from connecting.
For additional information, check the *.log files inside the scripts folder.
Please include these logs when opening a new issue on our GitHub issue tracker.
======================================================
Do you want to try flashing another device? [y/N] N
======================================================
Cleaning up...
No screen session found.
No screen session found.
No screen session found.
Closing AP
Exiting...
pi@raspberrypi:~/tuya-convert $ 

@TungstenE2
Copy link

why is this ticket closed with no solution?

also this ticket is closed: fashberg/WThermostatBeca#39

@kueblc
Copy link
Collaborator

kueblc commented Apr 9, 2021

why is this ticket closed with no solution?

@TungstenE2, the original ticket author @ndfred closed this after posting the solution

I was actually in a python virtual env which is why python wasn't seeing the packages man_facepalming

It might be best to install the tuya-convert python packages in a virtual env to make it cleaner, and that may avoid issues like this in the future. I'll close this as this was really my mistake.

@bachoo786
Copy link

has anyone tried to resolve this issue? I cant seem to be using tuya convert anymore and its a shame. any help please?

@micooke
Copy link

micooke commented Jun 3, 2021

It's an issue of polluted path. It's not an issue with tuya-convert.

Python is telling you it can't find this module, which means it is not installed for the python env you are using.

Noting the general issue that sudo python install installs module to a system-wide location, and a non sudo install to a user location.

@smizles
Copy link

smizles commented Jul 5, 2021

I have the exact same behavior when I run ./start_flash.sh where it errors out with the following message:

Waiting for the device to install the intermediate firmware
Traceback (most recent call last):
  File "./smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/pi/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/pi/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'

I originally ran ./install_prereq.sh and ./start_flash.sh without sudo first, but both scripts asked for [sudo] password for pi: at various points in the script. NOTE: ./install_prereq.sh does have this error shown at the end:

Collecting git+https://github.com/drbild/sslpsk.git
  Cloning https://github.com/drbild/sslpsk.git to /tmp/pip-req-build-fya70jk6
Please do not run git as root, your regular user account is enough :)

If you need to run git with root rights for some other application than
what comes preinstalled on this image you can remove this sanity check:

    sudo rm /root/bin/git

You might have to restart your login session after doing that.
Command "git clone -q https://github.com/drbild/sslpsk.git /tmp/pip-req-build-y186am_q" failed with error code 1 in None

However, I am able to manually run git clone -q https://github.com/drbild/sslpsk.git /tmp/pip-req-build-y186am_q after the script finishes without any errors. I also noticed that after I read this thread when I reran it with sudo I got basically the same error but the /tmp/pip-req-build-... folder changes.

My main roadblock is I get the following error after running ./start_flash.sh:

Waiting for the device to install the intermediate firmware
Traceback (most recent call last):
  File "./smartconfig/main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/pi/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/pi/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'
........................................................................................................................
Timed out while waiting for the device to (re)connect

Per @ndfred 's suggestion above, I tried running sudo -H python3 -m pip install --upgrade pycryptodome and (I think) I must not have had it because it executed with the following message:

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pycryptodome
  Downloading https://www.piwheels.org/simple/pycryptodome/pycryptodome-3.10.1-cp35-abi3-linux_armv7l.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 243kB/s
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.10.1

However, again following @ndfred 's comment, I get the following errors after running their respective commands:

pi@octopi:~/tuya-convert $ python -m Cryptodome.SelfTest
/usr/bin/python: No module named Cryptodome
pi@octopi:~/tuya-convert $ python3 -m Cryptodome.SelfTest
/usr/bin/python3: Error while finding module specification for 'Cryptodome.SelfTest' (ModuleNotFoundError: No module named 'Cryptodome')

I double-checked the version information, and it appears that I have both versions of python:

pi@octopi:~/tuya-convert/scripts $ python --version
Python 2.7.16
pi@octopi:~/tuya-convert/scripts $ python3 --version
Python 3.7.3

Also, I tried to run main.py with manually specifying python3 and got this error message:

pi@octopi:~/tuya-convert/scripts/smartconfig $ python3 main.py
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    from smartconfig import smartconfig
  File "/home/pi/tuya-convert/scripts/smartconfig/smartconfig.py", line 43, in <module>
    from multicast import multicast_head, encode_multicast_body
  File "/home/pi/tuya-convert/scripts/smartconfig/multicast.py", line 12, in <module>
    from Cryptodome.Cipher import AES
ModuleNotFoundError: No module named 'Cryptodome'

Here are similar issues I have checked:

  1. Gosund SP1 cant flash it ! #643
  2. WIFI AP not working for flashing #754
  3. pin PyCryptodomex to version 3.9.4 #575
  4. Cryptodome module not found error fashberg/WThermostatBeca#39

Other information:

EDIT: Here are my current log files:
smarthack-mqtt.log
smarthack-psk.log
smarthack-udp.log
smarthack-web.log
smarthack-wifi.log

@smizles
Copy link

smizles commented Jul 5, 2021

UPDATE: I'm getting a different error from pi@octopi:~/tuya-convert/scripts/smartconfig $ python3 main.py after running sudo pip3 install pycryptodomex and letting that install. I will try to reflash shortly because the error looks like the error is related to the AP not running. 🤞

Sorry for some minor cross-posting, but this other thread was the second most promising: #643

2nd UPDATE: I noticed I still had the following errors in smarthack-psk.log and smarthack-web.log respectively:

Traceback (most recent call last):
  File "./psk-frontend.py", line 6, in <module>
    import sslpsk
ModuleNotFoundError: No module named 'sslpsk'
Traceback (most recent call last):
  File "./fake-registration-server.py", line 9, in <module>
    import tornado.web
ModuleNotFoundError: No module named 'tornado'

Both appear to have been solved by running sudo pip3 install sslpsk and sudo pip3 install tornado respectively and letting each finish installing before continuing on. I will try to reflash again, but I wanted to update my troubleshooting as I went. BTW, I still get the error Command "git clone -q https://github.com/drbild/sslpsk.git /tmp/pip-req-build-m7e4a1si" failed with error code 1 in None when I re-run ./install_prereq.sh, but at least I'm not getting anymore ModuleNotFoundError: No module named 'sslpsk' errors in my smarthack-psk.log file

NOTE: This may be an issue with trying to run tuya-convert from a spare RPi running Octopi because I have found other threads of people having issues from this distribution - but it should theoretically be supported because Octopi is just a customized distro of Raspberry Pi OS which is itself a Debian distro, right?

@micooke
Copy link

micooke commented Jul 5, 2021

So the install prerequisites isn't quite correct, as there is a flow through of sudo where to the pip commands - so yeah that's not right. But you can get around this by installing them manually. A big hint is that whenever you get a ModuleNotFoundError: No module named... error, that's directly from python. That module is not on pythons path. Think of installing a python package as sudo as installing it as another user (because it is), so you need to match the install user to whatever user you are running your program as. In general, don't install python packages as sudo unless they are required by a system utility or a python utility that is daemonised.

@smizles - you are installing pycryptodome as sudo, sudo -H python3 -m pip install --upgrade pycryptodomex then you ran the test as non sudo python3 -m Cryptodome.SelfTest . FYI - generally python is linked to python2.7 (you can test this with python --version ) and python modules are generally python version dependent, but they certainly are when you install them. So use pip3 to install for python3, and pip for python2.7

Also if I'm not being clear - try running tuya-convert as sudo, as that is what you have installed the modules as. Or try installing the prerequisite modules as non-sudo e.g. python3 -m pip install --upgrade paho-mqtt tornado git+https://github.com/drbild/sslpsk.git pycryptodomex

@smizles
Copy link

smizles commented Jul 9, 2021

Hi @micooke,
Thanks for taking the time to write such a thorough answer! I'll definitely have to try running that last code for manually installing the prerequisite modules as non-sudo, but I think I finally have them because now the script errors out after Smart config sent. / Resending smart config.... Maybe this is related to my device's firmware being incompatible (is there anything in any of the log files that might tell me so? I didn't see any glaring anomalies after a quick glance at each).

@smizles - you are installing pycryptodome as sudo, sudo -H python3 -m pip install --upgrade pycryptodomex then you ran the test as non sudo python3 -m Cryptodome.SelfTest .

Here's an interesting fact, I actually did not run either of the two scripts as sudo during my first attempt. The system actually paused midway through the script and prompted me for my sudo password (is this a big with the script or some default setting on my Raspberry Pi that I should change?). Anywho, either way the three modules I specified above were never installed (for any user, sudo or not apparently). The modules successfully installed when I manually ran the pip command specified in my second post (using sudo in that particular instance was testing a theory somebody else had in another comment).

FYI - generally python is linked to python2.7 (you can test this with python --version ) and python modules are generally python version dependent, but they certainly are when you install them. So use pip3 to install for python3, and pip for python2.7

Sorry for any miscommunication between my original and update comments. Apparently the modified Raspberry Pi OS that ships with Octopi comes preloaded with Python 2.7.16 and Python 3.7.3, hence why I tried specifying pip3 and python3 to prevent ambiguity.

@bhlohumi
Copy link

bhlohumi commented Oct 9, 2023

I installed this particular module as part of pipeline and it worked. Let me know if this helps.

@MATN92
Copy link

MATN92 commented Dec 20, 2023

this help

sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED

@amestsantim
Copy link

this help

sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED

This worked for me

@muava12
Copy link

muava12 commented Apr 2, 2024

i get this error
AttributeError: module 'Cryptodome.Cipher.AES' has no attribute 'new'

@muava12
Copy link

muava12 commented Apr 2, 2024

i get this error AttributeError: module 'Cryptodome.Cipher.AES' has no attribute 'new'

already resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests