importing own module

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Kenneth
Posts: 11
Joined: Wed May 07, 2014 10:29 am

importing own module

Post by Kenneth » Fri May 16, 2014 6:23 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: importing own module

Post by dhylands » Fri May 16, 2014 7:35 pm

So I was able to do imports in one of 2 ways:

1 - I created mod1.py in the root called mod1.py:

Code: Select all

def mod1():
    print('mod1')
2 - I created an __init__.py in a directory called mod2:

Code: Select all

def mod2():
    print('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.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: importing own module

Post by pfalcon » Fri May 16, 2014 8:10 pm

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.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Kenneth
Posts: 11
Joined: Wed May 07, 2014 10:29 am

Re: importing own module

Post by Kenneth » Fri May 16, 2014 8:24 pm

I can live with the requirement to extend the path with '1:/' :). Works great.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: importing own module

Post by torwag » Sun May 18, 2014 9:16 am

@ 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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: importing own module

Post by dhylands » Sun May 18, 2014 7:10 pm

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

Post Reply