简体   繁体   English

ImportError“无法导入名称Package1”

[英]ImportError “Cannot import name Package1”

My directory structure is: 我的目录结构是:

[File1]
    [Package1]
        __init__.py
            from Package1 import module1
            from Package1 import module2
            ...
        module1.py
        module2.py
        ...

I want to import a package so that using a class like 我想导入一个包,以便使用类似的类

from File1 import Package1 
Package1.Module1.Class1()…  

is possible. 是可能的。 When I try 当我尝试

from File1 import Package1 

I always get the error: 我总是得到错误:

cannot import name Package1

I think that Circular imports may be the problem, but I don't know how to fix it. 我认为通函进口可能是个问题,但我不知道如何解决。

1) You need to add __init__() in File1 folder also (empty also ok). 1)您还需要在File1文件夹中添加__init__() (也可以为空)。

2) Change __init__() inside Package1 as follows: 2)如下更改Package1 __init__()

__init__.py
from File1.Package1 import module1
from File1.Package1 import module2

Then from your python file you can access like 然后从您的python文件中,您可以像访问

from File1 import Package1
x=Package1.module1()
x=Package1.module2()

I think this will work.... have fun 我认为这会起作用....玩得开心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM