How can I get more diagnostic result in "no module named xyz" error?
I have a django project in my local directory which works fine when I run it from my local django server as in
python manage.py runserver.py 8000
But if I clone the local git repository where is this project (and which is current), for example:
git clone my_django_project my_django_project_clone
and I try to run the same command from the clone directory, the server crashes immediately:
Error: No module named xyz
The server does not produce any other output at all, even when I run it with the flag -v 3
.
Is there a way to get more diagnostic information as to why this (presumably) identical clone of the django production site is failing?
source to share
This is how I approach the problem
-
Run Python interactive prompt
python -vvv # -vvv notes to print out import trace information
-
Enter
import xyz
If this works, it means the module is a valid Python installation. If it doesn't work with Django, then it's probably a circular import issue and a problem with the Django init code itself. Django does a lot behind the scenes when it reads a settings.py
file, walks through all Django apps, etc., so looping imports often happens that Python can't load a module. It could also be the case where Django swallows the original exception ImportError
and doesn't print the nested exception, which is actually when Django is not initialized correctly. In this case, manual tuning import xyz
should show a real error.
-
If the module does not load, 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 the directories where Python is loading modules. Your module may not be in any of these directories, so you need to make sure that
A) You add your own modules to the Python sys.path list, or use the PYTHONPATH environment variable correctly to load your own packages. The settings path can depend on the Django version, Python version, and how Django is started. For example, the current working directory in which you are executing a command manage.py
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 are installing your third party packages correctly. The correct way to install third party Python packages is using the Pythonpip
sandbox tool virtualenv
for your development project. If you install Python packages for a system-wide Python installation ( /usr/lib/python
on UNIX), especially using sudo pip
, the result is not guaranteed.
source to share
You probably need to install dependencies. It is common to store dependencies inside .txt requirements and run "pip install -r requirements.txt" when cloning a project. To push dependencies to require.txt, you use "pip freeze> requirements.txt". This way, if you are using a PIL project or six or whatever, that will be added to this file and then you should be able to install them with one command. I hope I haven't missed your question.
source to share