ImportError "Cannot import name Package1" - python

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

Related

Correct to import module class python

Python project structure:
src/
- package-name/
-- A/
---B/
b1.py
---C/
c1.py
In c1.py, it uses a function defined in b1.py. I try 2 ways:
Method1: from src.package-name.A.B.b1 import b1_func
Method2: from ..B.b1 import b1_func
The import module starts from package-name directory, so src/ will not be visible in the imported module. So Method1 not working when import my own module.
Method2 is not working when run in IDE. ValueError: attempted relative import beyond top-level package
Any suggestions? thanks.
Do you have __init__.py files in A and B? It may be worthwhile to properly import b1_func into B's and then A's init files.
B __init__.py
from .b1_file import b1_func
or whatever
and A __init__.py
from B import b1_func
Then you should be able to import ..b1_func
I change the "Content Root" to the package-name directory in PyCharm and import package-name.B.b1. It works.
follow these steps to import the packages wherever u want
First of all, add __init__.py in all folders
i.e: __init__.py in src and __init__.py in package and __init__.py in A
and __init__.py in B and __init__.py in C.
If u want to import the functions from b1.py in c1.py add these lines in c1.py file.
import sys
sys.path.append(“../”)
#if u want from src folder add ../../
from B.b1 import YourFunctionName

Python3 import modules from folder to another folder

my structure dictionary is
mainFolder
folder1
__init__.py
file1.py
file2.py
folder2
__init__.py
file3.py
file4.py
setup.py
__init__.py
i need import file4.py from folder2 to folder1/file1.py
file1.py:
from ..folder2.file4 import MyClass
and i gets:
SystemError: Parent module '' not loaded, cannot perform relative
import
how to fix that ?
This is because you must to explicitly name the parent package.
So in your case you need either from mainFolder.folder2.file4 import Myclass, either from folder2.file4 import Myclass

python import module from parent package

I have the following directory structure
foo/
__init__.py
settings.py
bar/
__init__.py
myfile.py
In myfile.py I have:
import settings
I get the following error: ImportError: No module named settings, why? How can I efectively import the settings file from myfile.py
From http://docs.python.org/2/tutorial/modules.html#intra-package-references :
from .. import settings
Hope it helps
Here is another method that seems more clear:
In foo.__init__.py:
__all__ = ['settings', ..(all other modules at 'foo' level you want to show)...]
In myfile.py:
# instead of "from .. import..."
from foo import settings
print settings.theThing

python import within directory

I have a python project, I divided it into two parts, a library part and a
binary part.
For organization reasons, I wanted to have a hierarchy like this:
project/
lib/
__init__.py
module1.py
module2.py
bin/
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
The problem is, of course, the simple import in binary{1,2}.py doesn't work, at
least in the coding phase (before installation).
Any suggestions? How do you test your code when you're coding?
I use absolute imports everywhere.
project/
__init__.py
lib/
__init__.py
module1.py
module2.py
bin/
__init__.py
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
Setting PYTHONPATH to the directory above project, you can import module1 from binary1.py like this:
from project.lib import module1
The __init__.py files can make importing simpler, let's say in each file you have a class. Usually you would have to import the class like this (inclduding filename):
from project.lib.module1 import Module1
But if you edit lib/__init__.py to contain this line:
from project.lib.module1 import Module1
You can use the namespace of lib to import Module1 directly from lib:
from project.lib import Module1

Import parent module package

The directory structure:
[app]
start.py
import package1
[package1]
__init__.py
print('Init package1')
import module1
import subpackage1
module1.py
print('package1.module1')
import package1 # this works OK
[subpackage1]
__init__.py
print('Init package1.subpackage1')
import module1
module1.py
print('Init package1.subpackage1.module1')
#from package1 import subpackage1 # ImportError: cannot import name subpackage1
#from .. import subpackage1 # ImportError: cannot import name subpackage1
#import . as subpackage1 # SyntaxError: invalid syntax
import package1.subpackage1 as subpackage1 # AttributeError: 'module' object has no attribute 'subpackage1'
To avoid problems caused by circular imports in subpackage1.module1 i want to import module subpackage1 in order to refer to other modules from subpackage1 in form subpackage.module2. Because if i do from . import module2 the reference to module2 could not yet exist in subpackage1 when i try to this import.
I have tried 4 different approaches - none of them worked - see the comments in the code.
Any help?
Some time ago subpackage1 was top level package and it worked (see how this works in the source of package1.module1. Now, when i moved it one level down - i have this problem... I know that i can add package1 dir to sys.path, but that's ugly.
I used this hack, which worked for me:
#import package1.subpackage1 as subpackage1 # AttributeError: 'module' object has no attribute 'subpackage1'
subpackage1 = sys.modules[__name__.rpartition('.')[0]] # parent module
Or you can try this:
from package1 import subpackage1
which works in some cases: https://stackoverflow.com/a/24968941/248296
I'm not not exactly sure what you are trying to do, but your example might be a lot easier to understand if you used absolute imports and avoided putting code in __init__ modules.
Try something like this:
[app]
start.py
print('Start')
from package1 import module1
[package1]
__init__.py
print('Init: package1')
module1.py
print('Load: package1.module1')
from package1.subpackage1 import module1
[subpackage1]
__init__.py
print('Init: package1.subpackage1')
module1.py
print('Load: package1.subpackage1.module1')
from package1 import subpackage1
After running start.py, you should get output like this:
Start
Init: package1
Load: package1.module1
Init: package1.subpackage1
Load: package1.subpackage1.module1

Categories