Python nested subpackage doesn't import when running tests - python

I have a folder structure like this:
api
-- test
-- test_api.py
-- __init__.py
-- api
-- api.py
-- __init__.py
-- sub
-- sub.py
-- __init__.py
sub.py:
Base = 'base'
api.py:
from sub.sub import Base
def stuff_to_test():
pass
test_api.py:
from api.api import stuff_to_test
def test_stuff_to_test():
stuff_to_test()
I am in directory api.
I run pytest:
==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_api.py ______________________
ImportError while importing test module '/<somepath>/api/tests/test_api.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_api.py:1: in <module>
from ..api.api import stuff_to_test
api/__init__.py:1: in <module>
from . import api
api/api.py:1: in <module>
from sub.sub import Base
E ImportError: No module named 'sub'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.08 seconds ============================
Same happens if I run the python interpreter and import stuff from test_api.py:
>>> from tests.test_api import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/<somepath>/api/tests/test_api.py", line 1, in <module>
from api.api import stuff_to_test
File "/<somepath>/api/api/__init__.py", line 1, in <module>
from . import api
File "/<somepath>/api/api/api.py", line 1, in <module>
from sub.sub import Base
ImportError: No module named 'sub'
My first idea was to make the import in api.py relative:
from .sub.sub import Base
This way tests run fine.
But if I run python api/api.py I get this error:
Traceback (most recent call last):
File "api/api.py", line 1, in <module>
from .sub.sub import Base
SystemError: Parent module '' not loaded, cannot perform relative import
How can I have it so tests run and application runs?

I solved it by adding the following to test.__init__.py
project_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
api_path= os.path.join(project_path, 'api')
sys.path.append(api_path)

In python there are two ways to import a module, with a relative path or with an absolute path.
When you write from sub.sub import Base you are doing an absolute path import, for a relative path import write from .sub.sub import Base.
An absolute path import go look in the PYTHONPATH to find the starting point of your import, so you should write from api.sub.sub import Base.
For more information: Absolute vs. explicit relative import of Python module

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

Issues with importing from submodules

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?

Python Import Error: I am getting error when importing function from package. Import error on test.py when performing mymath.multiply()

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."

relative import with `__package__` specified

I know there are hundreds of post on this issue, but as fare as I see, mine is a bit different.
My project structure is the following:
package/
>script.py
>__init__.py
>submodule
>__init__.py
>otherstuff.py
>...
You should be able to run script.py simpy as a script $ ./package/script.py.
script.py needs information of submodule.__init__ so i attempt do do a relative import of submodule.
According to PEP-366 it is not possible to do relative imports, because if the script is run, __name__ will be set to '__main__' and __package__ to None.
To still use relative imports I manually set __package__ = 'package' before my imports. This allows me to properly do import package.
However from . import plotter yields
Traceback (most recent call last):
File "./script.py", line 9, in <module>
import package
File ".../package/script.py", line 13, in <module>
from . import submodule
ImportError: cannot import name submodule
if I previously imported package.
Else it yields
Traceback (most recent call last):
File "./script.py", line 12, in <module>
from . import submodule
SystemError: Parent module 'package' not loaded, cannot perform relative import
Can anyone help me how to fix the relative imports?
I don't want to use path hacks like
from os import path, pardir
from sys import path as syspath
PATH = path.abspath(path.dirname(__file__))
syspath.append(path.join(PATH, pardir))
and it is also not possible to add package to the 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