MicroPython code deployment is different in various boards. For some reason, My ESP8266 doesn't work any more for webREPL, even I reflash the stable firmware time by time.
And I have to test the code via serial. It is possible to paste the code via REPL, and save the pasted code string into a file in filesystem and get it runing.
But I find an issue for this mode. No matter Ctrl-A for raw REPL mode or Ctrl-D paste mode, the '\r\n' or '\x0D\x0A' will be converted into real return and newline anyway. When this string is saved into filesystem, the python code has syntax error.
For example:
'''
if not line or line == b'\r\n':
break
'''
will be saved as:
'''
if not line or line == b'
: break
'''
The latter will be invalid python code.
Now I can not get them works. Sad.
How to transfer code properly via REPL paste mode?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to transfer code properly via REPL paste mode?
I use two methods. Paste mode is selected with ctrl-e. This allows you to paste code and immediately run it with ctrl-d, and is a great feature of MicroPython for running small samples.
Longer programs should be saved as a file on your PC then transferred to the target. There are various ways to do this but I favour rshell as it has a number of powerful features.
Longer programs should be saved as a file on your PC then transferred to the target. There are various ways to do this but I favour rshell as it has a number of powerful features.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to transfer code properly via REPL paste mode?
Thanks, @pythoncoder,
Your answer is very helpful, I can not wait to try out rshell.
Actually I tried WebREPL and command line at first, but for some unknown reason, now WebREPL keeps me trying to "import webreply_setup", and refuse to start webrepl anyway. So that's why I tried to use Ctrl-E, to paste code, and then write pasted code into a remote python code to run. What a big loop.
And in Ctrl-E mode, if I try to transfer code with "\r\n", now I have to escapte them first, by writing "\\r\\e".
Anyway, now I got extra tools like rshell/uPyLoader to transfer source file.
Thank you.
Your answer is very helpful, I can not wait to try out rshell.
Actually I tried WebREPL and command line at first, but for some unknown reason, now WebREPL keeps me trying to "import webreply_setup", and refuse to start webrepl anyway. So that's why I tried to use Ctrl-E, to paste code, and then write pasted code into a remote python code to run. What a big loop.
And in Ctrl-E mode, if I try to transfer code with "\r\n", now I have to escapte them first, by writing "\\r\\e".
Anyway, now I got extra tools like rshell/uPyLoader to transfer source file.
Thank you.
Re: How to transfer code properly via REPL paste mode?
Use Python's raw strings:
ctrl-E
code=r'''(paste)'''
ctrl-D
then save that variable into the target file ( with open('target.py','w') as f: f.write(code) )
It'll transfer everything except nested triple-quoted strings, without additional backslash-escaping.
ctrl-E
code=r'''(paste)'''
ctrl-D
then save that variable into the target file ( with open('target.py','w') as f: f.write(code) )
It'll transfer everything except nested triple-quoted strings, without additional backslash-escaping.