Python- Openpyxl 在控制台中工作但无法导入
Python- Openpyxl works in console but fails to import
我在让 openpyxl 写入 Excel 文件时遇到问题,当我 运行 在 PyCharm Python 控制台中输入以下代码时它工作正常但是当我创建 & 运行 .py
文件时,出现以下错误:
C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\Scripts\python.exe C:/Python27/virtualenv-15.0.1/virtualenv/test.py
Traceback (most recent call last):
File "C:/Python27/virtualenv-15.0.1/virtualenv/test.py", line 1, in
from openpyxl import Workbook
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl__init__.py", line 28, in
from openpyxl.workbook import Workbook
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\workbook__init__.py", line 5, in
from .workbook import *
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\workbook\workbook.py", line 7, in
from openpyxl.worksheet import Worksheet
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\worksheet__init__.py", line 4, in
from .worksheet import *
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\worksheet\worksheet.py", line 34, in
from openpyxl.cell import Cell
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\cell__init__.py", line 4, in
from .cell import *
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\cell\cell.py", line 44, in
from openpyxl.styles import numbers, is_date_format
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\styles__init__.py", line 4, in
from openpyxl.descriptors import Typed
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\descriptors__init__.py", line 4, in
from .base import *
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\descriptors\base.py", line 12, in
from openpyxl.xml.functions import Element
File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\xml\functions.py", line 41, in
from xml.etree.ElementTree import (
ImportError: No module named etree.ElementTree
Process finished with exit code 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')
您的脚本在哪里使用 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
通过创建新的 PyCharm 虚拟环境并且不选中 "Inherit global site packages" 复选框解决了该问题:
https://www.jetbrains.com/help/pycharm/5.0/creating-virtual-environment.html?origin=old_help
我在让 openpyxl 写入 Excel 文件时遇到问题,当我 运行 在 PyCharm Python 控制台中输入以下代码时它工作正常但是当我创建 & 运行 .py
文件时,出现以下错误:
C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\Scripts\python.exe C:/Python27/virtualenv-15.0.1/virtualenv/test.py Traceback (most recent call last): File "C:/Python27/virtualenv-15.0.1/virtualenv/test.py", line 1, in from openpyxl import Workbook File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl__init__.py", line 28, in from openpyxl.workbook import Workbook File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\workbook__init__.py", line 5, in from .workbook import * File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\workbook\workbook.py", line 7, in from openpyxl.worksheet import Worksheet File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\worksheet__init__.py", line 4, in from .worksheet import * File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\worksheet\worksheet.py", line 34, in from openpyxl.cell import Cell File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\cell__init__.py", line 4, in from .cell import * File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\cell\cell.py", line 44, in from openpyxl.styles import numbers, is_date_format File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\styles__init__.py", line 4, in from openpyxl.descriptors import Typed File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\descriptors__init__.py", line 4, in from .base import * File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\descriptors\base.py", line 12, in from openpyxl.xml.functions import Element File "C:\Users\David\PycharmProjects\VirtualEnv1\VirtualEnv1\lib\site-packages\openpyxl\xml\functions.py", line 41, in from xml.etree.ElementTree import ( ImportError: No module named etree.ElementTree Process finished with exit code 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')
您的脚本在哪里使用 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
通过创建新的 PyCharm 虚拟环境并且不选中 "Inherit global site packages" 复选框解决了该问题:
https://www.jetbrains.com/help/pycharm/5.0/creating-virtual-environment.html?origin=old_help