开发者

In Python, how do I refer to an identifier by its absolute fully-qualified name?

开发者 https://www.devze.com 2022-12-13 09:56 出处:网络
I have a project with a directory structure that looks like: /foo/baz/__init__.py /bar/foo.py /bar/splat.py

I have a project with a directory structure that looks like:

/foo/baz/__init__.py
/bar/foo.py
/bar/splat.py

Problem is, /bar/splat.py refers to the foo.baz module. This fails with the error No module named baz because it's trying to search for this module within /bar/foo.py. I don't want Python to search the bar module, I want to tell it to search the root foo module for baz. How do I do that? In Ruby you'd just prefix the identifier with :: (In this case, ::Foo::Baz), is there a Python eq开发者_StackOverflowuivalent to this?


In Python 2.5 and 2.6,

from __future__ import absolute_import

should change Python's import behavior to do what you want (if the very root, /, is on sys.path of course;-). This becomes the normal Python behavior in 2.7 (not released yet, but an early alpha is already tagged, if you're curious).

0

精彩评论

暂无评论...
验证码 换一张
取 消