简体   繁体   中英

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).

2) Change __init__() inside Package1 as follows:

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

Then from your python file you can access like

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

I think this will work.... have fun

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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