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?
Related
So, I have a mac on High Sierra and I am trying to import and use an api. This is api is a python3 api and uses bs4, and specifically is using lxml within bs4 in order to parse a webpage.
However, I am having an issue getting bs4 to recognize that I have lxml installed on my machine. I have installed both of them using pip, and both appear to have installed correctly. I can write a program with ‘import bs4’ and ‘import lxml’ at the top and it compiles and runs perfectly fine. However, no matter what I do I always get the following error when I run a program using this api.
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
On top of this, when I run the following code
import lxml
import bs4
print(bs4.builder.builder_registry.builders)
the output is
[<class 'bs4.builder._htmlparser.HTMLParserTreeBuilder'>]
With no lxml listed.
I have tried everything I have found on the various stack overflow threads related to this. I have uninstalled and reinstalled both lxml and bs4 through various methods(pip, easy install, manually installing, homebrew). I've manually linked lxml from brew. And other things ive probably forgotten. However I cant get it to work.
Anyone have any ideas/has anyone gone through this before. Its possible I'm completely missing something small or stupid, since I've never messed with bs4 before, but I dont know.
I'm not exactly sure what's the cause, but I had a similar case with a Flask app I'm working on. I worked around it by importing bs4 locally in the function where I needed it.
One symptom I had was when I logged bs4.builder.builder_registry.builders at the top of my module, the logs ended up showing two entries: first with the proper builders and then with only HTML.
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.
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
I wanted to install urllib2 package from PyPI but it is not available.
It seems that it has been updated to urllib3, but is there any way to download urllib2 ?
import urllib2
Is that what you want?
If you find any library under http://docs.python.org/ you can always import without installing it.
Update 1:
If you need the source code...
The official Cpython code: http://hg.python.org/cpython/file/3b5fdb5bc597/Lib/urllib
Note The urllib2 module has been split across several modules in
Python 3 named urllib.request and urllib.error. The 2to3 tool will
automatically adapt imports when converting your sources to Python 3.
or try this? http://code.reddit.com/docs/urllib2-pysrc.html
I can't guarantee the integrity for the second alternative link.
Then how do I import that? I run everything in python 2.4, but one of my scripts import xml.etree.ElementTree...which is only Python 2.5
Consider upgrading to Python 2.5 (or, dare I say, 2.6!-) one of these decades... the times, they are a'changin'! Meanwhile, for ElementTree in particular, you might manage to back-patch it, but...
Come gather 'round people
Wherever you roam
And admit that the waters
Around you have grown
And accept it that soon
You'll be drenched to the bone.
If your time to you
Is worth savin'
Then you better start swimmin'
Or you'll sink like a stone
For the times they are a-changin'.
And if hearing it in great music helps you, feel free... but you'd better start swimmin', or you'll sink like a stone...!-)
Then it fails.
You can't import a python 2.5 library while you're running python 2.4. It won't work.
Why can't you run python 2.5+?
You should be able to install ElementTree for Python 2.4. It is in debian under the package name python2.4-elementtree (and ..-celementtree)
If you don't find a package for your OS, you can install it right from Source.
This type of problem is not all that unusual, as not everyone has control over the version of Python that they use. A few years ago I had this very problem as most of my work took place on a Windows PC, but at one point we needed to run it on a large UNIX box which was subject to strict change control, meaning that I couldn't install Python 2.5. I think even now that box may be running Python 2.4.
Looking back on my solution, it wasn't particularly elegant, but here it is...
while True:
try:
from xml.etree.cElementTree import *
break
except ImportError:
pass
try:
from xml.etree.ElementTree import *
break
except ImportError:
pass
try:
from cElementTree import *
break
except ImportError:
pass
from ElementTree import *
break
This was part of a module called xmlutils.py which was used by the rest of my code rather than importing xml.etree.ElementTree (or whatever) directly.
Edit: I should point out that cElementTree and ElementTree were the names for xml.etree.cElementTree and xml.etree.ElementTree before they became part of the standard library.
Now you can install separately the elementtree in Python 2.4, just go here http://effbot.org/zone/element-index.htm and download elementtree.