Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python module names with same name as existing modules

Tags:

python

module

I have a number of small utils modules that i organize under a 'msa' namespace so I can use in a number of different research projects. Currently I have them organized like this:

# folder structure:
packages  <-- in my pythonpath
--msa
----msa_utils.py
----msa_geom.py
----msa_pyglet.py
----msa_math.py
----etc

# imported and used this like
from msa import msa_pyglet
from msa import msa_math
msa_pyglet.draw_rect(msa_math.lerp(...))

However I would like to avoid the 'msa_' in the names and use like this:

# folder structure:
packages  <-- in my pythonpath
--msa
----utils.py
----geom.py
----pyglet.py
----math.py
----etc

# imported and used this like
import msa.pyglet
import msa.math
msa.pyglet.draw_rect(msa.math.lerp(...))

This shouldn't cause name conflicts when importing from outside, however there are name conflicts when the modules themselves import modules with conflicting names. E.g. msa/pyglet needs to imports pyglet (the external one), but ends up trying to import itself. Likewise any module which tries to import the standard math library imports only my math module. Which is all understandable. But what is the usual pythonic way of dealing with this? Do I have to give each module file a globally unique name?

like image 762
memo Avatar asked Aug 05 '16 16:08

memo


1 Answers

In Python 2, imports in packages without a package qualifier indeed first look for package local modules.

Thus, import pyglet will find msa.pyglet before the top-level pyglet is considered.

Switch to absolute imports to make the Python 3 behaviour the default, where unqualified names are always top level names:

from __future__ import absolute_import

Now import pyglet can only ever find the top-level name, never msa.pyglet. To reference other modules within your msa namespace, use from . import pyglet or from msa import pyglet.

See PEP 328 -- Imports: Multi-Line and Absolute/Relative for more details.

like image 70
Martijn Pieters Avatar answered Oct 08 '22 11:10

Martijn Pieters