Error with xlrd and open_woorkbook - python

I´m starting programming with python and I keep receiving the same error in this program:
import xlrd
import numpy as np
import matplotlib as plt
file_location = " X:\ \blabla.xlsx"
import workbook
wb=xlrd.open_workbook(filename= 'blabla.xlsx')
Traceback (most recent call last):
File "", line 1, in
AttributeError: module 'xlrd' has no attribute 'open_workbook'
The paths are well written and I don´t have more than one xlrd module, which I looked up trough help function:
help(xlrd)
Help on package xlrd:
NAME
xlrd
PACKAGE CONTENTS FILE
(built-in)

I had the same problem when I named a test python file as xlrd.py which is the same name as the module. I changed the name of my file and it worked.

I had the same problem, which was, as I figured out, due to the linux permissions. Running pip3 as root, the installer made the contents of the package only visible for the root user. Strangely, the command
import xlrd
did not report any error, just nothing has been imported. The shell commands
sudo chmod -R go+r /usr/local/lib
sudo find /usr/local/lib -type d -execdir chmod go+x {} +
solved the issue.

AttributeError: module 'xlrd' has no attribute 'open_workbook'
It means that open_workbook was not recognized as a method (i. e. a function) - what you wanted - but as an attribute (i. e. an variable).
Methods have opening parenthesis ( after them while attributes don't.
So something is bad - your real code is probably different from the code in your question, because in your question you have ( after the name open_workbook.

Related

how to import modules from local repository with virtualenv and pip

I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")

Python can't find PyGObject/gi even though it is installed

I have trouble setting up the Python-Path for it, I don't really know what to do here. I use elementary OS, which should be similar enough to Ubuntu.
My code so far is this:
#!/usr/bin/python3
import gi
gi.require_version('GTK', '3.0')
When I run the script, the system reports that there is no module named "gi".
However, when I try
sudo apt-get python3-gi
I get told that it is installed already.
I tried
print(sys.path)
and
locate python3-gi
which gave me back some directories. But there were too many, so I don't know what, if this is at all the right way, to to copy where.
Addendum:
Okay, so doing type export PATH="$PATH:/usr/local/bin/python-gi3" yields export is a shell builtin
bash: type: PATH=/home/ge0rg/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/bin/python3-gi: not found
uname -r yields 3.19.0-51-generic.
In the meantime, I also tried reinstalling python3-gi, to no avail. Also, since python3-gi seems to b located in /usr/lib/python3/dist-pacakges, I tried going into python and doing sys.path.append("/usr/lib/python3/dist-pacakges") where the above mentioned folder was not included before. However, after doing this import gi gives the same error as before: >>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
EDIT: Also, locate python3-gi yields:
`/usr/share/doc/python3-gi
/usr/share/doc/python3-gi-cairo
/usr/share/doc/python3-gi/changelog.Debian.gz
/usr/share/doc/python3-gi/copyright
/usr/share/doc/python3-gi-cairo/changelog.Debian.gz
/usr/share/doc/python3-gi-cairo/copyright
/usr/share/lintian/overrides/python3-gi
/var/lib/dpkg/info/python3-gi-cairo.list
/var/lib/dpkg/info/python3-gi-cairo.md5sums
/var/lib/dpkg/info/python3-gi-cairo.postinst
/var/lib/dpkg/info/python3-gi-cairo.postrm
/var/lib/dpkg/info/python3-gi-cairo.shlibs
/var/lib/dpkg/info/python3-gi.list
/var/lib/dpkg/info/python3-gi.md5sums
/var/lib/dpkg/info/python3-gi.postinst
/var/lib/dpkg/info/python3-gi.postrm
/var/lib/dpkg/info/python3-gi.prerm
/var/lib/dpkg/info/python3-gi.shlibs`
however, /usr/lib/dist-packages/gi also exists, but locate gi just gives back a whole array of mostly unrelated results.
I am guessing its installed in /usr/local/bin/python-gi3 . If you see it there, open your shell and type type export PATH="$PATH:/usr/local/bin/python-gi3" to set the path

python "ImportError: cannot import name urandom"

Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?
As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.

Python: Submodules Not Found

My Python couldn't figure out the submodules when I was trying to import reportlab.graphics.shapes like this:
>>> from reportlab.graphics.shapes import Drawing
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
from reportlab.graphics.shapes import Drawing
ImportError: No module named shapes
I have copied the reportlab package to /site-packages and I can import module reportlab.graphics successfully.
My Python version is 2.7.3.
Could anyone help me to fix this problem?
As #dan-boa pointed out, you can add paths to the module search path, but since you can find the parent module, I doubt that this is your root problem.
Do you have some left-over installation of the module at another path? You can check the path where it is finding the parent package (reportlab) by executing:
import reportlab
print reportlab.__file__
If this is indeed the path you were expecting, then try this recursively with the the sub-modules, until you can see where the problem is. Perhaps, your package is corrupted? Try manually checking in the path returned if you can find the files/modules in question.
If this is not the path you were expecting, clean-up the installation from this 2nd path and try again.
Finally, in case you do find that it is a path problem, instead of adding the path each time using sys.path.append, you can add it to PYTHONPATH
Please check your sys path and if the directory of the module is not present then add it.
import sys
sys.path.append('PATH_OF_THE_MODULE')
As site-packages is already their in the sys.path, may be therefore the package was imported successfully.

What is import gv in python?

I installed easy_install under windows, and installed pygraph but the commad import gv in the example taken from here doesn't work:
What is gv lib? what import gv does?
I get this error :
Traceback (most recent call last):
File "C:\Python27\graph.py", line 11, in <module>
import gv
ImportError: No module named gv
All the other imports works fine
Try to install libgv-python with your package manager. On Ubuntu:
sudo apt-get install libgv-python
This could have multiple problem sources:
While installing, something went wrong and the module couldn't be installed.
You haven't set your Python Path correctly.
#second example: The module pygraph.readwrite could exists, but it's possible that it hasn't got a submodule called dot.
Of course there are sure other possibile problems, but I think this would be the most likely.
Edit: Have a look at this. Looks like it's the same problem as yours.
For the second problem eventually this discussion may also help.
This seems to be a common issue with python-graph, see the discussion at http://code.google.com/p/python-graph/issues/detail?id=15. gv is GraphViz and apparently something with the place or the bindings of this library is wrong. You might have to modify sys.path, but it might be even more troublesome on Windows.
From python-graph's "Issue 15: import gv":
It appears that graphviz for windows has no python bindings, or they are not
included with the installer, and not available elsewhere.
Furthermore, I can find no mention of libgv-python (the library which gv is from, I
think) ever being available on windows.
You can workaround the absent gv.py by using the command line tools directly.
Assuming you're trying the example code, remove these lines:
sys.path.append('..')
sys.path.append('/usr/lib/graphviz/python/')
sys.path.append('/usr/lib64/graphviz/python/')
import gv
And remove these lines:
gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','europe.png')
And add these lines instead, making sure the path to dot.exe is correct, or is otherwise in your PATH:
f = open('europe.dot', 'a')
f.write(dot)
f.close()
import os
command = '"C:\\Program Files\\Graphviz 2.28\\bin\\dot.exe" -Tpng europe.dot > europe.png'
print command
os.system(command)
os.system('europe.png')
Module gv corresponding to graphviz-python a python binding of graphviz
See : http://www.graphviz.org/Home.php

Categories

Resources