Import error running unittest in Python3

2024/10/5 23:20:38

I have a problem importing files in Python 3.6. My directories tree is as given below:

project/app/├── __init__.py├── a.py└── b.pytest/├── __init__.py├── test_a.py└── test_b.py

It works my application (but, no works the tests) using following import statement in b.py:

from a import *

But, it does not work my application (but, works the tests) using this other in b.py:

from .a import *

So, I choose from a import *. Executing test like python3 -m unittest I always get following error:

E.
======================================================================
ERROR: tests.test_cell (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_cell
Traceback (most recent call last):File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 428, in _find_test_pathmodule = self._get_module_from_name(name)File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name__import__(name)File "/Users/serrodcal/Repositories/project/tests/test_b.py", line 2, in <module>from app.b import *File "/Users/serrodcal/Repositories/project/app/b.py", line 1, in <module>from a import *
ModuleNotFoundError: No module named 'a'----------------------------------------------------------------------
Ran 2 tests in 0.001sFAILED (errors=1)

In this case, my import statement in test_b.py is as given below:

from unittest import TestCase
from app.cell import *

Is there any way to fix this problem?

Answer

I was confused with imports in Python and how python works with modules.

project/module/__init__.pya.pyb.pytest/test_a.pytest_b.pymain.py

This is my new directories tree. The contains of the files are:

In main.py:

from module.b import Something

In b.py:

from .a import Something

In a.py:

from unittest import TestCase
from module.a import Something

In test_b.py:

from unittest import TestCase
from module.a import Something
from module.b import Something

Like this, it works fine, application and tests.

https://en.xdnf.cn/q/70429.html

Related Q&A

python: obtaining the OSs argv[0], not sys.argv[0]

(This question was asked here, but the answer was Linux-specific; Im running on FreeBSD and NetBSD systems which (EDIT: ordinarily) do not have /proc.)Python seems to dumb down argv[0], so you dont get…

Why does mypy not accept a list[str] as a list[Optional[str]]?

Example 1: from typing import List, Optionaldef myfunc() -> List[Optional[str]]:some_list = [x for x in "abc"]return some_listMypy complains on example 1:Incompatible return value type (go…

How to do I groupby, count and then plot a bar chart in Pandas?

I have a Pandas dataframe that looks like the following.year month class ---- ----- ----- 2015 1 1 2015 1 1 2015 1 2 2015 1 2 ...I want to be able to create 2 bar chart seri…

How do I execute more code after closing a PyQt window?

Heres an example below:if __name__ == __main__:import sysif (sys.flags.interactive != 1) or not hasattr(QtCore, PYQT_VERSION):QtGui.QApplication.instance().exec_()print "you just closed the pyqt w…

Tor doesnt work with urllib2

I am trying to use tor for anonymous access through privoxy as a proxy using urllib2.System info: Ubuntu 14.04, recently upgraded from 13.10 through dist-upgrade.This is a piece of code I am using for …

Python Selenium Chrome disable prompt for Trying to download multiple files

I am currently running a Python automator which needs to download multiple files within the same session using Selenium Chromedriver.The problem is that when the browser attempts to download the second…

Label outliers in a boxplot - Python

I am analysing extreme weather events. My Dataframe is called df and looks like this:| Date | Qm | |------------|--------------| | 1993-01-…

Matplotlib how to draw vertical line between two Y points

I have 2 y points for each x points. I can draw the plot with this code:import matplotlib.pyplot as pltx = [0, 2, 4, 6] y = [(1, 5), (1, 3), (2, 4), (2, 7)]plt.plot(x, [i for (i,j) in y], rs, markersiz…

Cythonizing fails because of unknown type name uint64_t

This may be a newbie problem. I cant cythonize a simple helloworld.pyx tutorial script while the exact same code works on linux:print("hello world")Here is the setup.py script: from distutils…

How to save changes in read-only Jupyter Notebook

I have opened a python Jupyter notebook but did not notice that it was in read-only, Not Trusted mode. How to save my changes now?Things that I have tried and did not help:File -> Make a Copy File …