繁体   English   中英

尝试使用Python 2.7时出现导入错误

[英]Getting import error in Python 2.7 when tried with

我需要从xlwt模块导入类,因此我尝试获取错误。 为什么这样? 你可以在这里引导我吗?

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 *
>>>

您可以运行import sys ; sys.path import sys ; sys.path并查看c:\\python27\\lib\\site-packages\\是否在您的路径列表中。 如果是,则再次尝试from xlwt import easyxf运行

您显然没有安装xlwt ,或者Python不知道您这样做。

尝试运行pip install xlwt

您还可以查看xlwt的代码在物理上的位置,然后将该文件目录添加到PYTHONPATH以便Python知道在那里查找该模块。

这可能是因为您正在xlwt安装xlwtvirtualenv中运行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM