0

I have this python script:

import xmltodict
with open('file.xml') as fd:
    doc = xmltodict.parse(fd.read())

    print("Name=" + doc['Data']['form'][0]['@Name'])
    print("Time=" + doc['Data']['form'][0]['Period']['@Time'])
    print("\tM-1\tM-2\tM-3\tM-4")
    load_m1 = doc['Data']['form'][0]['Value'][0]['val'][0]['#text']
    load_m2 = doc['Data']['form'][0]['Value'][1]['val'][0]['#text']
    load_m3 = doc['Data']['form'][0]['Value'][2]['val'][0]['#text']
    load_m4 = doc['Data']['form'][0]['Value'][3]['val'][0]['#text']

    memory_m1 = doc['Data']['form'][0]['Value'][0]['val'][1]['#text']
    memory_m2 = doc['Data']['form'][0]['Value'][1]['val'][1]['#text']
    memory_m3 = doc['Data']['form'][0]['Value'][2]['val'][1]['#text']
    memory_m4 = doc['Data']['form'][0]['Value'][3]['val'][1]['#text']

    cpu_m1 = doc['Data']['form'][0]['Value'][0]['val'][2]['#text']
    cpu_m2 = doc['Data']['form'][0]['Value'][1]['val'][2]['#text']
    cpu_m3 = doc['Data']['form'][0]['Value'][2]['val'][2]['#text']
    cpu_m4 = doc['Data']['form'][0]['Value'][3]['val'][2]['#text']

    task_m1 = doc['Data']['form'][0]['Value'][0]['val'][3]['#text']
    task_m2 = doc['Data']['form'][0]['Value'][1]['val'][3]['#text']
    task_m3 = doc['Data']['form'][0]['Value'][2]['val'][3]['#text']
    task_m4 = doc['Data']['form'][0]['Value'][3]['val'][3]['#text']

    print("Load\t" + load_m1 + "\t" + load_m2 + "\t" + load_m3 + "\t"+ load_m4)
    print("Memory\t" + memory_m1 + "\t" + memory_m2 + "\t" + memory_m3 + "\t" + memory_m4)
    print("CPU\t" + cpu_m1 + "\t" + cpu_m2 + "\t" + cpu_m3 + "\t" + cpu_m4)
    print("Task\t" + task_m1 + "\t" + task_m2 + "\t" + task_m3 + "\t" + task_m4 )

If I run it line by line from the python3 console no problem, but if I save it in a file and I run it as

$ python3 script.py

it returns this error:

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/xmltodict.py", line 5, in <module>
    import defusedexpat as expat
ImportError: No module named 'defusedexpat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "xml.py", line 1, in <module>
    import xmltodict
  File "/usr/lib/python3.4/site-packages/xmltodict.py", line 7, in <module>
    from xml.parsers import expat
  File "/home/script.py", line 3, in <module>
    doc = xmltodict.parse(fd.read())
AttributeError: 'module' object has no attribute 'parse'

And if later I want to run it again line by line from the console, this happens:

Python 3.4.3 (default, May  5 2015, 17:58:45)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmltodict
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/xmltodict.py", line 5, in <module>
    import defusedexpat as expat
ImportError: No module named 'defusedexpat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/xmltodict.py", line 7, in <module>
    from xml.parsers import expat
  File "/home/xml.py", line 3, in <module>
    doc = xmltodict.parse(fd.read())
AttributeError: 'module' object has no attribute 'parse'
>>>

I tested this on Ubuntu and cygwin. I installed this way: 'pip3 install xmltodict' under cygwin and in this way under Ubuntu: 'sudo apt-get install python3-xmltodict'

Firefly
  • 449
  • 5
  • 20
  • Make sure the `$: python3` and the version you used on the console are the same. It seems they somehow do not use the same packages... – lennyklb May 02 '16 at 11:57
  • Why are you using cygwin with Ubuntu? cygwin is for doing Unixy things on Windows. – martineau May 02 '16 at 12:21
  • 1
    @LennartKloppenburg I got it, It was the name of the script. "xml.py" How stupid I am!. Now I know that I cann't name the scripts as well. – Firefly May 02 '16 at 17:54

1 Answers1

-1

Try to uninstall xmltodict package with following command:

sudo pip uninstall xmltodict

or

sudo pip3 uninstall xmltodict

And reinstall with following command.

sudo pip3 install xmltodict
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58