Search code examples
pythondjangoelementtreepython-module

Error importing a python module in Django


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

  from elementtree import ElementTree 

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


Solution

  • Can you import elementtree within the django shell:

    python manage.py shell
    

    Assuming you have multiple python versions and do not know which one is being used to run your site, add the following to your view and push python_ver to 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 programatically in your settings.py:

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