2013-01-06 18 views
0

我需要從xlwt模塊導入類,因此我試圖得到錯誤。爲什麼這樣? 你能指導我嗎?在使用Python2.7時得到導入錯誤

Microsoft Windows [Version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

C:\Users\Matty>python 
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from xlwt import easyxf 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named xlwt 
>>> from xlwt import Workbook 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: No module named xlwt 
>>> 

編輯

C:\Users\matt>easy_install xlwt 
Searching for xlwt 
Best match: xlwt 0.7.4 
Processing xlwt-0.7.4-py2.7.egg 
Adding xlwt 0.7.4 to easy-install.pth file 

Using c:\python27\lib\site-packages\xlwt-0.7.4-py2.7.egg 
Processing dependencies for xlwt 
Finished processing dependencies for xlwt 

C:\Users\matt> 

EDIT1

按照從@ab它工作的意見。

但爲什麼我需要導入sys?

C:\Users\matt>python 
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sys 
>>> sys.path 
['', 'C:\\Python27\\lib\\site-packages\\beautifulsoup4-4.1.3-py2.7.egg', 'C:\\Py 
thon27\\lib\\site-packages\\selenium-2.28.0-py2.7.egg', 'C:\\Python27\\lib\\site 
-packages\\xlwt-0.7.4-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Py 
thon27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27 
\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] 
>>> from xlwt import easyxf 
>>> from xlwt import Workbook 
>>> from xlwt import * 
>>> 
+3

你安裝了'xlwt'模塊嗎? –

+0

你下載並安裝了它嗎? – Tim

+0

是的,我已經使用'easy_install xlwt'安裝了它。 –

回答

3

您可以運行import sys ; sys.path並查看c:\python27\lib\site-packages\是否在您的路徑列表中。如果是,那麼再試試運行from xlwt import easyxf

+0

請參閱EDIT1 –

+1

當我從'selenium,bs4'導入時,需要'import sys'來加載和運行sys.path(檢查您的PYTHONPATH) –

+0

然後我沒有使用'import sys'。但對於'xlrd','xlwt'爲什麼我需要這樣做? –

1

很顯然你沒有安裝xlwt,或Python不知道你怎麼做。

嘗試運行pip install xlwt

您也可以看看xlwt的代碼在物理上的位置,然後將該文件目錄添加到您的PYTHONPATH,以便Python知道該模塊的位置。

這可能是因爲您正在運行您沒有安裝xlwtvirtualenv中的代碼。

+0

請參閱我的**編輯**。我剛剛更新 –

+0

我沒有使用'virtualenv'。 –