python中每个模块都有一个名称_与现有模块同名的python模块名称

1586010002-jmsa.png

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值