So I've been following along a course which deals with web services and I've encountered an issue with flask dependencies.
my requirements.txt looks like this:
PyMySQL==1.0.2
cryptography==37.0.4
Flask==2.2.2
flask_sqlalchemy==2.5.1
sqlalchemy < 1.4.0
flask_migrate==2.7.0
flask_script==2.0.6
sqlalchemy_utils==0.38.3
And my manage.py which I run with python manage.py db init
looks like this
from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from configuration import Configuration
from models import database
from sqlalchemy_utils import database_exists, create_database
application = Flask(__name__)
application.config.from_object(Configuration)
migrate = Migrate(application, database)
manager = Manager(application)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
if(not database_exists(Configuration.SQLALCHEMY_DATABASE_URI)):
create_database(Configuration.SQLALCHEMY_DATABASE_URI)
manager.run()
When I use flask 2.2.2 I get this error:
Traceback (most recent call last):
File "/home/remax/PycharmProjects/V3/manage.py", line 3, in <module>
from flask_script import Manager
File "/home/remax/PycharmProjects/V3/venv/lib/python3.10/site-packages/flask_script/__init__.py", line 15, in <module>
from flask._compat import text_type
ModuleNotFoundError: No module named 'flask._compat'
But when I go back to flask 1.1.2 I get this
ImportError: cannot import name 'escape' from 'jinja2'
I honestly don't know how to deal with this, I've been trying to find a solution all morning
flask-script has been obsolete since Flask 0.11 added command support. It hasn't been updated to work with Flask 2, so if you want to use Flask 2, you'll need to get rid of it (and use Flask's native command support instead).
If you have upgraded Flask to 2.x and decide to downgrade to 1.x, you will need to install compatible versions of supporting packages (werkzeug, itsdangerous, jinja2). It might be easiest to just uninstall all packages from your virtualenv (you're using one, right?) and reinstall the correct versions, but you can also do it by hand.
Related
I want to set up a flask server with a scheduler using APScheduler.
Unfortunately flask doesn't want to run with APScheduler - it crashes at the imports.
I tried APScheduler==2.1.2 and use:
from flask import Flask
from apscheduler.scheduler import Scheduler
app = Flask(__name__)
I tried also the newest APScheduler==3.6.3 and use:
from flask import Flask
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
In both cases after running flask run I got:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "C:\(...)\Continuum\anaconda3\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "C:\(...)\app\__init__.py", line 2, in <module>
from apscheduler.schedulers.background import BackgroundScheduler
ModuleNotFoundError: No module named 'apscheduler'
I tried installing Apscheduler with pip, pip3 and conda - same results. I tried Flask-APScheduler - same reuslts.
PyCharm recognizes and hints the APScheduler (as well as IPython), but flask doesn't.
Solved. The issue was that I have 2 flasks installed on my computer:
first one is global
second one is for my venv only
The flask run was executing the global one (because in Path environment variable there was only path to this one), but APScheduler is installed within my venv. I deleted the global flask and changed the Path variable to my venv. I don't know, if this is a proper way to solve it, but now it works.
I'm really new to Python and I'm trying to do a tutorial on using flask. When I run the below code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, World!"
I get the following error message:
Traceback (most recent call last):
File "/Users/myname/Documents/PycharmProjects/flask/test_webapp.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask
This doesn't make any sense to me as when I run:
python -m pip list
Flask 1.1.1 is on the list.
I am using Python version 3.7.4.
I realise this questions has been asked before but after carefully reading through I can't find the correct answer. I realise this is quite basic but any help would be really appreciated!
python -m pip list answers in terms of Python 2.x
If you're using Python 3.x, you need to do pip3 install flask
Module Flask is installed for Python 2 (Python), but not for Python 3 (Python3) that you are using.
So, install it as pip3 install Flask
I am taking a webdev with python class and the current lecture involves creating databases with sqlalchemy. I have attached the script. In order for lines 7 and 10 to work, the "models" module needs to be installed. However, when I try to install the module, I get the following error:
Collecting models
Using cached https://files.pythonhosted.org/packages/92/3c/ac1ddde60c02b5a46993bd3c6f4c66a9dbc100059da8333178ce17a22db5/models-0.9.3.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\nikol\AppData\Local\Temp\pip-install-_om8dbe7\models\setup.py", line 25, in <module>
import models
File "C:\Users\nikol\AppData\Local\Temp\pip-install-_om8dbe7\models\models\__init__.py", line 23, in <module>
from base import *
ModuleNotFoundError: No module named 'base'
I have found that models has a newer version called doqu, but I can't seem to get doqu to work with this code. I tried manually installing models but didn't succeed.
from flask import Flask, render_template, request
from models import *
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = ""
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
db.create_all()
if __name__ == "__main__":
main()
You are misunderstanding what your class is asking of you. You are not asked to install the models project (which is a project that was last released in 2010, and as you found, broken, since renamed several times, and is indeed now called Doqu).
You are supposed to create your own models.py module, to store your SQLAlchemy models in; it is a common pattern to use that name in your projects.
I am trying to run a basic Flask app using Python 3.6. However, I get an ImportError: cannot import name 'ForkingMixIn'. I don't get this error when running with Python 2.7 or 3.5. How can I run Flask with Python 3.6?
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 65, in <module>
from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\fsk.py", line 9, in <module>
app.run()
File "C:\Python36\lib\site-packages\flask\app.py", line 828, in run
from werkzeug.serving import run_simple
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 68, in <module>
from socketserver import ThreadingMixIn, ForkingMixIn
ImportError: cannot import name 'ForkingMixIn'
This is fixed as of Werkzeug 0.11.15. Make sure you have installed the latest version of Werkzeug. pip install -U werkzeug.
This is a known issue that was reported to Werkzeug in anticipation of Python 3.6. Until that or another patch is merged and released, Werkzeug's dev server will not run on Python 3.6.
Check if OS can fork before importing ForkingMixIn since Python 3.6 will no longer define that when it is not available on the operating system (python/cpython#aadff9b) and ImportError: cannot import name 'ForkingMixIn' will occur.
In the mean time, you can run your app with an external WSGI server such as Gunicorn.
pip install gunicorn
gunicorn my_app:app
You can wrap your app in the debug middleware if you need the in-page debugger (as long as you only run Gunicorn with one worker).
Is CherryPy broken? I just set it up and tried to use the routes dispatcher but it has an import error, my code is as follows:
import cherrypy
mapper = cherrypy.dispatch.RoutesDispatcher()
The error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jwesonga/environments/cherrypy/lib/python2.6/site-packages/CherryPy-3.2.2-py2.6.egg/cherrypy/_cpdispatch.py", line 463, in __init__
import routes
ImportError: No module named routes
I'm on a Mac and I tried both 3.2.2 and 3.0 using virtualenv for the latter.
I have successfully used CherryPy with the routes dispatcher under OS X.
The error you've shown is:
ImportError: No module named routes
This is pretty clear -- Python can't find the routes modules. Have you installed it? This is not part of CherryPy, it's a separate module that you will need to install. If you're using MacPorts, you should be able to:
port install py-routes
(Or py25-routes or py26-routes depending on which Python you're using). If you're using virtualenv, you can simply run:
easy_install routes