in my script, I read the contents of the "apps" folder where there are py files inside, in the main I select one of these files and execute it with this command "exec (open (filename) .read ())" it works but not with the expected result. I would like to replace the main with another file py
is it possible?
Change the main program runtime
Re: Change the main program runtime
you could put something like
into main.py?
Code: Select all
import 'my_other_prog'
into main.py?
-
- Posts: 4
- Joined: Fri Jun 24, 2022 10:29 am
Re: Change the main program runtime
no, in this way I'm importing the other file but it doesn't change the code behaviour
I need to find a proper way to have multiple script in the RP2040 and a main script that switch between them
I need to find a proper way to have multiple script in the RP2040 and a main script that switch between them
Re: Change the main program runtime
What are the results you expect and what do you get ?urbanspaceman wrote: ↑Fri Jun 24, 2022 8:49 pm"exec (open (filename) .read ())" it works but not with the expected result.
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas
My repositories: https://github.com/karfas
Re: Change the main program runtime
importing and executing are really the same thing. You can think of MicroPython as doing `import main` to run your main.py.urbanspaceman wrote: ↑Sat Jun 25, 2022 9:22 amno, in this way I'm importing the other file but it doesn't change the code behaviour
I need to find a proper way to have multiple script in the RP2040 and a main script that switch between them
Re: Change the main program runtime
Hoping to be of some help-
Time ago, I have faced the need of how to choose wich micropython (example to run) script to import at runtime inside the main.py file.
The following is the WEB Server solution (*) which uses a Color Picker to choose the micropython script.
The code depends on the ESP-01S board used to add WiFi connectivity to the Raspberry Pi Pico. Clearly, "mutatis mutandi", with the new Pico W card the code will be simplified and no additional wiring will be required.
Coming to the code, the main work depends on the values of the R and G colors.
The two bytes of RED are used to select the number of the script (in the micropython listdir ()) that will be used in the string of the import statement in main.py
For example, if we enter the hexadecimal value "0E" (dec 15), this means that the file listdir () [15] will be imported - in listdir () [15] it matches the string of the micropython file, for example:
'rgb_rp2.py'. From this string, by cutting the end ".py" we build a new string "import rgb_rp2" which will become the argument of the exec instruction.
exec ('import rgb_rp2')
On the browser WEB page , we check if the file name is the one that we want to be imported, and if all is OK we proceed by retyping the file number "0E" followed by the "00" ( the Green two bytes) which will confirm the execution of the micropython's import instruction. ( see the code )
The RGB "000000" value is used in the example code only to make a reboot of the board.
If you need to choose between a small range of micropython files see the (**) about the Micropython Menu WEB Server where the code is simplified .
Hoping to make the things easier, see the pictures.
(*)
for the code of the IR ( Infrared) solution see near the end of the following YouTube video:
https://m.youtube.com/watch?v=cBZi8MhPaK0
(**)
for the Micropython Menu WEB Server see:
Raspberry Pi Pico - Micropython Menu, RGB LED & Servo WEB Servers
https://m.youtube.com/watch?v=Iqn3MU_FaiM
Time ago, I have faced the need of how to choose wich micropython (example to run) script to import at runtime inside the main.py file.
The following is the WEB Server solution (*) which uses a Color Picker to choose the micropython script.
The code depends on the ESP-01S board used to add WiFi connectivity to the Raspberry Pi Pico. Clearly, "mutatis mutandi", with the new Pico W card the code will be simplified and no additional wiring will be required.
Coming to the code, the main work depends on the values of the R and G colors.
The two bytes of RED are used to select the number of the script (in the micropython listdir ()) that will be used in the string of the import statement in main.py
For example, if we enter the hexadecimal value "0E" (dec 15), this means that the file listdir () [15] will be imported - in listdir () [15] it matches the string of the micropython file, for example:
'rgb_rp2.py'. From this string, by cutting the end ".py" we build a new string "import rgb_rp2" which will become the argument of the exec instruction.
exec ('import rgb_rp2')
On the browser WEB page , we check if the file name is the one that we want to be imported, and if all is OK we proceed by retyping the file number "0E" followed by the "00" ( the Green two bytes) which will confirm the execution of the micropython's import instruction. ( see the code )
The RGB "000000" value is used in the example code only to make a reboot of the board.
If you need to choose between a small range of micropython files see the (**) about the Micropython Menu WEB Server where the code is simplified .
Hoping to make the things easier, see the pictures.
(*)
for the code of the IR ( Infrared) solution see near the end of the following YouTube video:
https://m.youtube.com/watch?v=cBZi8MhPaK0
(**)
for the Micropython Menu WEB Server see:
Raspberry Pi Pico - Micropython Menu, RGB LED & Servo WEB Servers
https://m.youtube.com/watch?v=Iqn3MU_FaiM
- Attachments
-
- Micropython Scripts in the cwd
- root_dir.png (180.44 KiB) Viewed 2545 times
-
- How to import
- how_to_load_and_run_a_script.png (185.83 KiB) Viewed 2545 times
-
- How to choose
- how_to_choose.png (217.29 KiB) Viewed 2545 times
Re: Change the main program runtime
FWIW, instead of using exec, you can also just use import directly via builtins.__import__. e.g. to import the sys module:
Code: Select all
import builtins
sys = builtins.__import__('sys')
Re: Change the main program runtime
Thanks jmmo for your correct suggestion