The Question
I would like to know if there is a standard convention for the naming of Python directories that plan to be imported as a module. Meaning the directory contains a blank __init__.py
Background
Up until recently I have given it little thought and named solely based on what made sense at the file system level. What got me in trouble is that what made sense at the file system level also made sense for other developers' standalone modules. Consider the following directory:
+ drivers
+ prologix
- __init__.py
- driver_a.py
- driver_b.py
+ visa
- __init__.py
- driver_a.py
- driver_b.py
__init__.py
ringout.py <-- simple file to ring-out the drivers
While this worked fine when ringing out the prologix's drivers, I ran into an issue when trying to import my visa drivers as well as pyVisa's 'visa' module. It was very easy to diagnose the problem, but the fix to rename my visa driver's folder to 'visa_dir' makes the code more difficult to read (IMO).
import drivers.visa
vs
import drivers.visa_dir
Is there a better way to handle this?
Each module's namespace is unique, so even if you have two modules named visa
as long as you avoid importing them into the same namespace with the same name you won't have any problems. I tend to prefer absolute imports:
import drivers.visa
import pyVisa.visa
Or you could also use as
:
from drivers import visa
from pyVisa import visa as pyvisa
...etc. Just be careful how you import things. I'd prefer (as an end-user) that you structure your modules logically within your package and not worry about pre-mangling them for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With