xlrd and py2exe

763 views
Skip to first unread message

MaReK Penguin_007 Olsavsky

unread,
Mar 4, 2010, 9:35:00 AM3/4/10
to python-excel
Hello,
i've little question.
I use xlrd for reading of Excel files and i need to create standalone
package (for computers, without python) in MS Windows. I need find how
to set setup.py. Compilation finished OK, but run of program fails with
message in log:
Traceback (most recent call last):
File "frmMain.py", line 13, in <module>
File "clsConvert.pyo", line 4, in <module>
ImportError: No module named xlrd

In Python environment all works, but program generated by py2exe returns
this error.

Setup py:

#! /usr/bin/python
# -*- coding: utf-8 -*-

from distutils.core import setup
import py2exe

#setup(console=["victor.py"])
setup(
options = {
"py2exe": {
"compressed": 1,
"optimize": 2,
"packages": ["Pmw"],
"excludes": [],
}
},
name="Docházky Splirec",
version="1.0 B1",
description="Převod docházky firmy Splirec z XLS do Amzdy",
author="C.A.C spol. s r. o.",
author_email="x...@uuu.aa",
windows=[{"script": "frmMain.py"}],
)

Massa, Harald Armin

unread,
Mar 4, 2010, 10:34:56 AM3/4/10
to python...@googlegroups.com
I use xlrd for reading of Excel files and i need to create standalone
package (for computers, without python) in MS Windows. I need find how
to set setup.py. Compilation finished OK, but run of program fails with
message in log:
Traceback (most recent call last):
 File "frmMain.py", line 13, in <module>
 File "clsConvert.pyo", line 4, in <module>
ImportError: No module named xlrd

In Python environment all works, but program generated by py2exe returns
this error.

Setup py:

#! /usr/bin/python
# -*- coding: utf-8 -*-

Reason:

distutils does not pickup your xlrd import. 

Most likely course:

 - you import from xlrd, but do not import xlrd

that is

from xlrd import Sheet, Row...

I recommend to put an "import xlrd" in the main module of your application.

BEst wishes,

Harald

-- 
GHUM Harald Massa
persuadere et programmare
Harald Armin Massa
Spielberger Straße 49
70435 Stuttgart
0173/9409607
no fx, no carrier pigeon
-
%s is too gigantic of an industry to bend to the whims of reality

MaReK Penguin_007 Olsavsky

unread,
Mar 5, 2010, 2:06:36 AM3/5/10
to python...@googlegroups.com
Good morning,

Dne 4.3.2010 16:34, Massa, Harald Armin napsal(a):


> Setup py:
>
> #! /usr/bin/python
> # -*- coding: utf-8 -*-
>
> Reason:
>
> distutils does not pickup your xlrd import.
>
> Most likely course:
>
> - you import from xlrd, but do not import xlrd
>
> that is
>
> from xlrd import Sheet, Row...
>
> I recommend to put an "import xlrd" in the main module of your
> application.

^^^I've tried this, but without positive effect, error stays same:


"""
Traceback (most recent call last):
File "frmMain.py", line 13, in <module>
File "clsConvert.pyo", line 4, in <module>
ImportError: No module named xlrd
Traceback (most recent call last):
File "frmMain.py", line 13, in <module>
ImportError: No module named xlrd
"""

Sources (parts of) are at the bottom of mail. Does anybody using xlrd
and py2exe together? I'm user of Python 2.6.

*Setup.py*:


#! /usr/bin/python
# -*- coding: utf-8 -*-

from distutils.core import setup
import py2exe

setup(


options = {
"py2exe": {
"compressed": 1,
"optimize": 2,
"packages": ["Pmw"],
"excludes": [],
}
},
name="Docházky Splirec",
version="1.0 B1",
description="Převod docházky firmy Splirec z XLS do Amzdy",
author="C.A.C spol. s r. o.",

author_email="m.ols...@amzdy.cz",


windows=[{"script": "frmMain.py"}],
)

*Begin of frmMain.py*


#! /usr/bin/python
# -*- coding: utf-8 -*-

#system imports
from Tkinter import *
import tkMessageBox, tkFileDialog
import os, fnmatch
from time import localtime, strftime

#other imports
import Pmw
import xlrd
from clsConvert import *


class frmSplirec:
def __init__(self,master,ar,xy,flex):

*Begin of clsConvert:*


#! /usr/bin/python
# -*- coding: utf-8 -*-

import xlrd
from xlrd import open_workbook

import os
import re

