python relative import weirdness - python

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/

Related

Unknown parent package

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

can't be able to import module while having modules on different folders in python

I'm having project structure like below
code
|
-- core --
| |
| test1.py
| test2.py
|---database--
| |
| sample.py
--main.py
main.py is entry point for my application, from main.py I can import test1 from folder core by following code
from core import test1 and it works fine
But when I try to import test2 from test1(both are on same folder core)
I'm using the following in test1
import test2
but I'm getting no module named test2 error
why is that so?
How can I import sample.py in database from test1.py module in core ?
What do you have inside these files? Functions? Classes?
You can try:
in test1.py: from .test2 import *
and in sample.py: from ..core.test1 import *
First of all: Import statement docs
There are 4 main ways you can import modules or module attributes using import statement:
Absolute module import import module_name.submodule.submodule
Absolute limited import from module_name.submodule import module_attribute, module
Relative limited import from .submodule import module_attribute, module
Relative wildcard import from .submodule import *
What you should know for now: absolute imports resolves path from project root. In your case import test2 is telling python to load code/test2.py instead of code/core/test2.py. To fix this issue, either supply proper absolute path:
import core.test2
or switch to relative path:
from . import test2 if you import from code/core/test1.py
from .core import test2 if you import from code/main.py
from ..core import test2 if you import from code/database/sample.py
Warning: be aware of cyclic imports:
from .b import *
from .a import *

Python: A problem with the package import from ... import * using __all__ and __init__

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.

Import a module from a package

I'm trying to import a simple package, but it doesn't work.
I have a "package" directory that contains two files:
foo.py (with a function called fct)
__init__.py (with nothing in it)
Here is the content of tests.py:
import package.foo
foo.fct(7)
But it doesn't work.
If I change the import line to from package.foo import fct, I can execute the function.
You need import package.foo as foo or one of the alternatives below ...
import package.foo
package.foo.fct(7)
or:
import package.foo as foo
foo.fct(7)
or possibly:
from package import foo
foo.fct(7)

What does ".main" mean in python source code?

In a source code in python: usr/local/lib/python3.3/unittest/__init__.py
from .result import TestResult
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
skipUnless, expectedFailure)
from .suite import BaseTestSuite, TestSuite
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
findTestCases)
from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
I can't understand .result and .main. Why do they have a dot prefix in the name?
It's called a relative import.
It means you import from the module in the same directory that the module this code is in. Without the dot, it would import the from first module found in the PYTHON PATH.
You are importing main module which is in the same package that your file, you are doing a relative import (dot prefix). More on relative imports on PEP 328

Categories