简体   繁体   中英

Python: import error message - Django

I am a newbie to python and django, so I was running a code, and I got an error message as such:

File "/home/mbenchoufi/brisket/../brisket/views.py", line 11, in <module>
from influence.forms import SearchForm
ImportError: No module named forms

A really weird thing is that I have a file called forms.py, in the influence folder, and in this file a I have a class called SearchForm.

Here's the print sys.path infos

['/home/mbenchoufi/brisket', '/home/mbenchoufi/src/ckanclient', '/home/mbenchoufi/src/vdm', '/home/mbenchoufi/brisket/src/mediasync', '/home/mbenchoufi/brisket/src/transparencydata', '/home/mbenchoufi/brisket/src/simplepay', '/home/mbenchoufi/brisket/src/feedinator', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
['/home/mbenchoufi/brisket', '/home/mbenchoufi/src/ckanclient', '/home/mbenchoufi/src/vdm', '/home/mbenchoufi/brisket/src/mediasync', '/home/mbenchoufi/brisket/src/transparencydata', '/home/mbenchoufi/brisket/src/simplepay', '/home/mbenchoufi/brisket/src/feedinator', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
  1. You should try export PYTHONPATH='/path/to/your/influence/folder' (or something similar depending on the shell you are using)

  2. Try this stuff in the interactive ipython - if you get it working in there, then it is easy to get it working in the code. This is usually the best way to do, if you are a beginner.

Your project structure does not follow the Django 1.4 docs . Your manage.py file should be one level above of settings and urls. Check that out please.

You Must ensure that influence is in your sys.path.

so that, influence can be imported.

And, in folder influence , there are must a file named __init__.py

for make influence as a package.

add the following code in your views.py

import sys
influence_path = FIND_OUT_INFLUENCE_PATH
sys.path.append(influence_path)

from influence_path.foo import bar
...

When you in a shell do import. As the error shows.

you must set the DJANGO_SETTINGS_MODULE ,


If you still confused,

in the shell, cd your project path, and run the tree command,

then post your project folder tree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM