drn614
drn614

Reputation: 121

Python- Openpyxl works in console but fails to import

I am having an issue getting openpyxl to write to an Excel file, when I run the following code in the PyCharm Python console it works fine but when I create & run the .py file I get the following error :

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

I installed from https://openpyxl.readthedocs.org/en/default/index.html and am using the virtual environment as recommended. I also downloaded the elementtree package to the virtual environment but the script still fails. Any help would be appreciated, thanks!

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

Upvotes: 0

Views: 1737

Answers (2)

drn614
drn614

Reputation: 121

The issue was resolved by creating a new PyCharm virtual environment and not selecting the "Inherit global site packages" checkbox:

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

Upvotes: 1

dron22
dron22

Reputation: 1233

Where does your script use etree.ElementTree? This worked for me fine in the console:

$ 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

Upvotes: 0

Related Questions