AnotherSam
Posts: 14
Joined: Thu Aug 12, 2021 2:37 pm

Can't run python file in terminal, I've tried so many suggestions to no avail

Tue Sep 21, 2021 6:32 am

Hello all,
Bit of history: I have a FGPA eval board (ZCU111) that run a version of python (PYNQ) and i can easily log in using a terminal, and type "python filename.py" and it runs the scripts no problem. I thought I could extend the same process to my PC and Pico.
I know there are COUTNLESS threads on this topic, but I've tried just about everything suggested and I am at a loss. The ultimate goal is to have a python script on a PC toggle a python script on the PICO over the serial port. However, I can't get the simplest example to work.
Pic1.PNG
Pic1.PNG (112.67 KiB) Viewed 629 times
I have a python script that runs just fine in Thonny, but going to the command prompt and typing "python test.py" gave me an error suggesting python wasn't installed, which it most certainly is.
Pic2.PNG
Pic2.PNG (42.79 KiB) Viewed 629 times
The second step is running a python file on the Pico over the serial port. The ToggleLED.py files runs fine in Thonny when connected to the pico. However, not when i log into the pico using a terminal . I understand i need to leave the interpreter in order to run a script, so i tried all the suggestions this forum and others offered. I tried "exit()", cont D, cont B, different syntaxes of the command, you name it, i've tried it. Clearly I am missing something pretty basic here.
(My expertise lies FAR FAR away from this type of work, Thanks for your patience)

User avatar
B.Goode
Posts: 12974
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Tue Sep 21, 2021 7:52 am

I suggest you work through the documentation provided by Raspberry Pi.

https://hackspace.raspberrypi.org/books ... ython-pico;

and

https://datasheets.raspberrypi.org/pico ... on-sdk.pdf




In your second screenshot you have an instance of Thonny configured such that it has a connection to a Pico running microPython which is presenting its REPL prompt [ >>> ].

So that prompt is itself being generated by the microPython interpreter running on the attached Pico board. It is NOT a system-level Terminal or CLI prompt.

