Python cannot import name if placed in module [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to do relative imports in Python?
I'm experiencing something that seems very random to me.
I have a folder structure much like this:
dir A
__init__.py is empty
a.py imports stuff and b.py
dir B
__init__.py is empty
b.py imports NOTHING
a.py raises an error (cannot import name b). This only happens while b is part of module B.
If I move it outside the directory, the import error does NOT occur.
Any help would be appreciated. I must be overlooking something.

Did you try the relative import
from ..B import b
?
EDIT: This does not apply if it doesn't matter where package B lives.
But you don't describe what exactly you do. As you may know or not, there are several import forms:
import module
import package # imports package.__init__ under the name package
import package.module
from package import module
import package
from module import component
from package.module import component
As you only write
a.py imports stuff and b.py
I don't know what exactly happens: if you try to
import b
that fails because b lives in the package B. So you need one of
from B import b
import B.b
Your comment above mentions a name clash. Which of two equally named packages and modules have priority depends on in which directory you are: '.' is normally at the very start of sys.path, so if you are directly under your utils directory you might have a different experience than otherwise.

Related

Python not able to find my module even it exists

My folder structure is
fold1/fold2/a.py
fold1/fold3/b.py
fold1/fold3/c.py
a.py is doing
from fold1.fold3 import c
c.py is doing
import b
When I run a.py, I am getting error "No module named b"
I have __init__.py in all folders, but still why I am getting that error.
I know that lastly python checks for packages in current directory, so here current directory is "fold2" and I am not finding "b.py" in that fold2? but both "c.py" and "b.py" are in same directories right but still why I am getting that error?
Edit: The code is generated by pyxb python module, so I cannot change import statements.
import b is an absolute import: b has to be found in a package listed in sys.path.
In order to import b relative to the package containing c, use the relative import
from . import b
. refers to the package containing c (fold3). .. would refer to the package containing that package (fold1). You could also write
from ..fold3 import b
.. refers to fold1, so ..fold3 refers to the module fold3 in the package fold1.
When using relative imports, it doesn't matter where fold1 exists: it could be in sys.path, it could be a submodule of some other package. As long as the internal structure of fold1 remains the same, the relative imports will continue to work the same.
All the paths are relative to where you start the interpreter from!
c.py is looking for b in the parent of fold1
try using a form like from fold1.fold3 import b instead

Python Imports Not Working with Directories

I am having trouble importing python modules in a different folder than the one I am using. I know there are a number of posts about this on StackOverflow, but I am still running into a few issues with importing. Here is my general file structure:
Folder
__init__.py
A.py
folder
__init__.py
B.py
tests
__init__.py
C.ipynb
I am trying to import B from C, but doing so gives me a number of errors. If I write
from folder import B
I get
ModuleNotFoundError: No module named 'folder'.
If I write
from . import B
I get
ImportError: attempted relative import with no known parent package.
If I write
from . import B
or
from .. import B
I get
ImportError: attempted relative import with no known parent package.
Additionally, if I try to import B from A, I also get errors. Running
import folder.B
or
from folder import B
gives
ModuleNotFoundError: No module named 'folder'.
Could someone help me import these files correctly? I know that modifying sys.path is an option (although I am not entirely sure how to implement this), but I feel like there must be a way to do this without modifying sys.path.
Thank you, and I apologize if this has been answered already!

Attempted relative import with no known parent package [duplicate]

This question already has answers here:
Relative imports in Python 3
(31 answers)
Closed 4 months ago.
from ..box_utils import decode, nms
This line is giving error
ImportError: attempted relative import with no known parent package
What is this error and how to resolve this error?
Apparently, box_utils.py isn't part of a package. You still can import functions defined in this file, but only if the python script that tries to import these functions lives in the same directory as box_utils.py, see this answer.
Nota bene: In my case, I stumbled upon this error with an import statement with one period, like this:
from .foo import foo. This syntax, however, tells Python that foo.py is part of a package, which wasn't the case. The error disappeared when I removed the period.
If a different dictionary contains script.py, it can be accessed from the root. For instance:
If your program is structured...:
/alpha
/beta
/delta
/gamma
/epsilon
script.py
/zeta
...then a script in the epsilon directory can be called by:
from alpha.gamma.epsilon import script
in the latest python version, import it, directly don't use .. and .library
import the file which you want. this technique will work in the child directory.
If you import it from parent directory, then place the directory's full path.
package
|--__init__.py
|--foo.py
|--bar.py
Content of bar.py
from .foo import func
...
If someone is getting the exactly same error for from .foo import func.
It's because you've forgot to make it a package. So you just need to create __init__.py inside package directory.

How to Correctly Import Python Modules and Avoid Import Errors

I have a custom python module I'm working on and am confused how I should import modules into other modules. I want to use bits and pieces of some modules within others and keep getting an error: ImportError: cannot import name NameOfModule
I'm assuming there's some sort of circular reference that's causing the issue but I'm not sure if I need to add something to __init__.py, or if there's a specific way of importing the modules into each other, or if I should change my folder structure?
If I want to be able to use some function from mod1.py within mod2.py how should I go about setting up the import statements?
My current folder structure is:
FolderName
-__init__.py
-mod1.py
-mod2.py
-mod3.py
-mod4.py
Sample code:
__init__.py is empty
mod1.py: from . import mod2
mod2.py: from . import mod1
You should be using relative imports for files within the current module, like so:
from . import mod2
Or:
from .mod2 import foo
And unless you have a VERY good reason, you should be using Python 3.

python: how to call a constructor in a module in a package in another package directly [duplicate]

This question already has an answer here:
A Python module and package loading confusion
(1 answer)
Closed 9 years ago.
I am porting some matlab code to python. I need to work with packages and modules in this case. The relevant package directory structure looks like this:
toppackage
__init__.py
subpackage
__init__.py
module.py
...
In the script I use the package, I can work like this:
from toppackage.subpackage.module import SomeClass
s = SomeClass()
But I would prefer working like this:
import toppackage %somewhere at the beginning of file
s = toppackage.subpackage.module.SomeClass()
I see this is done in numpy. But I could not find it in documentation. How can I do that?
Thanks in advance.
You need to import contained packages in the __init__.py files.
You can import the packages inside the toppackage/__init__.py for example:
import toppackage.subpackage.module
or you can import just each directly contained package, so in toppackage/__init__.py:
from . import subpackage
and in toppackage/subpackage/__init__.py:
from . import module
Just importing the top-level package does not automatically make the contained packages available. You need to explicitly import the full path once, somewhere, before that works.
The numpy package imports the nested packages in the top-level __init__.py.
How this stuff works depends critically on what is in __init__.py. Whatever gets imported in that script becomes part of the package namespace.
For example, if your toppackage/__init__.py is empty, to gain access to subpackage, you'd need to do:
import toppackage
try:
p = toppackage.subpackage
except AttributeError:
print "you would see this"
import toppackage.subpackage
p = toppackage.subpackage #no error now
however, if toppackage/__init__.py included the line:
#toppackage/__init__.py
import subpackage
Then the above script would raise no Exceptions.
As noted in the comments, you can also use relative imports since you are in a package:
from . import subpackage
This avoids "namespace" conflicts -- e.g. if you have a subpackage named os it will get your subpackage rather than the python-level package.

Categories