Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...