I'm trying to use elementtree to parse xml but the import is giving me an error.
No module named etree
I've tried :
from xml.etree import ElementTree as ET
import xml.etree.ElementTree as ET
I have elementtree downloaded in my python library on mac. Am I missing something?
Thank you
ElementTree is a part of the standard Python library, you shouldn't need to install anything to get it to work.
Using a standard install from IdeOne, shows your first line works, with no extra libraries.
from xml.etree import ElementTree as ET
print ET
gives:
Success time: 0.03 memory: 44896 signal:0
<module 'xml.etree.ElementTree' from '/usr/lib/pypy/lib-python/2.7/xml/etree/ElementTree.py'>
Something has gone wrong with your install, or you are trying to use a third party library like lXML.
Alternatively, you have a file named xml.py in your project that is overriding your Python install. You can check this by running the following code in your file before any other imports.
import xml
print xml.__file__
If the path doesn't look like this, and shows a path to your local project thats your problem.
/usr/lib/python2.7/xml/__init__.pyc
Remove any local file named xml and also remove their compiled versions. Python initially searches for xml locally and hence conflict might occur.
Related
I'm following along with Natural Language Processing with Python. when i run
from nltk.etree.ElementTree import ElementTree
I get this error:
ImportError: No module named etree.ElementTree
Why is this?
etree was removed from ntlk back in 2012 (https://github.com/nltk/nltk/issues/80):
StevenBird1 said, at 2011-03-31T05:10:58.000Z:
Yes -- etree has been in the standard library since version 2.5. It
was just a temporary measure to include etree for the benefit of
people using 2.4.
This should work for you:
from xml.etree.ElementTree import ElementTree
Hi i am new to python and i want to understand the following in detail:
I've written a script, say 'foo.py', which uses the python html parser, i.e.
#!/usr/lib/python
from html.parser import HTMLParser # <-- executes ./tokenize.py ?!
...
Accidentally, in the current directory lies an other python script called 'tokenize.py'. By executing foo.py, the import line triggers tokenize.py to be executed as well. I guess the local directory has priority and the html.parser module has a tokenize.py as well...
But what exactly happens?
And what is a proper way to avoid such conflicts in the future?
thx
EDIT: I run python 3.3.2
You are right, modules in the current directory are loaded first.
The proper way to avoid this is always importing your modules by full name. In your case, in html.parser you should import tokenize like this:
from html.parser import tokenize
Instead of:
import tokenize
If html.parser is an external module which you don't control, just rename your tokenize.py to something else, e.g.:
from html.parser import tokenize as ext_tokenize
I'm writing a sublime text 2 plugin that uses a module SEAPI.py which in itself imports the requests module.
Since sublime text 2 uses it's own embedded python interpreter, it doesn't see the requests module installed in my ubuntu machine (I get the following error: ImportError: No module named requests).
Best solution I could find so far was to copy the 'requests' module (the whole directory of files) from /usr/lib/python2.7/dist-packages/requests into my plugin directory in the sublime text packages dir.
But after that, it says that it can't find the 'urllib3' module.
Is there a better way to import the requests module so that I won't have to copy all the files into my plugin directory ?
The current code I'm using is as follows:
MyPlugin.py
import sublime
import sublime_plugin
import SEAPI
...
SEAPI.py
import requests
try:
import simplejson as json
except:
import json
from time import time, sleep
...
Edit:
The selected answer is correct and fixes my main question, but a different problem exists with using the current version of 'Requests' with the embedded sublime text 2 interpreter. ST2's python is missing various modules which exist in regular 2.7 python (such as 'fileio').
I've solved it with using the 'Requests' module from here:
https://github.com/bgreenlee/sublime-github
And I had to edit the 'urllib3/response.py' file to this:
try:
from cStringIO import StringIO as BytesIO
except ImportError:
pass # _fileio doesn't seem to exist in ST's python in Linux, but we don't need it
You need to bundle full requests distribution with your Python package and then modify Python's sys.path (where it looks for modules) to point to a folder containing requests folder.
Download Requests library from a PyPi and extract it manually under your plugin folder
Before importing requests in your plugin, append the corrcet folder to sys.path to point a folder where it can found requests import
The (untested) code should look like something like this:
import sys
import os
# request-dists is the folder in our plugin
sys.path.append(os.path.join(os.path.dirname(__file__), "requests-dist"))
import requests
This also assumes that requests setup.py does not do any hacks when you install the module using easy_install or pip.
You also could import requests zip directly as Python supports importing from ZIP files, assuming requests is distributed in compatible way. Example (advanced):
https://github.com/miohtama/ztanesh/blob/master/zsh-scripts/python-lib/zipimporter.py
More about sys.path trick (2004)
http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html
Mikko's answer is good, but I may have found a slightly easier way:
import MyAwesomePlugin.requests
"MyAwesomePlugin" being the name of your plugin, of course.
I have some code in a module called XMLModel.py that parses an XML object with lxml.etree. When I try importing lxml.etree generically in an IPython or regular Python shell, it works fine. Command line versions of my code all work fine.
But when I try to have Apache execute the code as part of a web page, I get a bizarre import error:
File "/var/www/html/../ws/python-util/src/util/XMLModel.py", line 4, in <module>
import lxml.etree
ImportError: /opt/epd/7.3-2/lib/libxslt.so.1: undefined symbol: xmlXPathCompiledEvalToBoolean
I've searched for this "undefined symbol" problem but cannot make any sense about it. It might have something to do with building a static instead of dynamic version of lxml but I cannot find anything that spells it out clearly.
Has anyone else had this kind of problem specifically within a browser setting? What could make the import suddenly fail when the code is used that way?
My operating system is Red Hat 4.1.2-48. The directory /opt/epd is just where I store the Enthought Python distribution and then also place related modules, .so stuff, etc. It's all very standard.
I want o use Python-docx library to process word files. A docx.py references lxml, as i assume from
from lxml import etree
When i start the script, i get error:
No module named lxml
Is this a standard library? Why is not it referenced properly then? IronPython 2.7 RC1.
You need to install lxml which is not part of the stdlib. I don't know if it will work with IronPython though.
Update: Seems like it might be non-trivial to get lxml working with IronPython. See this question:
How to get lxml working under IronPython?