How to persistently save code.py file and start it from CircuitPython without PC connection?

General

Andy

5 years ago

Hello all!

I just bought Gamebuino META and exploring Python Language with my child. If we save our "code.py" file into Cortex's flash from Mu editor - all works perfectly, program correctly starts and run. But after exit from Circuit Python "code.py" will automatically erase from flash and progam can not be started again without Mu.

Is anybody knows how to save Python program persistently and start it from CircuitPython without a PC connection? I didn't found any docs about this problem.

Regards,

Andy

Thierry

NEW 5 years ago

I just received my Gamebuino and did not had time to try, but there are several file names convetion available.

Did you try with main.py instead of code.py?

jicehel

NEW 5 years ago

atm the program have to be named code.py to be executed by Meta but you can copy / paste your code in Notepad and make a save with the name you want (a more meaning name ...)

Thierry

NEW 5 years ago

from circuitpython documentation:

CircuitPython looks for a code file on the board to run. There are four options: code.txt, code.py, main.txt and main.py. CircuitPython looks for those files, in that order, and then runs the first one it finds. While we suggest using code.py as your code file, it is important to know that the other options exist. If your program doesn't seem to be updating as you work, make sure you haven't created another code file that's being read instead of the one you're working on.

So in your case, it should already work... 

Steph

NEW 5 years ago

Hi Andy,

Indeed, the content of the CIRCUITPY volume is volatile... I do not believe that it is currently possible to keep its contents after the volume has been unmounted. In addition, the volume can be unmounted suddenly. It is therefore prudent not to work directly on it... unless your code editor can keep your file, even if the volume has been unmounted.

However, if you are on macOS (like me), you can use a handy tool called fswatch to monitor a folder and execute a shell command when an event is detected in the observed folder. fswatch can therefore observe the folder that contains all your Pyhton scripts and, as soon as one of them is modified (saved), copy it to CIRCUITPY with the appropriate name (code.py or main.py).

fswatch is available on GitHub. But you can also install it by using Homebrew:

brew install fswatch

Then, open a Terminal and go to the directory that contains your Python scripts. Then execute the following command:

fswatch -0 . | while read -d "" file; do   \
  if [ ${file: -3} == ".py" ]; then        \
    cp ${file} /Volumes/CIRCUITPY/code.py; \
  fi;                                      \
done

Each time you modify a Python script in this folder, it will automatically be copied to CIRCUITPY with the name code.py.

Steph

5 years ago

For Linux, you can use the software suite inotify-tools :

apt install inotify-tools

In particular, look at the inotifywait program:

inotifywait simply blocks for inotify events, making it appropriate for use in shell scripts.

Steph

NEW 5 years ago

Steph Steph

For Linux, you can use the software suite inotify-tools :

apt install inotify-tools

In particular, look at the inotifywait program:

inotifywait simply blocks for inotify events, making it appropriate for use in shell scripts.

Steph

NEW 5 years ago

If you're on Windows... sorry, I can't help you :-(

Thierry

NEW 5 years ago

Wait what ?

python files are not persisted ?

Thierry

NEW 5 years ago

Now that I have my Meta, I was able to test this.

And it is strange: the file code.py is still on the gamebuino, I can see it in my file browser.

It is just not seen by CircuitMetaPython, don't know why.

olav

NEW 5 years ago

Thank you for porting CircuitPython to the META. I have tried v0.0.3 on both Linux and Windows 10. While on Windows I get the CIRCUITPY disk and can store and execute code.py from it, on Linux, Python crashes after pressing "A" with "Error 2 - Hard Fault".

Sorunome

5 years ago

what is your code.py file are you trying to use?

JPBONNAFOUS

5 years ago

On Linux V0.0.4 works fine and the file is not persistent on CIRCUITPY :-(.
The REPL works with "screen /dev/ttyACM0 -b 115200"
That rocks !

Sorunome

NEW 5 years ago

Currently, with these early versions of CircuitPython to gamebuino, if you exit circuitpython your program does indeed get erased. This means that you will need to copy the code.py file again from your computer after loading CircuitPython again. So it is very good for quick testing and learning! More permanent storage still has to come

Sorunome

NEW 5 years ago

olav olav

what is your code.py file are you trying to use?

Andy

NEW 5 years ago

Hello All! Big thanx for all answers, folks. 

Steph

We using Mu on Windows 7 and all works good). But thanks for very useful way to keep program updated (we plan to buy Rasberry Pi and wrote Python programs on it).

Thierry

There are four options: code.txt, code.py, main.txt and main.py.

Ok, thanks, it also useful we will checks this) 

Sorunome

Currently, with these early versions of CircuitPython to gamebuino, if you exit circuitpython your program does indeed get erased. 

It's sad to hear this(. IMHO it isn't hard to improve - need to allow CircuitPython grab code.txt, code.py, main.txt or main.py from root folder of SD-card).

what is your code.py file are you trying to use?

Just a simple game - cannon fires a bullet, that fly with parabolic path and must hit a target -). My son starts to learn physics on his school and we wants to apply knowledge -)

JPBONNAFOUS

NEW 5 years ago

olav olav

On Linux V0.0.4 works fine and the file is not persistent on CIRCUITPY :-(.
The REPL works with "screen /dev/ttyACM0 -b 115200"
That rocks !

Sorunome

5 years ago

as mentioned in this thread, unfortunately this stuff isn't persistent yet

Sorunome

NEW 5 years ago

JPBONNAFOUS JPBONNAFOUS

as mentioned in this thread, unfortunately this stuff isn't persistent yet