2017-06-01 41 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 

像這樣,它工作正常,應用程序和測試。