Reputation: 7530
I have a Django project where in the project's root folder (where manage.py
is) I can do
(venv) MyName-MacBook:mydjangoproject myname$ python
>>> import django
>>> from django.core.mail import EmailMessage, send_mail
>>>
However, when I create a subfolder, this doesn't work anymore
(venv) MyName-MacBook:mydjangoproject myname$ mkdir email && cd email
(venv) MyName-MacBook:email myname$ python
>>> import django
>>> from django.core.mail import EmailMessage, send_mail
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/myname/Documents/MacBook:mydjangoproject/venv/lib/python3.6/site-packages/django/core/mail/__init__.py", line 11, in <module>
from django.core.mail.message import (
File "/Users/myname/Documents/MacBook:mydjangoproject/venv/lib/python3.6/site-packages/django/core/mail/message.py", line 7, in <module>
from email import (
File "/Users/myname/Documents/MacBook:mydjangoproject/email/email.py", line 2, in <module>
from django.core.mail import EmailMessage, send_mail
ImportError: cannot import name 'EmailMessage'
>>>
I realise this is a very basic question, but I am a bit stuck. It's strange that I can import django
, but not the other functions.
Upvotes: 1
Views: 89
Reputation: 1492
Looking at your traceback I see you have a module named email
.
Django tries to import from the builtin module email
and the python interpreter thinks your module is the one to import. Try renaming that module to avoid name collisions and you should be fine
Edit: After your edit the problem becomes very clear. The python interpreter looks at your working directory before the builtins, finds an email.py
and tries to import from that
Upvotes: 2
Reputation: 9977
You have a package named django in your project, don't you :) Don't do that- it will make you import the wrong thing by accident.
Upvotes: 1