Importing a custom class

General discussion about topics related to OpenMV.
mjs513
Posts: 74
Joined: Sun Apr 30, 2017 12:52 pm

Importing a custom class

Postby mjs513 » Wed May 10, 2017 3:10 pm

Ok. I am working on a class for reading a vl53l0x sensor. I got the class in the same folder as the test sketch and using from vl53l0x import * but I keep getting an error about no module with that name. The file name is vl53l0x.py. So the question is how do I do this?

Thanks
Mike
User avatar
kwagyeman
Posts: 2286
Joined: Sun May 24, 2015 2:10 pm

Re: Importing a custom class

Postby kwagyeman » Wed May 10, 2017 3:34 pm

http://wiki.micropython.org/Importing-Modules

Since the internal flash is disabled when the SD card is attached you need to have two copies of the script for each situation.
Nyamekye,
mjs513
Posts: 74
Joined: Sun Apr 30, 2017 12:52 pm

Re: Importing a custom class

Postby mjs513 » Wed May 10, 2017 4:20 pm

Thanks. That worked. I also did a sys.path but it returned a empty set. Not sure what that means.

Mike
User avatar
falko
Posts: 7
Joined: Mon Dec 04, 2017 9:23 am
Location: Germany
Contact:

Re: Importing a custom class

Postby falko » Mon Jan 22, 2018 12:53 pm

Unfortunately, the link is not working (502 Bad Gateway).

Can you describe some sort of best practise when working with OpenMV IDE and custom modules?
As far as I know, I need to copy them to the camera board by hand. This is rather tedious during development...
User avatar
kwagyeman
Posts: 2286
Joined: Sun May 24, 2015 2:10 pm

Re: Importing a custom class

Postby kwagyeman » Mon Jan 22, 2018 1:01 pm

Hi, we will be releasing a library feature for OpenMV using OpenMV IDE in the next version.

So, in your home directory will be a library folder (think Arduino). We may also scan a MP library directory too for general purpose MP code.

Anyway, when you click run for your script OpenMV IDE will regex any import statements, and if it sees a module not built-in it will look on the file system for it. If it doesn't see it it will look in the library dirs first for the module and if it finds it copy it to the required location. It will fallback to a shipped system library directory otherwise and copy those instead. Note that if it sees a change between the library files on the PC and in your code it will update your system.

While this sounds heavy it only has to do this for any custom libraries you use.

We'll be shipping a MQTT library with the next OpenMV IDE release.
Nyamekye,
User avatar
kwagyeman
Posts: 2286
Joined: Sun May 24, 2015 2:10 pm

Re: Importing a custom class

Postby kwagyeman » Fri Feb 09, 2018 1:24 am

Hi, library management is now working in OpenMV IDE. It will automatically scan a user folder now along with the examples folder for imports in your code and copy/update modules as necessary.
Nyamekye,
User avatar
kwagyeman
Posts: 2286
Joined: Sun May 24, 2015 2:10 pm

Re: Importing a custom class

Postby kwagyeman » Sat Feb 17, 2018 1:59 am

Nyamekye,
User avatar
aakoch
Posts: 24
Joined: Wed Dec 06, 2017 11:07 pm

Re: Importing a custom class

Postby aakoch » Sat Apr 07, 2018 3:29 pm

I'm having troubles using the import feature. I can import but ONLY if I specify exactly what I need. For example, if I create a constants.py file with a variable, I can only access it if I import that variable.

constants.py:

Code: Select all

AREA_THRESHOLD = 20
Then, from another file, I try this, but it doesn't work:

Code: Select all

import constants
print(AREA_THRESHOLD)
But this works:

Code: Select all

from constants import AREA_THRESHOLD
print(AREA_THRESHOLD)
Is this a bug? I'm doing this on a Mac. Or am I just not doing it right?
User avatar
aakoch
Posts: 24
Joined: Wed Dec 06, 2017 11:07 pm

Re: Importing a custom class

Postby aakoch » Sat Apr 07, 2018 3:33 pm

This works, so I don't think this is a big issue anymore:

Code: Select all

from constants import *
User avatar
kwagyeman
Posts: 2286
Joined: Sun May 24, 2015 2:10 pm

Re: Importing a custom class

Postby kwagyeman » Sat Apr 07, 2018 3:50 pm

Importing in python is not like C. Imports are under a module name space which is the file name.

So, you could do something like:

Code: Select all

import constants
print(constants.AREA_THRESHOLD)
Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 5 guests