Having trouble importing nltk.etree.elementtree - python

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

Related

etree import not working- Python 2.7.5

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.

How to include third party Python packages in Sublime Text 2 plugins

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.

Urllb2 package missing from Pypi

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.

Missing lxml module in python?

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?

What if one of my programs runs in python 2.4, but IMPORTS something that requires python 2.5?

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.

Categories