For the last couple of weeks I have programmed a flask server. I was adviced to use virtualenv to make sure that all my python dependencies are easily tracked. However, now I have deployed it on my test server and it gives me the following error:
from flask import Flask
ImportError: No module named flask
Which is about the first python module that I have installed. Is there something I am doing wrong here? Virtualenv should've taken care for this, right? How can I check for these kind of errors during development?
Related
I am trying to learn the basics of apis and flask restful, and was trying to run the minimal api in the flask documentation.
I installed flask and flask-restful into the scripts folder in the virtual environment using command prompt, and my ide picked up that these modules are installed.
However, having saved the script, when I try to run the python file in command prompt as an http server, it comes back with the following error:-
ModuleNotFoundError: No module named 'flask_restful'
As far as I can tell, the module is definitely actually there (since py-charm makes it pretty clear if a module referred to in the code is not actually installed).
So two questions - what is going on? And more importantly, how do I fix it?
(PS - the above is with respect to windows command prompt)
My code for reference
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello':'world'}
api.add_resource(HelloWorld,'/')
if __name__ == '__main__':
app.run(debug=True)
Do you have flask-restful installed on your system? If not just use pip for installation, and type in your command prompt this command: pip install flask-restful
When I run my app locally, everything works, but when I try to deploy to Heroku, I get this error:
ModuleNotFoundError: No module named 'stripe'
I've read some other answers to similar issues and it seems like maybe my virtual environment is running a different version of Python than the one on Heroku, or something along those lines?
My Python version is 3.6.5
I do have a runtime.txt file with the following in it, which I thought was setting the python version for my Heroku app:
python-3.6.5
I read this SO thread, but I didn't really understand the part about the PYTHONPATH - Importing Stripe into Django - NameError
If anyone can help steer me in the right direction or has an idea of what I could try, I'd really appreciate it!
Thanks!
Add stripe to requirements.txt file in the root of your repository. You can read more about it here.
I'm using flask-restful and wanted to use bcrypt from passlib to hash users passwords stored in database, when I run the solution by python3 app.py, everything works perfectly and I can hash the passwords and validate them , but when I run the solution through uwsgi uwsgi uwsgi.ini I get internal server error when I try to authenticate.
When I review the log file from passlib.hash import bcrypt ImportError: No module named 'passlib' , although I can import it successfully from command line as well.
I tried to do one solution suggested in :
No module named 'passlib'
and to add "##UnresolvedImport" to import but still didn't work.
After not getting any answer , I was struggling for couple of days, afterward I just deleted the virtual environment , created a new one and re-installed all required libraries "flask , flask-restful , flask_bcrypt...etc , and even uwsgi ".
Afterward I could start the application with uwsgi without getting the ImportError.
I recently got a server through DigitalOcean and I'm playing around with a Flask App.
The app works perfectly fine on my computer when I run it locally. But it doesn't work when hosted on DigitalOcean. Both Virtual Environments are the same.
When I do 'pip freeze', the output is:
coverage==4.0.3
dominate==2.1.16
Flask==0.10.1
Flask-Bootstrap==3.3.5.7
Flask-Login==0.3.2
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
gunicorn==19.4.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.11
visitor==0.1.2
Werkzeug==0.11.3
wheel==0.24.0
WTForms==2.1
But I get an error when I look in Apache error log:
ImportError: No module named flask.ext.bootstrap
I'm not sure why it can't find it. Like I said, it works fine on my computer when I run it locally.
I also tried 'from flask_bootstrap import Bootstrap' instead of 'from flask.ext.bootstrap import Bootstrap'. Both works locally, but neither work on DigitalOcean server.
Any suggestions?
Please check how you configured Apache. It's probably using the system default Python without your virtual env.
When making Flask Apps using digitalocean, this tutorial is a lot more up to date and simpler than the one I posted above:
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04
I did the process again, but this time using gunicorn and nginx and found the overall process a lot easier.
I am trying to get sentry running on my dreamhost server. Dreamhost uses passenger wsgi to serve python frameworks, like django. I am able to get django apps running.
I am using virtualenv and I install sentry using pip, so all the code for sentry sits under the virtualenv directory. The instructions given for sentry doesn't use the startproject to create a directory that you can place somewhere for the passenger_wsgi.py file to find.
The sentry website gives examples of the program running under Nginx and uWSGI, but I am limited to (in this case) to apache and passenger wsgi.
Is it possible to run sentry under dreamhost's configuration and if so how does one pass things in like the config file to get it working. I have been able to locally start and interact with sentry, using :
sentry --config=/home/user/.sentry/sentry.conf.py start
so I know that all the dependencies are present on the host system
OK it looks like I was over thinking it, I forgot from a python perspective the file wsgi.py (which is found in the sentry directory) is called as sentry.wsgi when imported as a module. I was confused by sentry being a module that was downloaded from pip and how to access it. This is the reduced solution that works:
passenger_wgsi.py
import sys, os
INTERP = "/home/user/.virtualenv/sentry2/bin/python"
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
os.environ['SENTRY_CONF'] = "/home/user/.virtualenv/sentry2/sentry.conf.py"
import sentry.wsgi
believe it or not, that's it. If you look at the wsgi file in the sentry directory under the virtualenv install you will see it does all the importing of the django.core.handlers.wsgi and kicks off the correct application.