Raspberry Pico board, first use, upload port search leads to UnboundLocalError

I am using the latest PlatformIO on Linux Mint Vanessa.
I am using the Raspberry Pico for the first time.
I plugged the Pico into a USB board and see two files (index.htm, info_uf2.txt)
I created a main.cpp with the basic blink program.
I hit compile, and it compiled.
I hit upload and got the udev error, upon which I downloaded and created the /etc/udev/rules.d/99-platformio-udev.rules, and ran sudo service udev restart.

I am part of the dialout and plugdev groups.
When hitting upload I get this error:

Configuring upload protocol...
AVAILABLE: cmsis-dap, jlink, picotool, raspberrypi-swd
CURRENT: upload_protocol = picotool
Looking for upload port...
*** [upload] UnboundLocalError : local variable 'port' referenced before assignment
Traceback (most recent call last):
  File "/home/maxg/.platformio/packages/tool-scons/scons-local-4.4.0/SCons/Action.py", line 1318, in execute
    result = self.execfunction(target=target, source=rsources, env=env)
  File "/home/maxg/.platformio/platforms/raspberrypi/builder/main.py", line 31, in BeforeUpload
    env.AutodetectUploadPort()
  File "/home/maxg/.platformio/packages/tool-scons/scons-local-4.4.0/SCons/Util.py", line 737, in __call__
    return self.method(*nargs, **kwargs)
  File "/home/maxg/.platformio/penv/lib/python3.10/site-packages/platformio/builder/tools/pioupload.py", line 118, in AutodetectUploadPort
    ).find(initial_port)
  File "/home/maxg/.platformio/penv/lib/python3.10/site-packages/platformio/device/finder.py", line 156, in find
    return best_port or port
UnboundLocalError: local variable 'port' referenced before assignment

Any ideas on what I am missing?

Is the platform and core up to date? Please open a CLI and do

pio upgrade --dev
pio pkg update -g -p raspberrypi

then retry uploading.

1 Like

Thank you. I ran both commands.
While it said PlatformIO has been successfully upgraded to 6.1.5a1! the managers installed with the second command reported already up-to-date.

I forgot to say, when I created the first project, PlatformIO downloaded the board-related software.

In any case, the problem remains; it shows the same error.

Then the platform might have an actual logic error here where it always assumes it can find a COM port.

With the Pico beeing in BOOTSEL mode and its USB drive available, can you manually copy the created .pio\build\pico\firmware.uf2 file once to the Pico? Does uploading work thereafter?

1 Like

Yeah, this did work. :slight_smile:

Hi Max,
I seem to be having the same issue but to a ESP32 which I have in the past uploaded to many times before I rebuilt my laptop.
Error message was
*** [upload] UnboundLocalError : local variable ‘port’ referenced before assignment
Traceback (most recent call last):
File “C:\Users\andre.platformio\packages\tool-scons\scons-local-4.4.0\SCons\Action.py”, line 1318, in execute
result = self.execfunction(target=target, source=rsources, env=env)
File “C:\Users\andre.platformio\platforms\espressif32\builder\main.py”, line 35, in BeforeUpload
env.AutodetectUploadPort()
File “C:\Users\andre.platformio\packages\tool-scons\scons-local-4.4.0\SCons\Util.py”, line 737, in call
return self.method(*nargs, **kwargs)
File “C:\Users\andre.platformio\penv\lib\site-packages\platformio\builder\tools\pioupload.py”, line 113, in AutodetectUploadPort
UPLOAD_PORT=SerialPortFinder(
File “C:\Users\andre.platformio\penv\lib\site-packages\platformio\device\finder.py”, line 156, in find
return best_port or port
UnboundLocalError: local variable ‘port’ referenced before assignment

The sketch was a simple serial.print then delay and then loop
I have tried reinstalling and unisntalling it a few times including removing Windows and reinstalling it
Any ideas would be gratefully received
Andrew

But this error is thrown in the core. Please 1. delete C:\Users\andre.platformio\platforms\espressif32\ and 2. use a CLI to pio upgrade --dev. Then re-upload.

Hi Max,
Thank you so much for the information, I actually solved the issue by uninstalling everything then installing Arduino ide 2.0 which with the drivers that loaded with it and then installing VS Code and PlatformIO all worked straight out of the box.
Best Regards
Andrew

1 Like