Forums

Having a really hard time using flask_mail

I have no problem running my code on my local machine, however, if I try to host it using Python Anywhere I run into issues when trying to use flask_mail module.

I have a lot of files, but I'll try to limit it to what is useful.

This is my routes.py file that I am running on my local machine.

from flask import render_template, url_for, flash, redirect
from crhs_web import app, contactmail
from crhs_web.forms import RegistrationForm
from crhs_web.scripts import gene_expression
from flask_mail import Message

mail = contactmail.mail
mail.init_app(app)

# All routes below here.....

Here is my contactmail.py file located in the same directory as my routes.py

from crhs_web import app
from flask_mail import Mail

mail = Mail()

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = 'email@gmail.com'
app.config["MAIL_PASSWORD"] = 'password'

When running this, I get this error message.

2018-12-11 21:26:19,841: Error running WSGI application
2018-12-11 21:26:19,841: ModuleNotFoundError: No module named 'flask_mail'
2018-12-11 21:26:19,842:   File "/var/www/tkellough_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-12-11 21:26:19,842:     from run import app as application  # noqa
2018-12-11 21:26:19,842: 
2018-12-11 21:26:19,842:   File "/home/tkellough/mysite/Teaching_Website/run.py", line 1, in <module>
2018-12-11 21:26:19,842:     from crhs_web import app
2018-12-11 21:26:19,842: 
2018-12-11 21:26:19,842:   File "/home/tkellough/mysite/Teaching_Website/crhs_web/__init__.py", line 6, in 
<module>
2018-12-11 21:26:19,842:     from crhs_web import routes
2018-12-11 21:26:19,842: 
2018-12-11 21:26:19,843:   File "/home/tkellough/mysite/Teaching_Website/crhs_web/routes.py", line 2, in 
<module>
2018-12-11 21:26:19,843:     from crhs_web import app, contactmail
2018-12-11 21:26:19,843: 
2018-12-11 21:26:19,843:   File "/home/tkellough/mysite/Teaching_Website/crhs_web/contactmail.py", line 2, in 
<module>
2018-12-11 21:26:19,843:     from flask_mail import Mail
2018-12-11 21:26:19,843: ***************************************************
2018-12-11 21:26:19,843: If you're seeing an import error and don't know why,
2018-12-11 21:26:19,843: we have a dedicated help page to help you debug: 
2018-12-11 21:26:19,844: https://help.pythonanywhere.com/pages/DebuggingImportError/

However, if I comment out the flask_mail sections (like below) I can run the website without a problem. I just lose the features of my contact form.

from flask import render_template, url_for, flash, redirect
from crhs_web import app#, contactmail
from crhs_web.forms import RegistrationForm
from crhs_web.scripts import gene_expression
# from flask_mail import Message

# mail = contactmail.mail
# mail.init_app(app)

# All routes below here....

I'm really at a loss for what I need to do. My guess is an issue with my virtual environment? But I can't figure out how to ensure I'm doing the right thing. My virtual environment works fine on my local machine. It's location is

/home/tkellough/mysite/Teaching_Website/venv

I've tried editing my virtual environment path to that and I get this error.

Warning: No virtualenv detected at this path. Do you need to create it?

I've tried following the directions on creating a second virtual environment, but still not able to figure it out. If worse comes to worst, I can run the website without using the contact form since everything else works, I just hate not figuring out this last thing.

Any help would be greatly appreciated! I feel very lost and this is the first website I've ever made, but trying to learn everything and organize all my files in the proper locations is a challenging concept for me.

how did you install flask mail on pythonanywhere? (show us the exact steps you followed?) in particular how did you create your virtualenv etc.

Thank you for replying back. I tried a couple of different ways. I used Pycharm to create my virtual environment and uploaded that folder into pythonanywhere. I tried to link the folder and kept getting the error message saying there was no virtualenv detected.

I also tried creating a virtual environment by going to my command line and typing:

python -m venv venv
venv\Scripts\activate
pip install flask-mail
(installed rest of what I needed here)

Then loading that folder into python anywere at /home/tkellough/.virtualennvs

Still had the same issue.

However, I did end up figuring out an alternative method this morning to my problem. Instead of using flask-mail I switched to smtplib and it seems to be working fine. Unless there is a good reason to try to solve the flask-mail issue and give up the smtplib, I think I'll just keep it the way it is.

You cannot, in general, move a virtualenv between machines. It looks like you are starting on a Windows machine (blackslashes in paths) and that means that a virtualenv created there will not work on PythonAnywhere. You need to use the tools provided by pip to re-create the virtualenv on PythonAnywhere.

Oh, that makes sense. Sorry, this is all new information to me (like I said, first website), so I really appreciate the help.

When you say "use the tools provided by the pip to re-create the virtualenv" does that mean opening up a bash command inside /home/tkellough/.virtualenvs and making a new one there following pythonanywhere's directions on their help pages? I can figure out how to do the commands, but knowing where I need to enter them at isn't very clear to me.

Yes, that's exactly right. Anything you would do in a command prompt on your Windows machine will have an equivalent in a bash console.