UPDATE:
The error (in this particular case) was not caused by circular import, but by flaw in virtualenv
configuration. See my answer below, for elaboration.
I am using:
I am building a web app using Flask, among other things I need an ability to send mails to users. I have built a separate Python module, which will be responsible for mail handling. Though I've encountered a strange (as it seems to me, at least) import
issue, after I added the email-processing module to my app.
Here is the (insulated) import
issue I've encountered:
app.py
from flask import Flask
from test_mail import EmailTool
app = Flask(__name__)
@app.route('/')
def index():
return 'Testing!'
test_mail.py
from email.message import EmailMessage
class EmailTool(object):
pass
After launching my app and going to index (i.e. /
) I am receiving:
Traceback (most recent call last):
File "/app.py", line 2, in <module>
from tmp_test_mail import EmailTool
File "/test_mail.py", line 1, in <module>
from email.message import EmailMessage
ImportError: cannot import name EmailMessage
I've changed code for test_mail.py, in order to make sure email
module is accessible:
import email
class EmailTool(object):
pass
This way I do not get an error.
Searching for possible causes&solutions led me to believe (1, 2, 3, 4, 5), it most likely has something to do with circular reference. Though even after reading through all the mentioned materials and insulating the problem cause, I still can not see how is it a circular reference. So I am concluding that either it is not circular and the cause lies in something else, or it is circular and I am missing something obvious here.
I am asking for help in understanding the following:
- Is the case presented above considered to be a circular reference? (If yes, in what way it is actually circular)?
- Why am I getting an error when I do
from email.message import EmailMessage
but don't get an error if I doimport email
instead?