[英]Python Import Error - Run unittest
Why am I getting import error for a module I have in the project. 为什么我的项目中的模块出现导入错误。 All the packages are under the project, they all have __init __.py and other scripts do not give the same error.
所有软件包都在项目下,它们都具有__init __.py,其他脚本没有给出相同的错误。 Python version is 3.6.
Python版本是3.6。 Code was written in Unix environment.
代码是在Unix环境中编写的。
Here is the import error I get. 这是我得到的导入错误。 I am trying to run a test here.
我正在尝试在这里进行测试。
Ran 1 test in 0.001s
FAILED (errors=1)
Error
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/usr/lib/python3.6/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_SMSHandler
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/home/sevvalboylu/server/app/api/test_SMSHandler.py", line 11, in <module>
from middleware.services import Sender
ModuleNotFoundError: No module named 'middleware'
Looks like you are missing a project's root path in PYTHONPATH 看起来您在PYTHONPATH中缺少项目的根路径
From the docs ( Modules - The Module Source Path ) 从文档( 模块-模块源路径 )
When a module named spam is imported, the interpreter first searches for a built-in module with that name.
导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。 If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.
如果找不到,它将在变量sys.path给出的目录列表中搜索名为spam.py的文件。 sys.path is initialized from these locations:
sys.path从以下位置初始化:
- The directory containing the input script (or the current directory when no file is specified).
包含输入脚本的目录(如果未指定文件,则为当前目录)。
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
PYTHONPATH(目录名称列表,语法与shell变量PATH相同)。
- The installation-dependent default.
取决于安装的默认值。
If this solution doesn't work for you, please post the project's tree to make it easier find the problem. 如果该解决方案不适合您,请发布项目的树以使其更容易发现问题。
I've experienced a similar problem with import error when running unit tests (with correct importable structure), but the cause and the solution are different to described in the answer above. 运行单元测试(具有正确的可导入结构)时,我也遇到了导入错误的类似问题,但是原因和解决方案与上面的答案中描述的不同。 Still this is relevant and may help somebody.
但这仍然是相关的,可能会对某人有所帮助。
In my case I have structure like that ( __init__.py
present but omitted below for brevity): 就我而言,我具有这样的结构(
__init__.py
存在,但为简洁起见,在下面省略):
mypkg
\-- foo.py
another
tests
\-- some
\-- <tests here>
mypkg <--- same name as top level module mypkg
\-- test_a.py
When running from top level directory imports in test_a.py
of modules from mypkg
were failing: 当从顶层目录的进口运行
test_a.py
模块从mypkg
是失败的:
# test_a.py
from mypkg import foo # causes ModuleNotFoundError: No module named 'mypkg.foo'
Renaming mypkg
folder under tests
folder into something else solved the problem. 重命名
mypkg
文件夹下的tests
文件夹到别的解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.