2017-06-01 173 views
1

我在Python 3.6中导入文件时出现问题。我的目录树,如下所示:在Python3中导入错误正在运行unittest

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

它的工作原理我的应用程序(但没有工作的测试)使用b.py下面的import语句:

from a import * 

但是,它不工作我的应用程序(但是,在b.py使用该作品的其他测试):

from .a import * 

所以,我选择from a import *。执行像python3 -m unittest测试,我总是得到以下错误:

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_path 
    module = 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.001s 

FAILED (errors=1) 

在这种情况下,在test_b.py我的导入语句,如下所示:

from unittest import TestCase 
from app.cell import * 

有没有什么办法来解决这个问题呢?

+0

你可以发布你的内容\ _ \ _ init \ _ \ _。py吗? –

+0

我认为你更容易分享[资源库](https://github.com/MULCIA/TFMSTGCA)。在那里你可以看到所有的信息,因为在这篇文章中我不会假装展示我的具体情况。 –

+0

关于你的问题,'__init __。py'是空的,就像我可以看到的很多例子。 –

回答

0

我对Python中的导入以及python如何处理模块感到困惑。

project/ 
    module/ 
     __init__.py 
     a.py 
     b.py 
    test/ 
     test_a.py 
     test_b.py 
    main.py 

这是我的新目录树。该包含的文件有:

main.py

from module.b import Something 

b.py

from .a import Something 

a.py

from unittest import TestCase 
from module.a import Something 

test_b.py

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

像这样,它工作正常,应用程序和测试。