(Note: I have found a workaround, as stated at the bottom, but it seems like the wrong way to go about this.)
views.py
and forms.py
are both in
R:\\\\jeffy\\\\programming\\\\sandbox\\\\python\\\\django_files\\\\tutorial\\\\django_test\\\\django_test\\\\
and
R:\\\\jeffy\\\\programming\\\\sandbox\\\\python\\\\django_files\\\\tutorial\\\\django_test\\\\
is in Django's "Python Path"
But when attempting
from forms import MyRegistrationForm
in views.py
, it results in an ImportError ... No module named 'forms'
.
I changed it to
from django_test.forms import MyRegistrationForm
but that results in SyntaxError ... invalid syntax
.
Since
R:\\jeffy\\programming\\sandbox\\python\\django_files\\tutorial\\django_test\\django_test
is a package (it has the empty __init__.py
file in it), I thought I could refer to django_test
in this way. (It's also the Django project name.)
I can manually add
R:\\\\jeffy\\\\programming\\\\sandbox\\\\python\\\\django_files\\\\tutorial\\\\django_test\\\\django_test\\\\
to the PYTHONPATH
environment variable (Windows 7, 32 bit, Django 1.7c2), and get it to work. But since this implies that every Django project must be added to the PYTHONPATH
, it seems like it should be able to work without having to do this.
Am I correct? Is there a way to import forms.py
from views.py
(which is in the same directory) without having to manually add its directory to the PYTHONPATH
?
Thanks.
If it's in the same directory, try a relative import:
from .forms import MyRegistrationForm
EDIT: Someone posted this same suggestion as a comment already. I didn't see the comment.
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.