I am a newbie to flask and I am developing a small API for my team using Flask.
Earlier when I was using python 3.8 and pip older version ( I am not sure what version that was), my app.py was running fine. But whenever I did a pip install <SOME_PACKAGE_NAME>, I used to get a message stating to upgrade my pip, and so I upgraded it to pip 21.1.1. After upgrading the pip, I also updated python 3.8 to python 3.9 and created a new python virtual environment, and tried installing the below three libraries there that are required for my application:-
Flask
Flask-RESTful
Flask-JWT
But I saw that my flask and other packages have the latest versions, so I tried running my app in the new virtual environment, but it keeps failing. I am adding below the steps along with the failure message and a few other info.
(base) Apples-MacBook-Pro:pythonEnvs testuser$ virtualenv -p python3.9 venvTest
(base) Apples-MacBook-Pro:pythonEnvs testuser$ cd ../Documents/Personal/FlaskProject/src/Flask-Api/
(base) Apples-MacBook-Pro:Flask-Api testuser$ source ~/pythonEnvs/venvTest/bin/activate
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip --version
pip 21.1.1 from /Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/pip (python 3.9)
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ python --version
Python 3.9.5
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install flask
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install Flask-RESTful
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install Flask-JWT
(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ python3.9 app.py
Traceback (most recent call last):
File "/Users/testuser/Documents/githubProjects/FlaskProject/src/Flask-Api/app.py", line 2, in <module>
from flask_restful import Api
File "/Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/flask_restful/__init__.py", line 14, in <module>
from flask.helpers import _endpoint_from_view_func
ImportError: cannot import name '_endpoint_from_view_func' from 'flask.helpers' (/Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/flask/helpers.py)
I am fortunate enough to have the pip freeze from the older pip version, so I pasted that into requirements.txt and do an explicit pip install on it, and am running the application for now on it. But what exactly is the issue with the new Flask and other dependencies version.
Below are the results of pip freeze from older pip and after upgrading and installing libraries in the new virtual environment: -
# Pip Freeze from an older version
aniso8601==9.0.1
click==7.1.2
Flask==1.1.2
Flask-JWT==0.3.2
Flask-RESTful==0.3.8
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
PyJWT==1.4.2
pytz==2021.1
six==1.16.0
# Pip Freeze from the new version ( pip 21.1.1 )
aniso8601==9.0.1
click==8.0.0
Flask==2.0.0
Flask-JWT==0.3.2
Flask-RESTful==0.3.8
itsdangerous==2.0.0
Jinja2==3.0.0
MarkupSafe==2.0.0
PyJWT==1.4.2
pytz==2021.1
six==1.16.0
Werkzeug==2.0.0
Below is my app.py which I am trying to run:-
from flask import Flask
from flask_restful import Api
from flask_jwt import JWT
from security import authenticate, identity
from resources.user import UserRegister
from resources.item import Item, ItemList
# Resources are something our API is concerned with ( let it be students, items etc)
app = Flask(__name__)
app.secret_key = 'flask_application'
api = Api(app)
jwt = JWT(app, authenticate, identity) # JWT creates a new endpoint /auth
api.add_resource(Item, '/item/<string:name>')
api.add_resource(ItemList, '/items')
api.add_resource(UserRegister, '/register')
if __name__ == "__main__":
app.run(port=5000, debug = True) # To debug use (debug = True)
Please do let me know in the comments if anything is required.
( Authenticate and identity are functions used to authenticate users from our DB
UserRegister, Item, and ItemList are classes with some pre-defined attribution required for the respective endpoints)
Any insights into this will be of great help.
Thanks
Flask-Restful was no longer compatible with the recently released Flask 2.0.
A fix was applied some hours ago:
https://github.com/flask-restful/flask-restful/commit/fc9b34c39472284a57c50d94fec5b51fe8d71e14
There is no new release on PyPI yet.
You could either install Flask-Restful via GitHub or wait some time for a new release and meanwhile use your older pinned versions.
Related
I am just a beginner in python.
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel.yaml.
I did install the command pip3 install ruamel.yaml and my python list package shows that it's available.
site-packages % pip3 list
Package Version
------------------ ---------
certifi 2021.10.8
charset-normalizer 2.0.11
idna 3.3
pip 22.0.3
pytz 2021.3
PyYAML 6.0
requests 2.27.1
ruamel.yaml 0.17.21
ruamel.yaml.clib 0.2.6
setuptools 60.9.3
urllib3 1.26.8
Whereas when I am trying to import and use it in the python script it is giving me below error.
Import "ruamel.yaml" could not be resolved
Could someone please suggest how to resolve this.?
When Python cannot find a module you get an error looking like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ruamel.yaml
This might be because you are using some non-standard installation/execution environment, or because you try to run this from an editor. Some of those don't understand the concept of namespaces in Python.
It could also be that pip3 is not installing for the python that you are using to run the program. That is easy to check by installing some other package using pip3 and trying to import that.
In either case make sure you test your program running with the python that corresponds to pip3 ( using type pip3 or which pip3 resp type python ), or install using:
python -m pip install ruamel.yaml
and then run the program with python your_prog.py
I am building a web scraper and I am trying to import the 'requests' package but I am getting an error. I am being told the following:
ModuleNotFoundError: No module named 'requests'
Full Error:
(venv) USERs-MacBook-Pro:Scraper user$ /usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
Traceback (most recent call last):
File "/Users/user/git/ML/Python/Practice/Scraper/Scraper.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Steps I took when setting up project:
python3 -m venv project_name/venv
source project_name/venv/bin/activate
python3 -m pip install SomePackage
Confirmation package and dependences were installed:
(venv) USERs-MacBook-Pro:Scraper user$ pip list
Package Version
---------- ---------
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 20.2.3
requests 2.25.1
setuptools 49.2.1
urllib3 1.26.2
By using the absolute path to system Python like that:
/usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
you are using system's Python 3.9 and the packages that are installed for that one although you are in a virtual environment.
When creating a virtual environment you are creating a separate environment with the specified python version and all the packages you install with pip are installed to this environment and this python version.
You can better understand that if you run:
which python
inside your virtual environment.
This will show you the python that is used when you run python inside your venv and it's going to be different than
/usr/local/opt/python#3.9/bin/python3.9
So, by having installed requests using pip inside your environment, it is installed in the environments python which is executed when you run just python.
To sum up, to use the packages installed inside your venv with pip you should run your script (after activating the venv) with:
python /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
I am working on code from CS50 Beyond... online course available on YouTube By Prof.Brian Yu
This code is from Lecture ORM and API in models.py file.
I don't understand this error
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ export FLASK_APP=application.py
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 application.py
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ flask run
* Serving Flask app "application.py"
* 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
Usage: flask run [OPTIONS]
Error: While importing "application", an ImportError was raised:
Traceback (most recent call last):
File "/home/pglab/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/application.py", line 2, in <module>
from models import *
File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/models.py", line 4, in <module>
from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'
I check all the settings also,
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ pip install Flask
Requirement already satisfied: Flask in /home/pglab/venv/lib/python3.7/site-packages (1.1.1)
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3 -m pip install sqlalchemy
Requirement already satisfied: sqlalchemy in /home/pglab/venv/lib/python3.7/site-packages (1.3.13)
Everything seems fine! I run my flask app many times, but when i started ORM programs in flask things becomes messy....
Can somebody please help me out!
I guess you have installed the SQLalchemy outside the virtual environment.
please install it after activating the virtual environment then only the virtual environment is able to search the modules.
the main reason is the virtual environment scope is limited its not global
please follow below command
source/bin/activate //this will virtual enviroment
sudo pip install Flask-SQLAlchemy==2.4.1 // this will install the packge within your virtual envoronment
To the get the Flask wrapper for SQLAlchemy, do
python3 -m pip install flask-sqlalchemy
instead of
python3 -m pip install sqlalchemy
Or, since you already have SQLAlchemy installed, doing the former will suffice.
I am trying to install nibabel through pip:
pip install nibabel
It installs successfully, however, when I write the following code:
import nibabel as nib
I get this error :
ImportError: No module named 'nibabel'
And when I check in the terminal using:
pip show nibabel
It shows that it exists
Author-email: neuroimaging#python.org
License: MIT license
Location: /usr/local/lib/python3.6/site-packages
Requires: numpy, six
This issue happens in both Python 3.x and 2.x.
Here's what you can do:
If you're using Python 2.7, you might need root permission to install the nibabel module.
Try this:
pip install nibabel --user
Hopefully, you should get this Successfully installed nibabel-2.2.1 message.
Then, run your Python 2.7 environment and:
import nibabel as nib
If this doesn't help, you might want to try virtualenv.
Finally, double check if you meet the nibabel requirements e.g. Python 2.7, or >= 3.4.
I'm having issue with accessing Google Storage through Python 3.6.I'm installing with:
pip install --upgrade google-cloud-storage
Here's my Python script:
from google.cloud import storage
def main():
client = storage.Client()
bucket = client.get_bucket('my_bucket')
blob1 = bucket.blob('my_file.json')
blob1.upload_from_filename(filename='my_file.json')
if __name__ == "__main__":
main()
pip show google-cloud-storage gives me following output:
Name: google-cloud-storage
Version: 1.6.0
Summary: Python Client for Google Cloud Storage
Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
Author: Google Cloud Platform
Author-email: googleapis-publisher#google.com
License: Apache 2.0
Location: /usr/local/lib/python3.6/dist-packages
Requires: google-api-core, google-auth, google-cloud-core, requests, google-resumable-media
Any idea what's wrong here?
There is a chance you are installing it with one python version (E.g. python2) and running your code with another version (E.g. python3).
Try mentioning versions of pip and python in the command.
To be precise, use pip of the python version you wanna run the code with. Example:
python3.6 -m pip install --upgrade google-cloud-storage
There could be multiple versions of Python installed on your system and multiple versions of pip as well, the above will make sure you are using the correct version of both.
I met the same problem and solved by running my script as administrator.