Error importing xml.parsers.expat in Python 2.7 only works with Python3

4

Good morning, a few months ago started with python I'm using it to do tests in android applications. I have the following problem importing xml.parsers.expat . when I run directly on the terminal it works. More if I run using subprocess.call(import-expat.py, shell=True) does not work and gives an error.

This is the script (import-expat.py):

#! /usr/bin/env python
import xml.parsers.expat

This is the error:

 File "/opt/u_script_files/import-expat.py", line 2, in <module> import xml.parsers.expat
  File "/usr/lib/python2.7/xml/parsers/expat.py", line 4, in <module> from pyexpat import *
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: XML_SetHashSalt

I checked the dependencies and in my understanding this is all right:

$ ldd /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so 
linux-vdso.so.1 =>  (0x00007ffd9b392000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1608877000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f16084b2000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f1608288000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1608ca6000)

I thought the file libexpat.so.1 did not exist and I would look for it

$ apt-file search libexpat.so.1
libexpat1: /lib/x86_64-linux-gnu/libexpat.so.1
libexpat1: /lib/x86_64-linux-gnu/libexpat.so.1.6.0

But it does exist, and it is in the correct path, so I do not know where the error is, because running directly on the terminal works yes, but using subprocess.call does not work.

Additional data: S.O: Ubuntu 14.04 x86_64 x86_64 x86_64 GNU / Linux I appreciate your answers.

EDIT: Subprocess.call code used:

user_path_script = '/opt/u_script_files/import-expat.py'
   for index, line in enumerate(listdevtotest):
     emulatorid = listdevtotest[index][0]
     subprocess.call(user_path_script + ' ' +  emulatorid, shell=True)

I did a local test I created a "call-import-expat.py" script

#! /usr/bin/env python
import subprocess
subprocess.call('import-expat.py', shell=True)

and another script "import-expat.py":

#! /usr/bin/env python
import xml.parsers.expat
print "imported expat"

If it runs in terminal: python call-import-expat.py, it works fine and shows the text "imported expat".

But if the script is called via web (php) it does not work. On the web it is called as follows:

$cmd = 'python /path/script/call-import-expat.py';
$proc = popen($cmd, 'r');
pclose($proc);

NEW EDIT:

I just edited this line

 $cmd = 'python /path/script/call-import-expat.py';

by this:

 $cmd = 'python3 /path/script/call-import-expat.py';

I changed python by python3. But Python by default is 2.7. I do not understand why it works with Python3.

    
asked by anonymous 28.12.2015 / 14:44

1 answer

0

Izabel, have you tried to change the name of your file?

Sometimes python 2.X recognizes it as the same function file!

Now the part where you say your code runs normally with python 3 and not with 2, may be because your Python code was written in the way that is the default of this version.

If you want to test, this code is for you to check if your code imports the module and if it does not, it will respond with an error on your screen!

try:
    import xml.parsers.expat
except:
    print(ImportError)
    
03.01.2016 / 14:50