Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typical Naming Conventions for Python Directories in Packages

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?

like image 556
Adam Lewis Avatar asked May 10 '11 19:05

Adam Lewis


1 Answers

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.

like image 132
zeekay Avatar answered Nov 08 '22 04:11

zeekay