Python module import error in django

In my Django project, the following line raises an ImportError: "No module named elementtree".

  from elementtree import ElementTree 

      

However, the module is installed (i.e. I can launch an interactive python shell and type in the exact line without importing an ImportError) and the directory containing the module is on PYTHONPATH. But when I access any page in the browser, it somehow can't find the module and throws an ImportError. What could be causing this?

+2


source to share


3 answers


Can you import elementtree

to django shell:

python manage.py shell

      

Assuming you have multiple python versions, and don't know which one is used to run your site, add the following to your view and click python_ver

into your template, it will show you the Python version you are using:



import sys
python_ver = sys.version

      

You can also explicitly add the path to elementtree programmatically in settings.py

:

import sys
sys.path.append('path to where elementtree resides')

      

+5


source


I also ran into cross platform issues where ElementTree was available from different modules on different systems ... this ended up for me:

try:
    import elementtree.ElementTree as ET
except:
    import xml.etree.ElementTree as ET

      



May or may not help you ...

+1


source


Go to the installation directory

Example:

C: \ Python26 \ Lib \ site-packages

And check if both elementtree and django are in there.

If they don't both exist, you probably have multiple installation directories for different Python versions.


Anyway, you can fix your problem by running the following command:

installing python setup.py

Run it twice, once inside the django download and once inside the elementtree download. It will install both downloads to whatever your current python is by default.

Links:

0


source







All Articles