2015-11-18 136 views
1

我有一個測試/目錄和一個導入一些第三方程序(熊貓等)的文件。我在安裝了熊貓的虛擬環境中運行。由於導入錯誤導致Py.test測試類失敗

pip freeze | grep pandas; cat requirements.txt | grep pandas 
pandas==0.16.0 
pandas==0.16.0 

當我執行py.test我得到以下錯誤:

tests/test_pipeline.py:4: in <module> 
    import pandas as pd 
E ImportError: No module named pandas 

哪個是進口大熊貓test_pipeline.py文件中調用。

cat -n tests/test_pipeline.py | more 
    1 import sys 
    2 import os 
    3 import filecmp 
    4 import pandas as pd 

爲什麼在virtualenv設置正確時這是錯誤?我做錯了什麼?

TIA

回答

1

你安裝了多個版本的Python?如果是這樣,py.test可能會使用與從命令行運行腳本時使用的版本不同的版本。

例如,我的系統上,我的Python 3.5我PATH,所以:

>py.test --version 
This is pytest version 2.8.1, imported from c:\program files\python 3.5\lib\site-packages\pytest.py 

但是,如果我運行命令行Python腳本:(這一個打印platform.python_version()

>test.py 
2.7.8 

事實上,版本取決於我在腳本中聲明的版本,例如。如果該腳本的第一行顯示爲#! python3,則會打印3.5.0。 (並且Linux系統也會查看shebang線)

因此,確保在您運行py.test時使用的版本上安裝了pandas。

相關問題