1

I'm having issues using xlrd. I'm using Fedora 17.

I ran

python run.py

and I got the error

ImportError: cannot import name open_workbook

I've been Googling this for quite a while and haven't found a solution. It's getting a bit frustrating.

From what I've found on Google, I think it has something to do with Python looking for xlrd and finding a conflicting file/folder. If that's the case, I'm guessing it has something to do with how I installed xlrd. First, I believe I downloaded an rpm file (I can't find the exact place I downloaded it from). After I did that, my file was running fine but then for some reason I decided I needed to install xlrd from a more reputable source. So I went to http://www.python-excel.org/ and used the links there to download it. It installed fine, but this is when I started receiving the import error.

If possible, I'd like to uninstall xlrd completely from my computer and start fresh. If that will help or not, I don't know. But if somebody could point me in the right direction that would be wonderful.

Here's my code:

from xlrd import open_workbook

wb = open_work('week 1/AFROTC SP13 Eagles Sqaudron Weekly Attendance.xls')

LLab = {}
TuesPT = {}
ThursFriPT = {}

for s in wb.sheets():
    if s.name == "LLab":
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row,col).value)
            if values[0] != "end":
                if values[1] == "0" or values[1] == "1":
                    LLab[values[0]]=int(values[1])

    elif s.name == "TuesPT":
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row,col).value)
            if values[0] != "end":
                if values[1] == "0" or values[1] == "1":
                    TuesPT[values[0]]=int(values[1])

    elif s.name == "ThursFriPT":
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row,col).value)
            if values[0] != "end":
                if values[1] == "0" or values[1] == "1":
                    ThursFriPT[values[0]]=int(values[1])

print "LLab"
for key in LLab:
    print key,
    print LLab[key]

print
print

print "TuesPT"
for key in TuesPT:
    print key,
    print TuesPT[key]

print
print

print "ThursFriPT"
for key in ThursFriPT:
    print key,
    print ThursFriPT[key]
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
Dave
  • 503
  • 1
  • 8
  • 21
  • Yes, edited the original post – Dave Jan 27 '13 at 04:50
  • Have you tried uninstalling/reinstalling using `sudo apt-get remove python-xlrd` and then `sudo apt-get install python-xlrd`? Sorry, was away for a few hours. – RocketDonkey Jan 27 '13 at 06:55
  • Okay, what if you open an interpreter and type `import xlrd` and then `dir(xlrd)`? Basically, we'll first check and see if what you're importing as `xlrd` is in fact the module. If it doesn't contain `open_workbook`, we'll know where to go :) – RocketDonkey Jan 27 '13 at 06:59
  • When I execute the first command I get the same error. When I execute the second command I get an error saying 'xlrd' is not defined – Dave Jan 27 '13 at 07:01
  • Okay, so `xlrd` isn't being found at all. When you install using `sudo apt-get install python-xlrd`, what is the output? Does it say it installed successfully, and if so where? – RocketDonkey Jan 27 '13 at 07:03
  • Yes, it is installed successfully. How do I check where it is installed? – Dave Jan 27 '13 at 07:05
  • When I uninstall and reinstall mine, it goes to `/usr/local/lib/python2.6/dist-packages/xlrd`. – RocketDonkey Jan 27 '13 at 07:07
  • Next thing we'll check is your `PYTHONPATH` to ensure that the directory to which you're saving is actually available. – RocketDonkey Jan 27 '13 at 07:09
  • It's being installed in `python2.7/site-packages/xlrd/` – Dave Jan 27 '13 at 07:09
  • Okay, so in an interpreter, type in `import sys` and `sys.path` (or even `for p in sys.path: print p`) - do you see `site-packages`? – RocketDonkey Jan 27 '13 at 07:11
  • And the error you get when you `import xlrd` is `Cannot import name xlrd`? – RocketDonkey Jan 27 '13 at 07:14
  • No, it's `cannot import name open_workbook` – Dave Jan 27 '13 at 07:15
  • Sorry, I meant just `import xlrd`, on its own. And can you paste the full traceback you are getting? Every line if possible. – RocketDonkey Jan 27 '13 at 07:16
  • Absolutely, here it is: `>>> import xlrd Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/site-packages/xlrd/__init__.py", line 312, in import sheet File "/usr/lib/python2.7/site-packages/xlrd/sheet.py", line 19, in from formula import dump_formula, decompile_formula, rangename2d File "/usr/lib/python2.7/site-packages/xlrd/formula.py", line 13, in import copy File "copy.py", line 1, in from xlrd import open_workbook ImportError: cannot import name open_workbook >>>` – Dave Jan 27 '13 at 07:19
  • Okay, we are getting much closer :) In your current directory (the one from which you are running this script), is there another file called `copy.py`? This includes the file itself. – RocketDonkey Jan 27 '13 at 07:22
  • I hope this doesn't throw a wrench into the works...but no, there isn't – Dave Jan 27 '13 at 07:24
  • Eh, minor wrench :) In your interpreter, type `import copy` and then `copy.__file__`. Do you see something like `/usr/lib/python2.7/copy.pyc`? The issue here is that something is overriding the built-in `copy` module, which is not what we want to happen. – RocketDonkey Jan 27 '13 at 07:25
  • Yeah, sorry - should have specified that :) – RocketDonkey Jan 27 '13 at 07:27
  • Ok, I think I'm getting somewhere. So actually, the file I'm working on is called `run.py`. I do have another file called `copy.py` in my folder. Here are the contents: `from xlrd import open_workbook wb = open_workbook('test.xls') LLAB_Attendance = [] PT_Attendance = [] POC_Worktime = [] POC_Worktime = [] Make_up_tracker = [] LOC_tracker = [] PT_Incentive = [] for s in wb.sheets(): if s.name == "LLAB_Attendance": for row in range(s.nrows): for col in range(s.ncols): LLAB_Attendance.append(s.cell(row,col).value) print LLAB_Attendance[row][col],` – Dave Jan 27 '13 at 07:36
  • When I run the terminal, I import `copy.py` with no problems, and then `run.py` with no errors – Dave Jan 27 '13 at 07:36
  • 1
    Good stuff - change the name of that file to `something_other_than_copy.py` :) – RocketDonkey Jan 27 '13 at 07:36
  • I cannot believe that was it. Boy, do I feel stupid – Dave Jan 27 '13 at 07:37
  • Haha, does it work? And no need to feel stupid - `copy` isn't generally something you use every day, so it is easy to overlook it when naming a file. – RocketDonkey Jan 27 '13 at 07:38
  • Yes it does. But I still feel stupid haha. I have been racking my brain for HOURS...not just a few hours, but at least 6 hours trying to figure this out. You are my hero. If you answer my original post, I'll upvote it and accept it as the correct answer (I think that's the right gesture for showing my appreciation...I am a noob on this site, so I could be wrong) – Dave Jan 27 '13 at 07:41
  • Ha, sure - I'd be very surprised if you are the only person that has happened to (when working with spreadsheets, copying is pretty common, so why not have a module of copying utilities called `copy`? :) ). – RocketDonkey Jan 27 '13 at 07:42
  • No kidding. Well, hopefully I'll be able to accomplish what I was originally trying to do – Dave Jan 27 '13 at 07:43

1 Answers1

2

Per our discussion in the comments above, the end reason was that there was a file named copy in the directory itself. This caused an issue because the formula module in the xlrd package was trying to import copy, but instead of the built-in version it was importing the locally named copy, causing the error.

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84