I'm beginning to learn python and here I'm trying to read from an xml file using ElementTree:
import sys
from elementtree.ElementTree import ElementTree
doc = ElementTree(file="test.xml")
doc.write(sys.stdout)
However I get this error:
File "my_xml.py", line 2, in
from elementtree.ElementTree import ElementTree
ImportError: No module named elementtree.ElementTree
I do have lib files in /usr/lib/python2.6/xml/etree/...
What am I doing wrong?
Thanks a lot for your help :)
It should be:
from xml.etree.ElementTree import ElementTree
More information on this can be found at the Python docs.
Related
Hi I'm using ebooklib in python and I'm getting this error
ModuleNotFoundError: No module named 'ebooklib'
I have successfully installed this library and it appears in my pip3 packages list.
I'm importing this in my python file like this
import os
import logging
import sys
import json
import traceback
from ast import literal_eval
from htmlparse import MyHTMLParser
from ebooklib import epub
I don't know what's wrong.
I had the same issue & fixed it by adding:
> export PYTHONPATH="/usr/local/lib/python3.8/site-packages:$PYTHONPATH"
to my .bash_profile file.
To see where your ebooklib is installed to
print(ebooklib.__file__)
I'm getting at error when trying to import from qtgui site-package. Here's an excerp from the code that's having and error.
from gnuradio import qtgui
on of the files in sitepackage qtgui is textparser which contains the statement:
from stdlib import read_file
That's where the error is thrown.
Following the traceback the problem is that on of the files in qtgui, called textparser.py tries to import read_file from stdlib.
Here is the traceback of the offending line.
File "/usr/lib/python2.7/site-packages/qtgui/textparser.py", line 2, in <module>
from stdlib import read_file
ImportError: No module named stdlib
The line "from stdlib import read_file" looks correct except i cant find any information about a package called stdlib for python. However stdlib sounds like the c library. I'm new to Python, is that the proper way to import a c library?
So am I missing a python package or module, or a c library?
By the way I'm using python2.7, and Centos 7. I can't use Python 3 because I'm using GNUradio which i don't think supports Python 3.
Thanks in advance.
there is no such library in stdlib in python there is a library called stdlib_list what you gonna supposed to do in your code put a code sample
The answer in How do I find the location of Python module sources? says just import it and print its __file__. But my question is that I cannot import a library cv2 while it returns ImportError: libcudart.so.7.5: cannot open shared object file: No such file or directory, so I cannot get its __file__ too. I want to find where does Python import this library so that I can check what is wrong with the library.
Try:
import imp
imp.find_module('cv2')
I am trying to create a shortcut that execute on the selection this simple python code.
It just run a function from the library Biopython.
#!/Users/USERNAME/Library/Enthought/Canopy_64bit/User/bin/python
from Bio.Seq import reverse_complement
import sys
print reverse_complement(sys.stdin.read().rstrip())
But I get this error:
ImportError: No module named Bio.Seq
It does not make sense to me since if I run from the terminal
$ /Users/USERNAME/Library/Enthought/Canopy_64bit/User/bin/python
>>> from Bio.Seq import reverse_complement
The library is imported without any problem.
What do I do wrong? How do I tell gedit where to look for the library?
It's a problem with your path.
This should work:
#!/Users/USERNAME/Library/Enthought/Canopy_64bit/User/bin/python
import sys
sys.path.append('*yourpath of Bio module*')
from Bio.Seq import reverse_complement
print reverse_complement(sys.stdin.read().rstrip())
I installed lxml on a mac and trying to use it in my code and I get errors importing tostring and tounicode. Python cannot see it at all. Any ideas what I am doing wrong?
Here is the code that is causing problems -
from lxml.etree import tostring
from lxml.etree import tounicode
I get an unresolved import error
Also my IDE (eclipse) is able to see the init.py file for lxml.etree module. Here is what it sees ---
# this is a package
def get_include():
"""
Returns a list of header include paths (for lxml itself, libxml2
and libxslt) needed to compile C code against lxml if it was built
with statically linked libraries.
"""
import os
lxml_path = __path__[0]
include_path = os.path.join(lxml_path, 'includes')
includes = [include_path, lxml_path]
for name in os.listdir(include_path):
path = os.path.join(include_path, name)
if os.path.isdir(path):
includes.append(path)
return includes
Thanks for any help.
EDIT:
The only log I see is
Unresolved import: tostring
Unresolved import: tounicode
And when I add the following line before the import of tostring it works no errors --
import etree from lxml
Also to give you some more background on what I am trying to do. I got the readability code from here (https://github.com/buriy/python-readability) and trying to use it in my project.
EDIT2: I fixed the problem but still do not understand why. I wanted to use the code from the readability project directly without installing the package using easy_install. The idea was step into the code so that I understand what its doing. But when I copy the code into my project I get the above mentioned error in the readability code. If I install the package using easy_install then all I can simply import the class exported by readability and use it.
So can some one tell me what the difference is between using the code directly and the package installed? What is a .egg file? How to create one?
In lxml's code, it dynamically load modules. That makes IDE fails to analyze reference as IDE just analyzes raw code.
I've found this solution extremely useful when trying to resolve the resource and import etree in my Intellij IDEA Python project:
Have a look at lxml tutorial which suggests this solution:
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")