Python- Openpyxl在控制台中工作但无法导入

时间:2016-03-21 23:47:04

标签: python elementtree importerror openpyxl

我在使用openpyxl写入Excel文件时遇到问题,当我在PyCharm Python控制台中运行以下代码时,它工作正常,但是当我创建&运行.py文件我收到以下错误:

  

C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ Scripts \ python.exe C:/Python27/virtualenv-15.0.1/virtualenv/test.py   Traceback(最近一次调用最后一次):     文件" C:/Python27/virtualenv-15.0.1/virtualenv/test.py" ;,第1行,在       来自openpyxl import Workbook     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl__init __。py",第28行,in       来自openpyxl.workbook导入工作簿     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ workbook__init __。py",第5行,in       来自.workbook import *     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ workbook \ workbook.py",第7行,in       来自openpyxl.worksheet导入工作表     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ worksheet__init __。py",第4行,in       来自.worksheet import *     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ worksheet \ worksheet.py",第34行,在       来自openpyxl.cell导入Cell     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ cell__init __。py",第4行,in       来自.cell进口*     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ cell \ cell.py",第44行,in       来自openpyxl.styles导入数字,is_date_format     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ styles__init __。py",第4行,in       来自openpyxl.descriptors导入Typed     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ descriptors__init __。py",第4行,in       来自.base import *     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ descriptors \ base.py",第12行,in       来自openpyxl.xml.functions import Element     文件" C:\ Users \ David \ PycharmProjects \ VirtualEnv1 \ VirtualEnv1 \ lib \ site-packages \ openpyxl \ xml \ functions.py",第41行,在       来自xml.etree.ElementTree导入(   ImportError:没有名为etree.ElementTree的模块   处理以退出代码1完成

我从https://openpyxl.readthedocs.org/en/default/index.html安装,并按照建议使用虚拟环境。我还将elementtree包下载到虚拟环境,但脚本仍然失败。任何帮助将不胜感激,谢谢!

from openpyxl import Workbook

wb = Workbook()
ws1 = wb.create_sheet()
ws1.title = "worksheet1"

c = ws1['A4']

ws1['A4'] = 15

cell_range = ws1['A1':'C2']

for row in ws1.iter_rows('A1:C2'):
    for cell in row:
        print cell

wb.save('balances.xlsx')

Console run

2 个答案:

答案 0 :(得分:1)

通过创建新的PyCharm虚拟环境而不选择“继承全局站点包”复选框解决了该问题:

https://www.jetbrains.com/help/pycharm/5.0/creating-virtual-environment.html?origin=old_help

答案 1 :(得分:0)

您的脚本在哪里使用etree.ElementTree?这在控制台中对我很有用:

$ virtualenv .venv
$ . .venv/bin/activate
$ pip install openpyxl

$ tee test.py << 'EOF'
from openpyxl import Workbook

wb = Workbook()
ws1 = wb.create_sheet()
ws1.title = "worksheet1"

c = ws1['A4']

ws1['A4'] = 15

cell_range = ws1['A1':'C2']

for row in ws1.iter_rows('A1:C2'):
    for cell in row:
        print cell

wb.save('balances.xlsx')

EOF

$ python test.py