espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.69k stars 7.29k forks source link

module 'esptool' has no attribute '_main' (IDFGH-7400) #8979

Closed xiaguangbo closed 2 years ago

xiaguangbo commented 2 years ago

Environment

Problem Description

vscode 'Espressif IDF' plugin the 'ESP-IDF Flash device' button:

> Executing task in folder esp32s2: d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe d:\software\esp\esp-idf\components\esptool_py\esptool\esptool.py -p COM6 -b 460800 --before default_reset --after hard_reset --chip esp32s2 write_flash --flash_mode dio --flash_freq 80m --flash_size detect 0x10000 main.bin 0x1000 bootloader/bootloader.bin 0x8000 partition_table/partition-table.bin <

Traceback (most recent call last):
  File "d:\software\esp\esp-idf\components\esptool_py\esptool\esptool.py", line 22, in <module>
    esptool._main()  # type: ignore
AttributeError: module 'esptool' has no attribute '_main'
终端进程“d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe 'd:\software\esp\esp-idf\components\esptool_py\esptool\esptool.py', '-p', 'COM6', '-b', '460800', '--before', 'default_reset', '--after', 'hard_reset', '--chip', 'esp32s2', 'write_flash', '--flash_mode', 'dio', '--flash_freq', '80m', '--flash_size', 'detect', '0x10000', 'main.bin', '0x1000', 'bootloader/bootloader.bin', '0x8000', 'partition_table/partition-table.bin'”已终止,退出代码: 1。

do it manually: can see that the 'esptool' module exists

PS D:\xiaguangbo\project\dap\project\esp32s2> d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import esptool
>>> esptool._main()
esptool.py v3.3.1
usage: esptool [-h] [--chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2}] [--port PORT] [--baud BAUD]
               [--before {default_...
...

I tried adding 'peintf('\n aaa \n')' to 'D:\software\esp.espressif\python_env\idf5.0_py3.8_env\Scripts\esptool.py' and 'D:\software\esp.espressif\python_env\idf5.0_py3.8_env\Lib\site-packages\esptool.py', but I don't see the output, but I can see two outputs when I add it to 'd:\software\esp\esp-idf\components\esptool_py\esptool\esptool.py', apparently 'd:\software\esp\esp-idf\components\esptool_py\esptool\esptool.py' is importing itself

If 'idf.py' is used, it is normal

I renamed 'esptool.py' to 'esptool2.py' and executed it manually, the result is correct: Although there is a 'No such file or directory: 'main.bin'' error in the middle, it is simply solved

PS D:\xiaguangbo\project\dap\project\esp32s2> d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe d:\software\esp\esp-idf\components\esptool_py\esptool\esptool2.py -p COM6 -b 460800 --before default_reset --after hard_reset --chip esp32s2 write_flash --flash_mode dio --flash_freq 80m --flash_size detect 0x10000 main.bin 0x1000 bootloader/bootloader.bin 0x8000 partition_table/partition-table.bin
usage: esptool write_flash [-h] [--erase-all] [--flash_freq {keep,80m,60m,48m,40m,30m,26m,24m,20m,16m,15m,12m}] [--flash_mode {keep,qio,qout,dio,dout}] [--flash_size FLASH_SIZE]
                           [--spi-connection SPI_CONNECTION] [--no-progress] [--verify] [--encrypt] [--encrypt-files <address> <filename> [<address> <filename> ...]]
                           [--ignore-flash-encryption-efuse-setting] [--compress | --no-compress]
                           <address> <filename> [<address> <filename> ...]
esptool write_flash: error: argument <address> <filename>: [Errno 2] No such file or directory: 'main.bin'
PS D:\xiaguangbo\project\dap\project\esp32s2> d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe d:\software\esp\esp-idf\components\esptool_py\esptool\esptool2.py -p COM6 -b 460800 --before default_reset --after hard_reset --chip esp32s2 write_flash --flash_mode dio --flash_freq 80m --flash_size detect 0x10000 build/main.bin 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin
esptool.py v3.3.1
Serial port COM6
Connecting....
Chip is ESP32-S2
Features: WiFi, No Embedded Flash, No Embedded PSRAM, ADC and temperature sensor calibration in BLK2 of efuse V1
Crystal is 40MHz
MAC: 7c:df:a1:04:a5:86
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Wrote 161936 bytes (89867 compressed) at 0x00010000 in 2.2 seconds (effective 593.8 kbit/s)...
Hash of data verified.
Flash params set to 0x022f
Compressed 21872 bytes to 13748...
Wrote 21872 bytes (13748 compressed) at 0x00001000 in 0.5 seconds (effective 330.2 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 442.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
radimkarnis commented 2 years ago

Hello @xiaguangbo, thank you for the detailed report, this will help us a lot! There are reports of this issue in the esptool repository: https://github.com/espressif/esptool/issues/740 EDIT: not the same thing We will fix this ASAP.

dotlambda commented 2 years ago

@radimkarnis I assume it's the same thing. The reason this happens is that sys.executable is in d:\software\esp\.espressif\python_env\idf5.0_py3.8_env\Scripts while esptool.py is in d:\software\esp\esp-idf\components\esptool_py\esptool. It's definitely a case fixed by https://github.com/espressif/esptool/pull/742.

radimkarnis commented 2 years ago

@dotlambda This issue seems to be a result of this wrapper script importing itself. This wrapper is a part of ESP-IDF and serves as a compatibility layer. You can see that in this one, we try to remove the path to the script itself:

    sys.path.remove(os.path.dirname(os.path.realpath(__file__)))  # do not import this script

https://github.com/espressif/esptool/pull/742 concerns a wrapper script, which is directly in the esptool repository. The issue is similar, but I don't think both of these can be solved in the same place.

RavioliSauce commented 2 years ago

@dotlambda I was getting the same "module 'esptool' has no attribute '_main'" error. Your fix worked for me! I am now able to communicate with my ESP8266!

radimkarnis commented 2 years ago

@RavioliSauce, thanks for confirming this! Could you please verify if this also solves the issue?

xiaguangbo commented 2 years ago

@radimkarnis Today I reinstalled the whole environment and it works fine, I don't know if this is a code fix or something is wrong with my previous environment

radimkarnis commented 2 years ago

@xiaguangbo thanks for the info and cooperation!

We have released esptool v4.0.1 with a fix. Hopefully, this definitely solves the issue.

wreyford commented 2 years ago

I used ESP-IDF setup in Visual Studio Code insiders, and installed Master and tools using the interface, on a Windows10 PC, and got the same issue now. I cannot flash my code to ESP32-S3-DevKitC-1

Traceback (most recent call last): File "c:\esp\esp-idf-master\esp-idf\components\esptool_py\esptool\esptool.py", line 22, in esptool._main() # type: ignore AttributeError: module 'esptool' has no attribute '_main'

xiaguangbo commented 2 years ago

@wreyford Delete the esptool_py folder and use git pull, delete ./espressif/python_env and reinstall using the install script, try

LAestafa commented 1 month ago

hola cuando intento flashear un esp32 me sale este error Traceback (most recent call last): File "C:\Users\Osniel\AppData\Local\Programs\Python\Python312\Scripts\esptool.py", line 37, in esptool._main() ^^^^^^^^^^^^^ AttributeError: module 'esptool' has no attribute '_main'

como pudiera solucionar esto

LAestafa commented 1 month ago

porque me sale este error

Traceback (most recent call last): File "C:\Users\Osniel\AppData\Local\Programs\Python\Python312\Scripts\esptool.py", line 37, in esptool._main() ^^^^^^^^^^^^^ AttributeError: module 'esptool' has no attribute '_main'

si alguien pudiera ayudar

invictus-anic3tus commented 1 month ago

@radimkarnis I have the exact same problem! I don't know what to do