booting main.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
johnv
Posts: 25
Joined: Sun Nov 10, 2019 6:01 pm

booting main.py

Post by johnv » Mon Dec 23, 2019 2:00 pm

hi,

i want to run a script directly from booting the device,

when i make a file main.py it runs it straight from boot,

but when i make a folder main.py with my file as a subdirectory it doesnt,

can you run subdirectories in main.py or can you only run it as a single file?

MCHobby
Posts: 23
Joined: Mon Jan 26, 2015 2:05 pm
Contact:

Re: booting main.py

Post by MCHobby » Mon Dec 23, 2019 2:33 pm

You can place all your files in the root directory of the board file system.
You can also place your library into the "lib" subfolder.
A third option is to create a sub-directory say "mydir" and then, you can append the "mydir" entry into the sys.path list.
Place your starting code inside a file named "mymain.py" inside the "mydir" folder. From "main.py" you wil be able to write "import mymain.py" and your software will be imported from "mydir" (as available in the sys.path) and executed.

PS: I don't know if sys.path can be modified directly from boot.py but it should be possible.

Cheers,
Dominique

johnv
Posts: 25
Joined: Sun Nov 10, 2019 6:01 pm

Re: booting main.py

Post by johnv » Mon Dec 23, 2019 8:54 pm

this is the code from the main.py folder

Code: Select all

import sys
sys.path
sys.path.insert(0,'/mydir/mymain.py')
print (sys.path.insert)
from mydir import mymain
so i went for the third option,

i must say it took a while but i learned alot in my search :D

i created a folder named 'mydir'

and put 2 files in it: 'mymain.py' and '__init__.py'

the last one so it would recognize the 'mydir' folder as a module.

tnx dominique, youre awesome!!!

btw is this the best way to implement it? i just put some code togheter from different sites and tried to make something work :)

User avatar
pythoncoder
Posts: 4152
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: booting main.py

Post by pythoncoder » Tue Dec 24, 2019 7:27 am

You might also want to look up "Python packages" in any tutorial to learn more about using subdirectories to contain Python code.
Peter Hinch

johnv
Posts: 25
Joined: Sun Nov 10, 2019 6:01 pm

Re: booting main.py

Post by johnv » Tue Dec 24, 2019 7:56 am

i will look into it, tnx for the info! ;

i ordered this book, i hope it wil be any good :D Advanced Programming in MicroPython By Example by magda yuri

and a small bookbinding machine, so i can print out stuff from the internet and keep it tighty :lol:

hydraspl
Posts: 2
Joined: Sat Dec 07, 2019 8:50 pm

Re: booting main.py

Post by hydraspl » Tue Dec 24, 2019 2:15 pm

Can I fix this on my own or should I know the basics of programming?


The best way to choose the best lender service? Always compare the proposals of the different banks with each other! Only in this way can you find the cheapest deal in your state, with your level of income, and for the time period that feels comfortable for you. Many here stress the improtance of finding the best rate, but there are situations when you cannot really choose and honestly, many people looking for a payday loan are in no position to choose. Some of them have low income to show for themselves, others have bad credit history, we all have financial problems from time to time. My advice is, do look around, visit a couple of websites with the rates compared, but do not forget that it is but a short term loan. Take it out, use it, and focus on improving your budget planning to never need a loan again. Good luck!

Post Reply