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

Asked

Viewed 125 times

5

Good morning, a few months ago I started with python I’m using it to do tests on android applications. I have the following problem importing the xml.parsers.expat. when running straight into 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

That is the mistake:

 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 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 looked 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’s in the right path, so I don’t know where the bug is, because running straight on the terminal works yes, but using subprocess.call does not work.

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

EDIT: Code where subprocess.call is 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 by creating 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 on the terminal: python call-import-Expat.py, it works well and displays 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 that line

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

for this:

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

I changed python by python3. But Python by default is 2.7. I don’t understand why it works with Python3.

  • Put the part of the code that calls the method subprocess.call.

  • @Dener I added the code to the question. Thank you

1 answer

0

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

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

The part where you say your code runs normally with python 3 and not with 2 may be because your Python code has been written in the way that is standard for this version.

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

try:
    import xml.parsers.expat
except:
    print(ImportError)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.