class clsConvertAll:

Massa, Harald Armin

unread,
Mar 5, 2010, 2:17:54 AM3/5/10
to python...@googlegroups.com
Marek,

just checked my code (running in production since ~8months) again, it is using xlrd and is p2exed to a single file.

Two notices:

I am using the idiom:

import xlrd
xlrd.open_workbook(dateiname)

(that's out of laziness, because in 2 years I can't remember where open_workbook is from)

and:

put some "xref":1

in your py2exe-options, that shows you what is referencing what.

AAAAndd:

I have improved my success with py2exe by 2000% with totally deleting the build-directory between 2 consecutive py2exe-builds, so my setups ususally start with:

import jpath, os,sys
p1=jpath.path(os.path.abspath(__file__))
p=p1.dirname().joinpath("build").joinpath("bdist.win32")
if p.exists():
    print "-"*23
    print "deleting build Directory"
    p.rmtree()
    print "-"*23

jpath is the path.py module from jason orendorff.

Best wishes,

John Machin

unread,
Mar 5, 2010, 2:29:37 AM3/5/10
to python...@googlegroups.com
On 5/03/2010 6:06 PM, MaReK Penguin_007 Olsavsky wrote:

> import xlrd
> from xlrd import open_workbook

As Harald said, don't do that. Delete the
"from xlrd import open_workbook"
and where ever in that module you use
"something = open_workbook(..."
change it to
"something = xlrd.open_workbook(..."

John Machin

unread,
Mar 5, 2010, 2:36:24 AM3/5/10
to python...@googlegroups.com
On 5/03/2010 6:17 PM, Massa, Harald Armin wrote:

> I have improved my success with py2exe by 2000% with totally deleting
> the build-directory between 2 consecutive py2exe-builds

Agreed; for some unknown reason py2exe seems to have trouble working out
what's out of date and what's not. Run your build twice; still using old
code -- delete build directory, run build again, it uses latest code.
Mystery.

MaReK Penguin_007 Olsavsky

unread,
Mar 5, 2010, 8:29:33 AM3/5/10
to python...@googlegroups.com
Dne 5.3.2010 8:29, John Machin napsal(a):

Hello,
i've tried it, but result vas the same.
In second i've tried merge both, of my program, classes into one file,
with same result:


"""
Traceback (most recent call last):
File "frmMain.py", line 13, in <module>
ImportError: No module named xlrd
"""

I've tried to reduce setup py tu minimal version:


#! /usr/bin/python
# -*- coding: utf-8 -*-

from distutils.core import setup
import py2exe

setup(windows=["frmMain.py"])

And rewrote initial imports:


#system imports
from Tkinter import *
import tkMessageBox, tkFileDialog
import os, fnmatch
from time import localtime, strftime

#other imports
import Pmw
import xlrd

#from xlrd import *

Same result. Is really possible make distribution exe with "xlrd"?

Thank You All,
MaReK

John Machin

unread,
Mar 5, 2010, 10:02:53 AM3/5/10
to python...@googlegroups.com

Yes really possible. A couple of years ago I was regularly creating over
10 exes from the one setup.py file ... each needed xlrd or xlwt or both.
No problems. No party tricks needed. Just avoided "from <module> import
<thing>".

Have you followed Harald's advice to (1) """put some "xref":1


in your py2exe-options, that shows you what is referencing what."""

(2) delete the build directory first?

Try this:

Make a little app that does this:

#! /usr/bin/python
# -*- coding: utf-8 -*-

import xlrd
print xlrd.__file__

and run it using python in the same directory where you run the py2exe
setup. What does it print? Try to run it as a py2exe console app. What
happens?

MaReK Penguin_007 Olsavsky

unread,
Mar 8, 2010, 4:50:30 AM3/8/10
to python...@googlegroups.com
Hello,

Dne 5.3.2010 16:02, John Machin napsal(a):


>
> #! /usr/bin/python
> # -*- coding: utf-8 -*-
> import xlrd
> print xlrd.__file__
>
> and run it using python in the same directory where you run the py2exe
> setup. What does it print? Try to run it as a py2exe console app. What
> happens?
>

thank You, that helped.
I've used to install xlrd and xlwt from "*.exe" files, and there were
bad pathes. After installed with setup.py from sources package all works OK.
I'd like to know why program worked after start from console
("c:\python26\python.exe frmMain.py"), but after "compilation" doesn't.
Thanks for help and patience.
MaReK

Reply all
Reply to author
Forward
0 new messages