Python relative imports within a package not on th

2019-02-18 06:14发布

How can I import a file that is in a parent directory within a python package (that is not on the path) into a file in a child dir?

I'm not totally clear on the vocabulary of python packaging so by way of example:

dir1/
    __init__.py
    runner.py
    in_dir1.py
    dir2/
        __init__.py
        in_dir2.py

dir1/in_dir1.py:

def example():
    print "Hello from dir1/in_dir1.example()"

dir1/dir2/in_dir2.py

import in_dir1   #or whatever this should be to make this work
print "Inside in_dir2.py, calling in_dir1.example()"
print in_dir1.example()

Given that dir1 is not on the python path I'm looking for the best way to import in_dir1 into in_dir2.

I tried from .. import in_dir1 and from ..dir1 import in_dir1 based on this Q/A but neither works. What is the correct way of executing that intent? This Q/A seems to contain the answer; however, I'm not quite sure what to make of it / how to actually solve my problem using PEP 366

Both __init__.py files are empty and I am on v2.6.

I'm attempting to do this without using any of the path hacks that Google keeps turning up.

2条回答
劫难
2楼-- · 2019-02-18 07:01

You can actually do this:

import sys
sys.path.append('..')

and that will work. But don't do that. It could break other modules.

I guess you could remove it directly after the import, but don't.

EDIT:

Actually, this also works and I think there's no reason it's not safe:

inside in_dir2.py you can do:

import sys
import os
current_module = sys.modules[__name__]
indir2file=current_module.__file__
sys.path.append(os.path.dirname(os.path.abspath(indir2file))+os.sep+".."+os.sep)
import in_dir1

Try hard to restructure your code instead.

查看更多
【Aperson】
3楼-- · 2019-02-18 07:02

The answer is in the link you gave:

Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to 'main') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.

You cannot do relative imports in __main__ scripts (i.e. if you directly run python in_dir2.py).

To solve this, what PEP 366 allows you to do is set the global __package__:

import dir1
if __name__ == '__main__':
    __package__ = 'dir1.dir2'
    from .. import in_dir1

Note that the package dir1 still has to be on sys.path! You can manipulate sys.path to achieve this. But by then, what have you achieved over absolute imports?

查看更多
登录 后发表回答