During installation of Django, why do I keep getti

2019-07-23 04:36发布

问题:

I'm just trying to get a "hello world" using django. I've downloaded django onto my desktop and i have python with Idle in my applications folder. I've followed the instructions on https://www.djangoproject.com/download/ but when i try to import django in Idle I get ImportError: No module named django

I've looked around and have tried sys.path which yields ['', '/Users/nicholastzikas/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']

I've also seen PYTHONPATH mentioned a few times, but I have no idea how to use this. Any help is much appreciated.

回答1:

Probably the path where django got installed to is somehow not in any of the paths listed in your sys.path. Normally it should be inside of site-packages. What you could try is the following:

1) Search for the dir where django was installed on your system.

2) once you find it, you can quickly test if that is the problem, adding in IDLE the path with sys.path.append('/path/to/django/dir') and then try import django again.

3.a) If that worked, you can manually move the path to '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages' for example.

3.b) If that doesn't work, well, then maybe the django files are corrupted (try installing again) or maybe the versions of your installed python and django mismatch?



回答2:

Two suggestions. First, if you haven't restarted IDLE since you installed django, try quitting IDLE (Command-Q) and restarting it. If that doesn't help, chances are that you inadvertently installed Django to another Python instance, probably one of the Apple-supplied system Pythons. Just try it again: quit IDLE again then, from a new Terminal shell window, install Django again but make sure you are using /usr/local/bin/python2.7 rather than just python.