I cannot use any library because I always get this error
I installed modules several times still doesnt work
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask.ext.bcrypt import Bcrypt
from flask_mysqldb import MySQL
debug = True
app = Flask(__name__)
bcrypt = Bcrypt(app)
cors = CORS(app)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = ''
mysql = MySQL(app)
#app.route('/register', methods=['POST'])
def register():
print(request)
if __name__ == '__main__':
app.run()
Traceback (most recent call last):
File "/_____/_____/_______/________/server/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
import(module_name)
File "_____/_____/_______/________/server/app.py", line 2, in
from flask_cors import CORS
ModuleNotFoundError: No module named 'flask_cors'
I think you might have used pip to install flask-cors but looks like you are using python3. So you need to use pip3, in my opinion.
pip3 install -U flask-cors
If you have already tried pip3 then I have underestimated your problem. Apologies.
Related
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.
I am trying to use the flask_socketio library, following the documentation: https://python-socketio.readthedocs.io/en/latest/intro.html#what-is-socket-io
I have a file called socketio.py, and tried using their sample code:
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
if __name__ == '__main__':
socketio.run(app)
I get this error when trying to run it through python3 ($ python3 socketio.py)
Traceback (most recent call last):
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
File "/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py", line 21, in <module>
import socketio
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
ImportError: cannot import name 'SocketIO' from partially initialized module 'flask_socketio' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py)
When I tried using flask run to run it , I get this error
Error: While importing "socketio", an ImportError was raised:
Traceback (most recent call last):
File "python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
File "/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py", line 22, in <module>
from socketio.exceptions import ConnectionRefusedError # noqa: F401
ModuleNotFoundError: No module named 'socketio.exceptions'; 'socketio' is not a package
I have tried uninstalling socketio and reinstalling and flask-socketio as there seems to be issues if you have both installed, but it still continues to give the same error.
I can see you have named your program socketio.py. This is conflicting with socketio module used by flask_socketio. Rename your filename and try running.
I am new to the python and flask. I am watching a you tube tutorial and i am stuck at 20:05 where he run the code "from app import db". I follow exactly the step he taught but yet I still face this problem.Below are the program I type and at the terminal there it shows the error.
The Program:
from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False) #This is to set the character the person input
date_created = db.Column(db.DateTime, default=datetime.utcnow) #This is to set the time
def __repr__(self):
return '<Task %r>' % self.id
#app.route('/')
def index():
return render_template('index.html')
if __name__ =="__main__":
app.run(debug=True)
The Program I type In Terminal:
from app import db
The Respond After Typing The Command At Terminal:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Visual Studio Code Project\app.py", line 1, in <module>
from flask import Flask, render_template, url_for
ModuleNotFoundError: No module named 'flask'
The You Tube Link: https://www.youtube.com/watch?v=Z1RJmh_OqeA&t=335s
The Program
Can anyone help me please?
"flask" is used as a Python module in VSCode, we need to install it in the current Python environment: please use "pip install flask" (or "pip3 install flask") in the VSCode terminal.
In addition, the module "flask_sqlalchemy" also needs to be installed. (pip install flask_sqlalchemy)
For more information about the use of "flask" in VSCode, please refer to Flask Tutorial in Visual Studio Code.
Update:
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 am not able to run my python program that executes big query.
My python version: 3.6.0
My pip version: 19.3.1
Traceback (most recent call last):
File "app.py", line 2, in <module>
from bqservice import query_service
File "C:\work\python-bigquery\bqservice\query_service.py", line 1, in <module>
from google.cloud import bigquery
File "C:\work\python-bigquery\env\lib\site-packages\google\cloud\bigquery\__init__.py", line 35, in <module>
from google.cloud.bigquery.client import Client
File "C:\work\python-bigquery\env\lib\site-packages\google\cloud\bigquery\client.py", line 50, in <module>
import google.cloud._helpers
File "C:\work\python-bigquery\env\lib\site-packages\google\cloud\_helpers.py", line 33, in <module>
from google.protobuf import duration_pb2
File "C:\work\python-bigquery\env\lib\site-packages\google\protobuf\duration_pb2.py", line 5, in <module>
from google.protobuf import descriptor as _descriptor
File "C:\work\python-bigquery\env\lib\site-packages\google\protobuf\descriptor.py", line 47, in <module>
from google.protobuf.pyext import _message
ImportError: DLL load failed: The specified procedure could not be found.
But the same code works well when I dockerize it and run it as any service like cloud run etc.
I have referred the post: ImportError: DLL load failed: The specified module could not be found
But it did not work
Here is my code:
from flask import Flask, request, jsonify
from bqservice import query_service
# Init App
app = Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return "Home page"
#app.route('/match/<id>', methods=['GET'])
def get_a_project(id):
match_summary = query_service.get_match_details(id)
return match_summary
# This is for debug mode on
if __name__ == '__main__':
app.run( port=8080, debug=True)
My requirements.txt
cachetools==3.1.1
certifi==2019.9.11
chardet==3.0.4
Click==7.0
Flask==1.1.1
google-api-core==1.14.3
google-auth==1.7.1
google-cloud-bigquery==1.22.0
google-cloud-core==1.0.3
google-resumable-media==0.5.0
googleapis-common-protos==1.6.0
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
protobuf==3.11.0
pyasn1==0.4.8
pyasn1-modules==0.2.7
pytz==2019.3
requests==2.22.0
rsa==4.0
six==1.13.0
urllib3==1.25.7
Werkzeug==0.16.0
This was dependencies were populate by pip install as I added only Flask and google-bigquery
My dao:
from google.cloud import bigquery
from os import environ
from baseball import matches
def get_match_details(gameId):
client = bigquery.Client()
query_job = client.query("select * from `bigquery-public-data.baseball.schedules` where gameid= '"+gameId+"'")
results = query_job.result() # Waits for job to complete.
for row in results: # API request - fetches results
requested_match = matches.Matches(row["homeTeamName"], row["awayTeamName"], row["dayNight"], row["startTime"], row["attendance"], row["duration"])
return requested_match.get_details()
Any help?
It seems that your internal package 'bqservice' is calling a not installed package making you to see that error message.
Some people solved this issue by installing the package protobuf
Some other found some incompatibilities with their Tensorflow version.
You don't experience this issue when running the code on a GCP resources because, most of the time, those packages are installed by default.
Hope this is helpful.