Looked at all similar discussions, but I can't have clue.
I have the following structure
ava folder
main.py
utils folder
__init__.py
collect_env.py
distribution_env.py
gradcam_utils.py
logger.py
misc.py
module_hooks.py
precise_bn.py
setup_env.py
__init__.py has
from .collect_env import collect_env
from .distribution_env import build_ddp, build_dp, default_device
from .gradcam_utils import GradCAM
from .logger import get_root_logger
from .misc import get_random_string, get_shm_dir, get_thread_id
from .module_hooks import register_module_hooks
from .precise_bn import PreciseBNHook
from .setup_env import setup_multi_processes
__all__ = [
'get_root_logger', 'collect_env', 'get_random_string', 'get_thread_id',
'get_shm_dir', 'GradCAM', 'PreciseBNHook', 'register_module_hooks',
'setup_multi_processes', 'build_ddp', 'build_dp', 'default_device'
]
In main.py, tried to import
from ..utils import (collect_env, get_root_logger, register_module_hooks, setup_multi_processes)
Then the error is
ImportError: attempted relative import with no known parent package
Related
I have the following project structure:
stages
--Transform
----__init__.py
----Transformer.py
----PDFTransformer.py
core.py
I would like to import PDFTransformer like this:
from stages.Transform import PDFTransformer
Therefore I have created stages/Transform/__init__.py with the following content in it:
from .Transformer import Transformer
from .PDFTransformer import PDFTransformer
But python interpreter throws the following error (in PDFTransformer.py):
ModuleNotFoundError: No module named 'Transformer'
The PDFTransformer.py includes the following:
import Transformer
class PDFTransformer (Transformer):
pass
What I`m doing wrong?
If you ask Python to import Transformer from PDFTransformer.py, but when PDFTransformer.py is imported from core.py - i.e. when core.py is the __main__ file - then the import Transformer is relative to core.py
In other words it is as if you wrote import Transformer in core.py. That obviously wouldn't work.
Instead in PDFTransformer.py put from . import Transformer, and it will import Transformer.py
add __all__ =['Transformer', 'PDFTransformer'] in stages/Transform/__init__.py file.
so this __init__.py file would be like
from .Transformer import Transformer
from .PDFTransformer import PDFTransformer
__all__ =['Transformer', 'PDFTransformer']
this tell the python to load these function when you do from stages.Transform import PDFTransformer
This is my project structure:
maths/
__init__.py
trig.py
coord.py
pitch.py
models/
__init__.py
model.py
In model.py, I have:
import sys
sys.path.append("../")
from maths.pitch import *
if __name__ == "__main__":
# my routines
In pitch.py, I have:
from trig import calculate_distance_angles
When I run model.py, I'm getting the error:
File "model.py", line 38, in <module>
from maths.pitch import *
File "../maths/pitch.py", line 9, in <module>
from trig import calculate_distance_angles
ModuleNotFoundError: No module named 'trig'
How do I fix this?
The error came from here: from trig import calculate_distance_angles. I suppose you are looking for your maths/trig.py file:
maths/
__init__.py
trig.py <-- this one
coord.py
pitch.py
models/
__init__.py
model.py
On your sys.path, you may have this:
(all the original sys.path stuff)...
"../" (added at model.py)
But the maths model, as far as we can see, is not on sys.path. If that's true, you may have to modify the import as follows. This should work:
from maths.trig import calculate_distance_angles
I have the following Python package with 2 moludes:
-pack1
|-__init__
|-mod1.py
|-mod2.py
-import_test.py
with the code:
# in mod1.py
a = 1
and
# in mod2.py
from mod1 import a
b = 2
and the __init__ code:
# in __init__.py
__all__ = ['mod1', 'mod2']
Next, I am trying to import the package:
# in import_test.py
from pack1 import *
But I get an error:
ModuleNotFoundError: No module named 'mod1'
If I remove the dependency "from mod1 import a" in mod2.py, the import goes correctly. But that dependency makes the import incorrect with that exception "ModuleNotFoundError".
???
The issue here is that from mod2 perspective the first level in which it will search for a module is in the path from which you are importing it (here I am assuming that pack1 is not in your PYTHONPATH and that you are importing it from the same directory where pack1 is contained).
This means that if pack1 is in the directory /dir/to/pack1 and you do:
from mod1 import a
Python will look for mod1 in the same directory as pack1, i.e., /dir/to/pack1.
To solve your issue it is enough to do either:
from pack1.mod1 import a
or in Python 3.5+
from .mod1 import a
As a side note, unless this is a must for you, I do not recommend designing your package to be used as from pack import *, even if __all__ exists to give you better control of your public API.
I have a file:
STARTDIR/module/submodule/config.py
I have another file:
STARDIR/utils/filesys/getAbsPath.py
Why does this line work, in config.py?
from ..utils.filesys import getAbsPath
It seems like .. refers to module, not STARTDIR. There is no utils in module at all. In fact, doing
from .. import utils
yields
ImportError: cannot import name utils
This should work:
from ...utils.filesystem import getAbsPath
This is because:
from . import … imports from STARTDIR/module/submodule/
from .. import … imports from STARTDIR/module/
from ... import … imports from STARTDIR/
For:
app/
__init__.py
abc.py
mod/
__init__.py
def.py
How would I import abc.py from def.py?
to import module 'abc.py' that is in the parent directory of your current module:
import os
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)
import abc
in module def if you want to import say abc just do:
from ..abc import *
Note: as def is a python keyword, using that name for a module does not sound like a good idea.