Posts: 22
Joined: Mon Jun 23, 2014 7:04 pm

Importing modules in modules

Wed Nov 20, 2019 6:27 am

A customer of mine has gotten me onto Sparkfun's Qwiic series of I2C toys. They have a neat little relay board that works via I2C so it's easier to hook to the Pi than one of those meant for Arduino boards you see all over Amazon.

Trick is, Sparkfun has focused on the Arduino for the Qwiic system, they have a few Raspberry Pi GPIO adapters but they've put out very little code for the Pi, no python modules etc. I figured I'd write a little module for the relay board. I've never written a Python module before, seems straightforward (Just a bunch of functions that glue a human readable nametag to the proper hex bytes) but I have a quick and possibly silly question:

Is it considered poor practice to import a module, in a module? LIke, if the first line of my module qwiicrelay.py is import smbus am I breaking any rules?

Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Importing modules in modules

Wed Nov 20, 2019 9:51 am

This is pretty standard. Go ahead.

Look at any of the current python code for using I2C devices and they will do exactly the same thing.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Importing modules in modules

Wed Nov 20, 2019 10:57 am

In agreement with @scotty101, citing this in support -
Aside from some naming restrictions, nothing special is required for a Python file to be a module, but you need to understand the import mechanism in order to use this concept properly and avoid some issues.

Concretely, the import modu statement will look for the proper file, which is modu.py in the same directory as the caller if it exists. If it is not found, the Python interpreter will search for modu.py in the “path” recursively and raise an ImportError exception if it is not found.

Once modu.py is found, the Python interpreter will execute the module in an isolated scope. Any top-level statement in modu.py will be executed, including other imports if any. Function and class definitions are stored in the module’s dictionary.

Then, the module’s variables, functions, and classes will be available to the caller through the module’s namespace, a central concept in programming that is particularly helpful and powerful in Python.
Ref: https://docs.python-guide.org/writing/s ... e/#modules

{I don't know how "official" that python-guide site is... }

Posts: 22
Joined: Mon Jun 23, 2014 7:04 pm

Re: Importing modules in modules

Wed Nov 20, 2019 7:02 pm

Thanks folks. I am aware that python import looks through the directory containing the invoking script then $PATH, that's fairly straightforward.

Looking back at the code, they did it a little strange. The board has an ATTINY84 for a brain, and they wrote it in such a way that it only has commands for toggling the relays. There are discrete functions to turn ALL relays on or off, but none to turn just one on or off, just to toggle them. So if you want to definitely turn one ON, you have to first request its status, and then do the whole "if it's off, toggle; elif it's already on, don't toggle" thing. I don't know why they did it that way, but there it is. Whee.

Return to “Python”