Import custon module into script

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
christian.pinto
Posts: 1
Joined: Fri Nov 14, 2014 12:13 pm

Import custon module into script

Post by christian.pinto » Fri Nov 14, 2014 1:41 pm

Hi all,

I am trying to define a few custom python modules to be used in my scripts. My custom module works pretty well, except for the fact that i can not import in into a script.
My idea was to do something like:

Code: Select all

import my_module
While True:
  module_obj.func()

Bu then i get the following error: No module named 'my_module'.

While the custom module works if i do: my_module.module_obj.func().

To create the custom module I took the pyb module as a reference.

Is there something specific to do to allow the module to be imported into a script??


Thanks in advance,

Christian

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

Re: Import custon module into script

Post by dhylands » Fri Nov 14, 2014 10:20 pm

If you just do:

Code: Select all

import my_module
then you'll need to prefix everything from my_module (so my_module.module_obj)

However, if you do:

Code: Select all

from my_module import module_obj
then you can use module_obj without a prefix.

This is the same between python and micropython.

Post Reply