unresolved import tostring in lxml.etree - python

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")

Related

Python error, Can't import from stdlib error

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

How do I find the location of Python module sources while I can not import it?

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')

Having an issue creating an exe with py2exe and script importing xlrd

My goal is to create a python script that loops over cells of an excel document. This is my python script called reader.py, and it works just fine.
import xlrd
import os
exceldoc = raw_input("Enter the path to the doc [C:\\folder\\file.xlsx]: ")
wb = xlrd.open_workbook(exceldoc,'rb')
.... some code....
The problem I'm encountering is attempting to use py2exe to create an executable so this script can be used elsewhere.
Here is my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
setup(name='Excel Document Checker',console=['reader.py'])
I run the following command: python setup.py py2exe
It appears to run fine; it creates the dist folder that has my reader.exe file, but near the end of the command I get the following:
The following modules appear to be missing
['cElementTree', 'elementtree.ElementTree']
I did some searching online, and tried the recommendations here Re: Error: Element Tree not found, this changing my setup.py file:
from distutils.core import setup
import py2exe
import sys
from glob import glob
options={
"py2exe":{"unbuffered": True,"optimize": 2,
'includes':['xml.etree.ElementPath', 'xml.etree.ElementTree', 'xml.etree.cElementTree'],
"packages": ["elementtree", "xml"]}}
setup(name='Excel Document Checker',options = options,console=['reader.py'])
I'm now getting an error:
ImportError: No module named elementtree
I'm sort of at an impasse here. Any help or guidance is greatly appreciate.
Just some information - I'm running Python 2.6 on a 32 bit system.
You explicitly told setup.py to depend on a package named elementtree here:
"packages": ["elementtree", "xml"]}}
There is no such package in the stdlib. There's xml.etree, but obviously that's the same name.
The example you found is apparently designed for someone who has installed the third-party package elementtree, which is necessary if you need features added after Python 2.6's version of xml.etree, or if you need to work with Python 1.5-2.4, but not if you just want to use Python 2.6's version. (And anyway, if you do need the third-party package… then you have to install it or it won't work, obviously.)
So, just don't do that, and that error will go away.
Also, if your code—or the code you import (e.g., xlrd) is using xml.etree.cElementTree, then, as the py2exe FAQ says, you must also import xml.etree.ElementTree before using it to get it working. (And you also may need to specify it manually as a dependency.)
You presumably don't want to change all the third-party modules you're using… but I believe that making sure to import xml.etree.ElementTree before importing any of those third-party modules works fine.

How to fix Python error importing ElementTree?

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.

web2py external libraries

how can i import other external libraries in web2py? is it possible to
load up libs in the static file?
can somebody give me an example?
thanks
peter
If the library is shipped with python, then you can just use import as you would do in regular python script. You can place your import statements into your models, controllers and views, as well as your own python modules (stored in modules folder). For example, I often use traceback module to log stack traces in my controllers:
import traceback
def myaction():
try:
...
except Exception as exc:
logging.error(traceback.format_exc())
return dict(error=str(exc))
If the library is not shipped with python (for example, pyodbc), then you will have to install that library (using distutils or easy_install or pip) so that python can find it and run web2py from the source code: python web2py.py. Then you will be able to use regular import statement as described above. Before you do this make sure you installed the library properly: run python interpreter and type "import library_name". If you don't get any errors you are good to go.
If you have a third party python module or package, you can place it to modules folder and import it as shown below:
mymodule = local_import('module_name')
You can also force web2py to reload the module every time local_import is executed by setting reload option:
mymodule = local_import('module_name', reload=True)
See http://web2py.com/book/default/section/4/18?search=site-packages for more information.
In web2py you import external library as you normally do in Python
import module_name
or
from module_name import object_name
I am not sure what you mean by "in the static file"

Categories

Resources