Issues with importing from submodules - python

I've got a project with the following structure:
\main_model
__init__.py
main.py
\second_module
__init__.py
second.py
\third_module
__init__.py
third.py
second_module and third_module are both Git submodules used as libraries by many other projects.
The first line of main.py is an import statement: from main_model.second_module import second
And the first line of second.py is too: from second_module.third_module import third
Running main.py raises an error with the following Traceback:
Traceback (most recent call last):
File "/main_model/main.py", line 1, in <module>
from main_model.second_module import func_a, func_b
File "/main_model/second_module/second.py", line 1, in <module>
from third_module.third import func_c, func_d
ModuleNotFoundError: No module named 'third_module'
So evidently I would need to amend the import statement in second.py to be from main_model.second_module.third_module import third. I can't do that, though, because second_module is a standalone submodule.
Is there a consistent way of structuring the imports, or modifying PYTHONPATH, that would allow the same import syntax from all levels?

Related

Import Error when trying to make python 2 modul

This doesn't seem to be an issue in python 3 but I'm needing to use python 2.7 for this and get the following issue
DIRECTORY STRUCTURE
module
├── __init__.py
└── submodule
├── __init__.py
└── test.py
# module/__init__.py
from module import submodule
# module/submodule/__init__.py
from module.submodule import test
# module/submodule/test.py
from module import submodule
when I try to import module from somewhere else it results in the following error:
>>> import module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "module/__init__.py", line 1, in <module>
from module import submodule
File "module/submodule/__init__.py", line 1, in <module>
from module.submodule import test
File "module/submodule/test.py", line 1, in <module>
from module import submodule
ImportError: cannot import name submodule
I'm assuming the issue has something to do with circular imports but i need to use submodule in both module/__init__.py and module/submodule/test.py
any help is appreciated
As you've mentioned circular imports, one way to resolve this is by importing the module locally.
For e.g.
def fun():
from module import submodule

Attempted relative import in non-package” even with __init__.py

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

Python self-created package import path

(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/

How to specify an import path when the file is called from different directories?

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.

Running python from command line gives import error

I want to run my python program from command line but it gives me below error
ImportError: No module named 'main'
My folder structure is as below
Project
|-----main
|-----__init__.py
|-----S3Operations.py
|-----BusinessOperations.py
My init.py code is as below
import sys
from S3Operations import Models
sys.path.append("D:/code/Project/main")
if __name__ == '__main__':
s3=Models()
s3.test()
And my S3Operations.py code is
import os.path
from main import BusinessService
class ModelsMlS3(object):
def test(self):
print("Testing success")
When I run the program using command line i get the below error
$ python __init__.py
Traceback (most recent call last):
File "__init__.py", line 2, in <module>
from S3Operations import ModelsMlS3
File "D:\code\Project\main\S3Operations.py", line 11, in <module>
from main import BusinessService
ImportError: No module named 'main'
Can any one please suggest a solution for the same.
You just need to do:
import BusinessService # instead of `from main import BusinessService`
as in your project, there is no __init__.py file present in Project directory (which holds main.py).
For importing it like:
from main import BusinessService
you need to create __init__.py in the folder in order to make it as module.

Categories

Resources