In that context it is not appropriate to try to run another instance of a Python interpreter. {A similar misunderstanding to the one dealt with here - viewtopic.php?f=146&t=319643&p=1913629#p1913734}

AnotherSam
Posts: 14
Joined: Thu Aug 12, 2021 2:37 pm

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Tue Sep 21, 2021 11:36 am

B.Goode wrote:
Tue Sep 21, 2021 7:52 am
In your second screenshot you have an instance of Thonny configured such that it has a connection to a Pico running microPython which is presenting its REPL prompt [ >>> ].

So that prompt is itself being generated by the microPython interpreter running on the attached Pico board. It is NOT a system-level Terminal or CLI prompt.
rstanding to the one dealt with here - viewtopic.php?f=146&t=319643&p=1913629#p1913734}
Pic1.PNG
Pic1.PNG (74.76 KiB) Viewed 571 times
Forgive me if I'm wrong here: but not quite. Thonny is in the background simply so you can see the python script. I am using an open source terminal emulator to talk to the pico. I fully understand that ">>>" means the interpreter is running. The question is, can I exit that interpreter?
for example: on my PC i can open the the command terminal I can start python and get the ">>>". I can exit the interpreter "exit()" and simply run my python script.
Pic2.PNG
Pic2.PNG (24.23 KiB) Viewed 571 times
Perhaps I keep asking the question wrong, but can i do the same with the pico and a serial connection? Is there a way to exit the ">>>"?

jayben
Posts: 356
Joined: Mon Aug 19, 2019 9:56 pm

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Tue Sep 21, 2021 11:47 am

If you did find a way to exit from MicroPython, what code would the CPU be running?

Since there is no underlying operating system sitting behind the MicroPython interpreter, the processor would halt or crash, having no code to run.

What you need is a way of emulating the action of Thonny in running code from the REPL prompt, such as rshell https://github.com/dhylands/rshell

horuable
Posts: 181
Joined: Sat Mar 06, 2021 12:35 am

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Tue Sep 21, 2021 5:03 pm

If you have Thonny running in the background disconnect it from Pico by using Run->Disconnect from the top menu.

Then when you connect to Pico via a terminal (I'm using PuTTY, so YMMV) hit CTRL-B, and then CTRL-D (just in case). To run your script simply import it, which will also run it:

Code: Select all

import ToggleLED
(notice lack of the .py extension).

You can stop the execution with standard CTRL-C, but to rerun the script you have to issue a soft reboot (CTRL-D) before importing it again.
AFAIK import is the only way to run a script from the file in REPL.

User avatar
OneMadGypsy
Posts: 359
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Wed Sep 22, 2021 7:35 pm

If I am properly understanding what you want to do, this is one way you can do it.


First download RShell

Then make a directory on your computer something like:

Code: Select all

micropython/
    |____________ sync.sh (or bat if windows)
    |____________ run.sh  (or bat if windows)
    |____________ pico/   <-- this acts as a virtual directory that represents your Pico filesystem
                    |____________ somefile.py
                    |____________ anotherfile.py

For sync.sh you can use the below script. If you are on windows write the bat equivalent for the first actual line, and change the -port argument to whatever COM you are using. The second line tells RShell to exactly mirror the pico directory to the actual Pico. Do not use a main.py. This method makes it unnecessary, and you don't have to worry about something in main.py blocking your scripts. Another thing to note is "mirror" literally means mirror. If a file IS on your Pico and ISN'T in the pico folder ~ it wont be on your Pico after sync. The one exception is if the file is nested in a directory on your Pico. RShell wont have permission to touch that folder for deletion. RShell can add more files to the folder though. If you remove the -mirror cmdarg RShell wont delete any files.

It may seem wrong but "/pyboard/" is correct ... even for the Pico. RShell at some point officially supported the Pico, so you may be able to change that to "/pico/". I would just leave it as it is unless it doesn't work. The "pico" in the below script is referring to the pico/ directory in the above directory structure. In other words "rsync -m pico /pyboard/" is literally saying "sync and mirror everything in the pico folder to the actual Pico". If you want to upload other modules to your Pico, just dump them in the pico/ folder.

Code: Select all

#!/bin/bash

cd ~/Documents/micropython

rshell -p /dev/ttyACM0 --buffer-size 512 -b 115200 rsync -m pico /pyboard/

For run.sh you can use the below script. The first 3 actual lines need to be "windowsified" if you are on windows, as well as assigning the proper port. What this file does is first run sync.sh to get the files to your board, clears the terminal (optional), and then it starts your board and the REPL. These files change directory first because you may want to assign these scripts to a keyboard shortcut in some editor. Without the "cd" you may get caught in a situation where your editor has the CWD set to something else, and when these scripts run they will fail as they can't find "pico/" or "sync.sh".

Code: Select all

#!/bin/bash

cd ~/Documents/micropython

./sync.sh
clear
rshell -p /dev/ttyACM0 --buffer-size 512 -b 115200 repl

From there it's simple. At the REPL prompt ...

Code: Select all

>>> import somefile

If you want a certain file to start automatically when you init run.sh, change the last line to this:

Code: Select all

rshell -p /dev/ttyACM0 --buffer-size 512 -b 115200 repl pyboard '~ import somefile'

One bonus of using this method is that you can use whatever editor you want to code. These scripts handle all the uploading and running so, the editor becomes nothing more than a place to type. You could write all your code in LibreOffice if that's something that makes you happy. It ceases to matter after you have these scripts setup. run and sync don't have to be separate files. I do it this way so I have options. Really the only options are sync and run or just sync. However if you combine the files you will have to do a little trick, cause RShell will want to completely quit the terminal after syncing. IIRC in Windows the way I got around this was by using "start rshell -p COM0 --buffer-size 512 -b 115200 rsync -m pico /pyboard/&PAUSE", then you have to wait for sync to finish and "press any key to continue". You have to have the PAUSE because without out it, since start was used, it will want to immediately start processing the next line after it starts. That would be bad, because you definitely want sync to finish before run is initiated. I don't have a linux solution for combining them or maybe my current method is the solution.


BONUS:

Here is the windows version, except I am really just writing things from old memories. I know I am in the ballpark, though... maybe even spot on. Even if I am spot on, you may still need to at least change the port to the proper one. You may also have to change the baudrate. I have a vague memory of windows not liking this baudrate, but I may be thinking of something else. It may have been the buffer-size. There was something. The RShell repository examples use defaults. If you have trouble revert to whatever the defaults are. If these are the defaults then I am just stupid, but I know I changed them at some point and windows didn't like it, but linux didn't care. I'm assuming these values are the changes I made, cause I'm on the same linux machine.

sync.bat (probably)

Code: Select all

cd %~dp0
rshell -p COM0 --buffer-size 512 -b 115200 rsync -m pico /pyboard/

run.bat (also probably) DO NOT write the word "run" in either of these files, or change the name of this file if you do. Failure to heed this warning is going to lock up your computer with this file infinitely opening itself. I don't even think you can do "run name_of_file". It's just going to think that "name_of_file" is an argument for run.bat.

Code: Select all

cd %~dp0
sync.bat
clr
rshell -p COM0 --buffer-size 512 -b 115200 repl

I know this post is verbose. I wanted to make sure that anyone reading it understood every iota of it. When I was putting all this together I couldn't find anything this hand-holdy to simplify the job. I had to fight my way through a number of issues until I finally got it to work. The information that I did find failed to truly utilize the power that RShell provides. Everything I found was "how to get A file on the Pico", and that file was main.py. I want all the files on the Pico, and I want to load whatever file I choose. This is a very reliable way to do it..
Last edited by OneMadGypsy on Wed Sep 22, 2021 10:03 pm, edited 12 times in total.
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

User avatar
scruss
Posts: 4365
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Wed Sep 22, 2021 8:50 pm

Another way if you're at the MicroPython '>>>' REPL prompt and have a MicroPython program called ToggleLED.py saved on the Raspberry Pi Pico board, this should run it:

Code: Select all

exec(open('ToggleLED.py').read())
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Thu Sep 23, 2021 12:10 pm

It is worth pointing out that 'mpremote' has become the recommended way of interacting with MicroPython from a host command line - https://pypi.org/project/mpremote

That includes mechanisms to have the host's directory seen as if it were the MicroPython file system which simplifies running host-stored programs on the target device.

Code: Select all

pi@Pi3B:/tmp/mprtest $ cat test.py 
import os

def WriteLine(f, s=""):
   f.write(s + "\n")

with open("/remote/result.txt", "w") as f:
  WriteLine(f, str(os.uname()))
  for file in os.listdir("/remote"):
    WriteLine(f, file)

Code: Select all

pi@Pi3B:/tmp/mprtest $ ls
test.py
pi@Pi3B:/tmp/mprtest $ mpremote connect /dev/ttyACM0 mount . run test.py
Local directory . is mounted at /remote
pi@Pi3B:/tmp/mprtest $ ls
result.txt  test.py
pi@Pi3B:/tmp/mprtest $ cat result.txt 
(sysname='picopython', nodename='rp2', release='1.17.0', version='v1.17-63 on 2021-09-21 (GNU 7.3.1 MinSizeRel)', machine='Raspberry Pi Pico with RP2040')
result.txt
test.py

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Can't run python file in terminal, I've tried so many suggestions to no avail

Sat Sep 25, 2021 2:32 pm

jayben wrote:
Tue Sep 21, 2021 11:47 am
If you did find a way to exit from MicroPython, what code would the CPU be running?
That was a question which intrigued me. Becoming unresponsive, putting itself into a loop, sleep loop, or halted state, basically 'stopped', or initiating a soft or hard rest, would be what I would have anticipated - Whatever a Pico SDK program does when it exits its 'main'.

There is an option which can be added to 'mpconfigport.h" to enable 'sys.exit()' so I thought I would give that a whirl -

Code: Select all

#define MICROPY_PY_SYS_EXIT (1)
It doesn't do anything when executed in the REPL but will end a running program immediately and return to the REPL.

Not having that by default was something I had found lacking so I am glad I investigated.

Return to “MicroPython”