PyUPDI problems

Hello,

I have some problems when uploading code for AVR via PyUPDI. The upload button is not working, I get some errors:

Processing ATmega808 (platform: atmelmegaavr; board: ATmega808; 
framework: )
------------------------------------------------------------------------ 
------------------------------------------------------------------------ 
----------------------------------------------------------------Verbose 
mode can be enabled via `-v, --verbose` option
CONFIGURATION: 
https://docs.platformio.org/page/boards/atmelmegaavr/ATmega808.html
PLATFORM: Atmel megaAVR (1.5.0) > ATmega808
HARDWARE: ATMEGA808 16MHz, 1KB RAM, 8KB Flash
PACKAGES:
 - tool-avrdude-megaavr 1.60300.191015 (6.3.0)
 - toolchain-atmelavr 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder ->
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\ATmega808\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project 
Inspect"
RAM:   [          ]   0.0% (used 0 bytes from 1024 bytes)
Flash: [          ]   1.5% (used 124 bytes from 8192 bytes)
Configuring upload protocol...
AVAILABLE: jtag2updi
CURRENT: upload_protocol = jtag2updi
Looking for upload port...
Auto-detected: COM3
Forcing reset using 1200bps open/close on port COM3
Uploading .pio\build\ATmega808\firmware.hex
Traceback (most recent call last):
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\Scripts\pyupd 
i-script.py",  line 33, in <module>
    sys.exit(load_entry_point('pyupdi==0.0.0', 'console_scripts', 
'pyupdi')())
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\Scripts\pyupd 
i-script.py",  line 25, in importlib_load_entry_point
    return next(matches).load()
  File "C:\Users\gigauser\.platformio\python3\lib\importlib\metadata.py", line 
77, in load
    module = import_module(match.group('module'))
  File "C:\Users\gigauser\.platformio\python3\lib\importlib\__init__.py", line 
127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in 
_find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in 
exec_module
  File "<frozen importlib._bootstrap>", line 241, in 
_call_with_frames_removed
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\__init__.py",  line 1, in <module>
    from .pyupdi import _main
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\pyupdi.py",  line 11, in <module>
    from updi.nvm import UpdiNvmProgrammer
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\nvm.py",  line 6, in <module>
    from updi.application import UpdiApplication
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\application.py",  line 7, in <module>
    from updi.link import UpdiDatalink
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\link.py",  line 7, in <module>
    from updi.physical import UpdiPhysical
  File "C:\Users\gigauser\AppData\Local\Programs\Python\Python310\lib\site-pack 
ages\updi\physical.py",  line 6, in <module>
    import serial
  File "C:\Users\gigauser\.platformio\penv\lib\site-packages\serial\__init__.py 
",  line 29, in <module>
    from serial.serialwin32 import Serial
  File "C:\Users\gigauser\.platformio\penv\lib\site-packages\serial\serialwin32 
.py",  line 15, in <module>
    import ctypes
  File "C:\Users\gigauser\.platformio\python3\lib\ctypes\__init__.py", 
line 8, in <module>
    from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: The specified 
module could not be found.
*** [upload] Error 1
======================================================================== 
==================  [FAILED] Took 2.24 seconds 
======================================================================== 
==================The  terminal process 
"C:\Users\gigauser\.platformio\penv\Scripts\platformio.exe 'run', 
'--target', 'upload', '--environment', 'ATmega808'" terminated with exit 
code: 1.

If I call PyUPDI as command line, everything works well:

pyupdi -d mega808 -c COM3 -b 115200 -f .pio/build/ATmega808/firmware.hex
Device info: {'family': 'megaAVR', 'nvm': 'P:0', 'ocd': 'D:1', 'osc': 
'3', 'device_id': '1E9326', 'device_rev': '0.0'}
Programming successful

My platfomio.ini looks like this:

[env:ATmega808]
platform = atmelmegaavr
board = ATmega808
framework =
upload_speed = 115200
upload_flags =
    -d
    mega808
    -c
    $UPLOAD_PORT
    -b
    $UPLOAD_SPEED
upload_command = pyupdi $UPLOAD_FLAGS -f $SOURCE

Any ideas how to solve the Upload-Button problem?

Thanks!

The fundamental problem here that you’ve installed pyupdi in your system’s Python installation (C:\Users\gigauser\AppData\Local\Programs\Python\Python310) but PlatformIO is running in its isolated Python environment with a different Python version (C:\Users\gigauser\.platformio\penv). Mixing them together should be avoided at all costs.

When you call into pyupdi in the PlatformIO context, the Python packages environment variable etc. is set to that although your system’s Python executable is found, it picks up packages from PlatformIO’s python installation. This leads likely to errors like these.

Try to find a way to install and invoke the PyUPDI tool within the context of PlatformIO’s Python installation. From within a CLI you can execute pio system info to get the “Python exectuable” path. Use that path together with -m pip install pyupdi to try and install PyUPDI in PlatformIO’s python enviornment. Then, instead of using pyupdi in your upload command, you can reference PlatformIO’s python installation using the $PYTHONEXE (example) and use it with like -m pyupdi to invoke the programing tool with in the modules of the referenced Python executable.

Hello,

there was obviously a misunderstanding from my side from WHERE I have to install PyUPDI. I was not aware that Platformio has it’s own PYTHON. I installed it now again in the Platformio Terminal (before was Powershell…) and now it runs as it should.

Thanks! :slight_smile: