Pydev and Django: Shell doesn't find certain modules?
I am developing a Django project with PyDev in Eclipse. PyDev's Django Shell did a great job for a while. Now this is not the case:
>>> import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
C:\Python26\python.exe 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
>>>
>>> from django.core import management;import mysite.settings as settings;management.setup_environ(settings)
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.settings
>>>
Dev server works fine. What could I be doing wrong?
The model module is also noticeably absent:
>>> import mysite.myapp.models
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.myapp.models
On a regular command line, outside of PyDev, the shell works fine.
Why might this be happening?
a source to share
Sounds like a simple path problem. What result:
import sys; print sys.path
I don't know anything about PyDev, but there is probably a setting in there to add paths to the PYTHONPATH setting. If not, you can do it right in the shell:
sys.path.insert(0, '/path/to/directory/containing/mysite/')
a source to share
I had a similar problem with this a while ago, moving my project out of Django 1.3 and having a settings.py file at the root of my source and then moving it to my application.
For example, what happened was that I had the following:
rootOfSource / - settings.py - myapp
and I changed it like:
rootOfSource / - myapp - myapp / settings.py
and I also changed my settings file like this:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
However, when I debugged in os.eviron I found that DJANGO_SETTINGS_MODULE was not as expected, and then I changed the manage.py file like this:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
os.environ.__setitem__("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Which then allowed me to run from PyDev.
Hope it helps.
a source to share
