Page 1 of 1
importing own module
Posted: Fri May 16, 2014 6:23 pm
by Kenneth
I have created a folder on the sdcard containing an empty __init__.py and another file containing a class. However, I can't 'import <foldername>', nor 'from <foldername>.<filename> import <classname>'. Not from the REPL, not from main.py
Code: Select all
Traceback (most recent call last):
File "1:/main.py", line 2, in <module>
ImportError: No module named 'mymodule'
What am I doing wrong?
Re: importing own module
Posted: Fri May 16, 2014 7:35 pm
by dhylands
So I was able to do imports in one of 2 ways:
1 - I created mod1.py in the root called mod1.py:
2 - I created an __init__.py in a directory called mod2:
Code: Select all
>>> import mod1
>>> mod1.mod1()
mod1
>>> import mod2
>>> mod2.mod2()
mod2
So from my host, the internal flash looked like:
Code: Select all
807 >ls -lR /media/dhylands/4421-0000/
/media/dhylands/4421-0000/:
total 6
-rw-r--r-- 1 dhylands dhylands 279 Jan 1 00:00 boot.py
-rw-r--r-- 1 dhylands dhylands 33 Jan 1 00:00 main.py
-rw-r--r-- 1 dhylands dhylands 30 May 16 11:46 mod1.py
drwx------ 2 dhylands dhylands 512 May 16 11:46 mod2/
-rw-r--r-- 1 dhylands dhylands 2436 Jan 1 00:00 pybcdc.inf
-rw-r--r-- 1 dhylands dhylands 529 Jan 1 00:00 README.txt
/media/dhylands/4421-0000/mod2:
total 1
-rw-r--r-- 1 dhylands dhylands 30 May 16 11:46 __init__.py
So that all works for the internal flash. For the sdcard, you'll need to modify sys.path.
Code: Select all
>>> import sys
>>> sys.path
['0:/', '0:/lib']
So it only looks on the internal flash for modules to import. I coped mod3.py and mod/__init__.py to my sdcard and then was able to import them by doing:
Code: Select all
>>> import sys
>>> sys.path.append('1:/')
>>> import mod3
>>> mod3.mod3()
mod3
>>> import mod4
>>> mod4.mod4()
mod4
Sorry about the long answer. I didn't know the answer before I started typing my reply, and I figured the rest of the stuff would be useful for others.
Re: importing own module
Posted: Fri May 16, 2014 8:10 pm
by pfalcon
I can provide short summary: MicroPython uses sys.path to process imports, just like Python does.
MicroPython also doesn't currently support concept of "current directory", which is highly unlike Python (Python imports a module from a current directory as a first choice, period). The reason why MicroPython doesn't support concept of a current directory is because it was decided that it will be hard for users to track the fact that a board has its own current dir, just the same as their main working system has. So, feel free to submit a bug report at
https://github.com/micropython/micropython/issues/new if you find current behavior (of not trying to confuse user) to be actually more confusing, than if the board just behaved the same way as the host system.
Re: importing own module
Posted: Fri May 16, 2014 8:24 pm
by Kenneth
I can live with the requirement to extend the path with '1:/'

. Works great.
Re: importing own module
Posted: Sun May 18, 2014 9:16 am
by torwag
@ dhylands:
Thanks for the very nice how-to. Would you mind to add this to the wiki? As pfalcon mentioned it is somehow the standard way but I guess many people never touched the sys.path since all modules installed via some sort of package manager are "automagically" ready to use.
Would be good to tell people that the path is not as populated and complete as they would expect it from a PC.
Hmm, would it make sens to add something like "1:/modules" by default? Or would this be to much of a cut into users freedom?
Re: importing own module
Posted: Sun May 18, 2014 7:10 pm
by dhylands
torwag wrote:@ dhylands:
Thanks for the very nice how-to. Would you mind to add this to the wiki?
Done. I created
http://wiki.micropython.org/Importing-Modules and put a link to it on
http://wiki.micropython.org/Home