Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get more diagnostic output on a "no module named xyz" error?

Tags:

python

django

I have, in a local directory, a django project that works fine when I run it from a local django server, as in

python manage.py runserver.py 8000

But if I clone the local git repository where this project lives (and which is up-to-date), like this:

git clone my_django_project my_django_project_clone

and I try the same command above from the clone's directory, the server fails immediately:

Error: No module named xyz

The server produces no other output at all, even when I run it with the -v 3 flag.

Is there any way to get more diagnostic information on why this (supposedly) identical clone of a working django site is failing?

like image 740
kjo Avatar asked Sep 28 '22 05:09

kjo


1 Answers

This is how I would approach the problem

  1. Start interactive Python prompt

     python -vvv   # -vvv notes to print out import trace information
    
  2. Type

     import xyz
    

If this works it means the module is valid Python installation. If it doesn't work with Django then it is a probably an issue with circular imports and problem with Django initialization code itself. Django does a lot of behind the scenes magic when it reads settings.py file, scans all Django applications and so on, so circular import is often the case why Python cannot load the module. Also this could be a case that Django swallows the original ImportError exception and does not print the nested exception what actually when wrong when initializating Django. In this case, manually typing import xyz should show you the real error.

  1. If the module loading fails then the next thing is to type

    import sys
    for p in sys.path: print(p)  # print p for Python 2.x
    

This will show all directories where Python pulls in modules. Your module isn't probably in any of these directories so you need to make sure

A) You append your own modules to Python sys.path list or use PYTHONPATH environment variable correctly to load your own packages. Settings up path may depend on Django version, Python version and how Django is run. For example the current working directory where you run manage.py command may or may not be included. If the module is in the current folder you can try this:

   PYTHONPATH=. python manage.py runserver

B) You install your third party packages properly. The proper way to install third party Python packages is using pip tool with virtualenv isolated Python environment for your development project. If you install Python packages to system-wide Python installation (/usr/lib/python on UNIX), especially using sudo pip, the result is not guaranteed to work.

like image 134
Mikko Ohtamaa Avatar answered Oct 29 '22 19:10

Mikko Ohtamaa