如何避免 Python 模块系统中的命名冲突?
在我的 Django 项目中,我有一个名为 profile
的应用程序,其中主要包含我的 profile.models.UserProfile
类,以获取有关 User
对象的其他信息(可能对于 Django 的人来说似乎很熟悉)。现在我已经将一些初始化代码放入 profile/__init__.py
(一些信号)中,但遇到了一个问题:Django 告诉我名为 hotshot_profile
的表尚未被成立。
经过几个小时的搜索,我将问题追溯到导入订单。运行 python -v Manage.py test 我发现了以下内容:
import nose.plugins.prof # precompiled from /home/rassie/.virtualenvs/myproject/lib/python2.6/site-packages/nose/plugins/prof.pyc
import hotshot # directory /usr/lib64/python2.6/hotshot
import hotshot # precompiled from /usr/lib64/python2.6/hotshot/__init__.pyc
dlopen("/home/rassie/.virtualenvs/myproject/lib64/python2.6/lib-dynload/_hotshot.so", 2);
import _hotshot # dynamically loaded from /home/rassie/.virtualenvs/myproject/lib64/python2.6/lib-dynload/_hotshot.so
import hotshot.stats # from /usr/lib64/python2.6/hotshot/stats.py
import profile # directory /home/rassie/MyProject/apps/profile
import profile # precompiled from /home/rassie/MyProject/apps/profile/__init__.pyc
所以我的 Nose runner 导入了nose.plugins.prof(即使这个插件已关闭),导入hotshot
,尝试导入profile
。然而,profile
是从我的项目导入的,而它准备好的应该是从系统Python导入的。
显然,我自己的 profile
模块与系统 profile
模块发生冲突。显然,我无法从我自己的编程中排除与 Python 捆绑在一起的每个模块名称。所以问题是,我该去哪里?我是否必须为我的所有应用创建一个 myproject
命名空间? Django 可以使用它吗?
PS:表的名称 hotshot_profile
似乎来自与 pybb
中的 Profile
类的进一步尚未完全分析的命名冲突,我也在我的项目中使用它。但这超出了这个问题的范围。
In my Django project I have an app called profile
, which mostly contains my profile.models.UserProfile
class for additional information on User
objects (may seem familiar to Django folks). Now I've put some initialization code into profile/__init__.py
(some signals) and have ran into a problem: Django tells me that a table called hotshot_profile
has not been found.
After literally hours of searching, I traced the problem back to importing order. Running python -v manage.py test
I've found the following:
import nose.plugins.prof # precompiled from /home/rassie/.virtualenvs/myproject/lib/python2.6/site-packages/nose/plugins/prof.pyc
import hotshot # directory /usr/lib64/python2.6/hotshot
import hotshot # precompiled from /usr/lib64/python2.6/hotshot/__init__.pyc
dlopen("/home/rassie/.virtualenvs/myproject/lib64/python2.6/lib-dynload/_hotshot.so", 2);
import _hotshot # dynamically loaded from /home/rassie/.virtualenvs/myproject/lib64/python2.6/lib-dynload/_hotshot.so
import hotshot.stats # from /usr/lib64/python2.6/hotshot/stats.py
import profile # directory /home/rassie/MyProject/apps/profile
import profile # precompiled from /home/rassie/MyProject/apps/profile/__init__.pyc
So my Nose runner imports the nose.plugins.prof
(even though this plugin is off), imports hotshot
, which tries to import profile
. However, profile
gets imported from my project, while it ready should have been imported from system Python.
Obviously, my own profile
module clashes with system profile
module. I obviously can't exclude every module name that comes bundled with Python from my own programming. So the question is, where do I go from here? Do I have to create a myproject
namespace for all of my apps? Will Django work with that?
PS: Table's name hotshot_profile
seems to come from a further yet-to-be-fully-analyzed naming clash with a Profile
class from pybb
, which I'm also using in my project. But that's out of this question's scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不应该以
import mymodule
(相对导入)的形式导入您自己的模块。相反,您应该始终使用import myproject.mymodule
(绝对导入)。这避免了所有名称冲突。You should never import your own modules in the form
import mymodule
(relative imports). Instead you should always useimport myproject.mymodule
instead (absolute imports). This avoids all name clashes.