3中的相对导入问题

3中的相对导入问题

本文介绍了Python 3中的相对导入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python导入使我发疯(我对python导入的经验有时与惯于胜于隐式"成语完全不符:():

Python imports drive me crazy (my experience with python imports sometime doesn't correspond at all to idiom 'Explicit is better than implicit' :( ):

[app]
    start.py
        from package1 import module1
    [package1]
        __init__.py
            print('Init package1')
        module1.py
            print('Init package1.module1')
            from . import module2
        module2.py
            print('Init package1.module2')
            import sys, pprint
            pprint.pprint(sys.modules)
            from . import module1

我得到:

vic@ubuntu:~/Desktop/app2$ python3 start.py
Init package1
Init package1.module1
Init package1.module2
{'__main__': <module '__main__' from 'start.py'>,
 ...
 'package1': <module 'package1' from '/home/vic/Desktop/app2/package1/__init__.py'>,
 'package1.module1': <module 'package1.module1' from '/home/vic/Desktop/app2/package1/module1.py'>,
 'package1.module2': <module 'package1.module2' from '/home/vic/Desktop/app2/package1/module2.py'>,
 ...
Traceback (most recent call last):
  File "start.py", line 3, in <module>
    from package1 import module1
  File "/home/vic/Desktop/app2/package1/module1.py", line 3, in <module>
    from . import module2
  File "/home/vic/Desktop/app2/package1/module2.py", line 5, in <module>
    from . import module1
ImportError: cannot import name module1
vic@ubuntu:~/Desktop/app2$

import package1.module1可以工作,但是我想使用from . import module1,因为我想将package1移植到我的其他应用程序中,这就是为什么我要使用相对路径.

import package1.module1 works, but i want to use from . import module1 because i want to make package1 portable for my other applications, that's why i want to use relative paths.

我正在使用python 3.

I am using python 3.

我需要循环进口. module1中的一个函数断言其参数之一是module2中定义的类的实例,反之亦然.

I need circular imports. A function in module1 asserts that one of its parameter is instance of a class defined in module2 and viceversa.

换句话说:

sys.modules包含'package1.module1': <module 'package1.module1' from '/home/vic/Desktop/app2/package1/module1.py'>.我想以from . import module1形式获得对它的引用,但是它试图获取名称,而不是像情况下import package1.module1这样的程序包(可以正常工作).我尝试了import .module1 as m1-但这是语法错误.

sys.modules contains 'package1.module1': <module 'package1.module1' from '/home/vic/Desktop/app2/package1/module1.py'>. I want to get a reference to it in form from . import module1, but it tries to get a name, not a package like in case import package1.module1 (which works fine). I tried import .module1 as m1 - but that's a syntax error.

此外,module1中的from . import module2正常工作,但module2中的from . import module1无效...

Also, from . import module2 in module1 works fine, but from . import module1 in module2 doesn't work...

更新:

此技巧有效(但我正在寻找官方"方式):

This hack works (but i am looking for the 'official' way):

print('Init package1.module2')

import sys, pprint
pprint.pprint(sys.modules)

#from . import module1
parent_module_name = __name__.rpartition('.')[0]
module1 = sys.modules[parent_module_name + '.module1']

推荐答案

一个更好的解决方案是将package1放在它自己的单独包中.当然,它不能导入package2,但是如果它可重用,又为什么呢?

A better solution for your problem is to put package1 in it's own separate package. Of course then it can't import package2, but then again if it is reusable, why would it?

这篇关于Python 3中的相对导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:02