I have a problem where both absolute and relative imports are failing in a non-obvious manner. I have the following directory structure:
package1
__init__.py
src
test1.py
lib
test2.py
__init__.py
__init__.py
Test1.py has the following code:
import os
print(f'CWD in test12.py is : {os.getcwd()}')
from src.lib import test2
Test2.py has the following code:
import os
print(f'CWD in test2.py is : {os.getcwd()}')
from package1.src import test1
If I am positioned in package1 and I run the following command, I get the result:
PS C:\Users\jgoss\Personal\package1> python src/test1.py
CWD in test2.py is : C:\Users\jgoss\Personal\package1
Traceback (most recent call last):
File "C:\Users\jgoss\Personal\package1\src\test1.py", line 3, in <module>
from src.lib import test2
ModuleNotFoundError: No module named 'src'
which does not make sense to me as src is a package, not a module.
If I change test1.py and test2.py to use relative imports like this:
Test1.py
import os
print(f'CWD in test12.py is : {os.getcwd()}')
from ..lib import test2
test2.py
PS C:\Users\jgoss\Personal\Infrastructure> python src/test1.py
CWD in test12.py is : C:\Users\jgoss\Personal\package1
Traceback (most recent call last):
File "C:\Users\jgoss\Personal\package1\src\test1.py", line 3, in <module>
from ..lib import test2
ImportError: attempted relative import with no known parent package
which I cannot interpret since package1 is a package.
Can anyone see what I am doing wrong?
The only unusual thing that I can see is that there is a mutual dependency between test1.py and test2.py since each one imports the other.
Related
I have a file hierarchy of a python package, "try_from_import", like this:
.
├── __init__.py
├── fibo.py
└── test
└── __init__.py
Within the directory "test", I cannot import the module fibo.py:
In [1]: from .. import fibo
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-94aab0998156> in <module>
----> 1 from .. import fibo
ImportError: attempted relative import with no known parent package
How can I import the module fibo from the folder "test"? I am using python 3.7 on a Mac OS if that matters.
[update] Following the advice in the comments, "do not run python from within the package", I tried to run a file test/test.py which contains "from .. import fibo" from outside the package named "try_from_import". But I still got the error:
% python try_from_import/test/test.py
Traceback (most recent call last):
File "try_from_import/test/test.py", line 1, in <module>
from .. import fibo
Getting error “Attempted relative import in non-package” even with init.py.
pkg/
__init__.py
conf/
core.py
__init__.py
scripts/
core_script.py
__init__.py
In core_script.py I have the following import statement
from ..conf.core import gDocsCred
However, when I run, I get the following error:
scripts$ python core_test.py
Traceback (most recent call last):
File "core_script.py", line 3, in <module>
from ..conf.core import gDocsCred
ValueError: Attempted relative import in non-package
The problem was there because I was not using it as a package.
python -m pkg.scripts.core_script
(Solved)
This link https://dev.to/codemouse92/dead-simple-python-project-structure-and-imports-38c6 explains Python structure and imports well, too.
I have created a Python package test_pkg.
test_pkg/
├── setup.py
└── test_pkg
├── __init__.py
├── main.py
└── sub_pkg
├── __init__.py
└── sub_pkg_a.py
In the main.py, a module from a sub-package will be imported,
but I have to explicitly write:
from test_pkg.sub_pkg import sub_pkg_a
Otherwise, if I only write:
from sub_pkg import sub_pkg_a
Python will report:
>>> import test_pkg.main
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/zheng/tests/test_pkg/test_pkg/main.py", line 2, in <module>
from sub_pkg import sub_pkg_a
ModuleNotFoundError: No module named 'sub_pkg'
Could you tell me why the package name is required: test_pkg.sub_pkg?
The relative import works fine with the package, but if I execute the main.py script directly, there will be another import error:
~/tests/test_pkg/test_pkg$ python3 main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from .sub_pkg import sub_pkg_a
ModuleNotFoundError: No module named '__main__.sub_pkg'; '__main__' is not a package
So, the question is: paths of __init__.sub_pkg and __main__.sub_pkg are different?
What you need to use here is relative import.
Try running the same thing with
from .sub_pkg import sub_pkg_a
The dot in this case refers to "relative to the file that import is in", in this case relative to the location of main.py
This article explains it pretty nicely https://realpython.com/absolute-vs-relative-python-imports/
This is my Directory Structure:
test.py
mymath/
__init__.py
mymath.py
test.py
import mymath
mymath.multiply()
__init__.py
from mymath
import multiply
mymath.py
def multiply():
When I run:
python3 test.py
I get the error:
Traceback (most recent call last):
File "test.py", line 2, in <module>
import mymath
File "/home/kcb/python-scripts/mymath/__init__.py", line 1, in <module>
from mymath import multiply
ImportError: cannot import name 'multiply'
When you run python myscript.py, the current working directory directory is added to your module search path, and thus any modules in that directory are importable.
However, when mymath/__init__.py tries to do from mymath import multiply, it can't find mymath.py, because mymath/ is not in your module search path.
The best solution is to change mymath/__init__.py to use a different import statement:
from .mymath import multiply
This means "import multiply from a module named mymath in the same directory as this module."
I am facing an issue that is related to import path. I have a library file (func_a.py) as follows. This file is called from different directories. In such a case, how do I specify import path in client.py?
.
├── main.py
└── package_a
├── __init__.py
├── client.py
└── func_a.py
The codes are as follows:
$ cat package_a/func_a.py
def something():
print('something')
$ cat package_a/client.py
import func_a
func_a.something()
$ cat main.py import package_a.func_a as func_a
import package_a.client as client
func_a.something()
This is the error. When I call client.py, the file misses func_a.py since the current directory is the root, not package_a/.
$ python main.py Traceback (most recent call last):
File "main.py", line 2, in <module>
import package_a.client as client
File "/home/jef/work/test/package_a/client.py", line 1, in <module>
import func_a
ModuleNotFoundError: No module named 'func_a'
My python is 3.6. Thank you for your help.
Update
Although calling main.py is OK, calling client.py failed. I make both work.
$ cat client.py
from package_a import func_a
func_a.something()
$ python client.py
Traceback (most recent call last):
File "client.py", line 2, in <module>
from package_a import func_a
ModuleNotFoundError: No module named 'package_a'
$ cat package_a/client.py
import func_a
^ This import statement is incorrect. To import the func_a module from the package_a package, use this import statement instead:
from package_a import func_a
You will need to ensure that the directory containing package_a is visible in sys.path.