I'm a relatively new python developer trying to install an older version of flask and am having installation issues.
Background Context:
- I am trying to download this older version of flask in order to run benchmark HTTP tests against this version of python (this version of flask is the one we use in production at work for older web apps)
- I am not as experienced in the python ecosystem as I am with other languages and their package managers
- using python v 3.7
What I tried:
mkdir myproject
cd myproject
python3 -m venv venv
. venv/bin/activate
requirements.txt file:
Flask==1.0.2
# I was hoping installing flask this way would correctly install compatible dependencies but `flask run` command keeps failing
pip install -r requirements.txt
webapp.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
Running flask run
causes this error:
raceback (most recent call last):
File "/usr/local/bin/flask", line 5, in <module>
from flask.cli import main
File "/Library/Python/3.8/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/Library/Python/3.8/site-packages/jinja2/__init__.py)
What I am not trying to do:
- I am not trying to use a new version of flask, I'd like to use 1.0.2 specifically
CodePudding user response:
It seems like this version of flask depended on a module from jinja2 named 'escape', which newer versions of jinja don't have anymore. The best solution would be to downgrade your jinja package so that it can be compatible with flask. Try any version 2.xx.xx, and learn more about jinja changes here: https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-1-0
CodePudding user response:
Flask 1.0.2 is quite old, so you'll need to pin a number of requirements to the old versions so that they're all compatible. pip
doesn't do this for you because it only does basic dependency resolution, so you're running into compatibility issues by using a Jinja2 version that's too new (it may install flask 1.0.2
but will try to install the latest versions of other requirements). Similarly, you'd run into issue with the other dependencies like itsdangerous
and MarkupSafe
because their newer versions removed or changed functionality.
For example, here's a configuration that works, as a requirements.txt
, for your specific version of Flask:
click==8.1.3
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==2.1.2
However, if you want to mirror the environment you're using for your production apps, you should get get their requirements.txt
or dump a pip freeze
of the code, which will generate the list of modules with their exact versions.
Here's an example for my test app for your code:
> pip freeze
click==8.1.3
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==2.1.2
And with your code from your webapp.py
, I can run it with flask run
and have it do the right thing...
# make our virtual environment for testing
> python3 -m venv .venv
> source .venv/bin/activate
# install requirements
> pip install -r requirements.txt
# pinned versions are all installed
> flask run
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000 (Press CTRL C to quit)
# hit localhost:5000
> curl localhost:5000
<p>Hello, World!</p>%