SQLAlchemy is not imported when running FastAPI - python

I am creating a website that gathers data from various websites and display them.
Everything seems to work fine. But when I try to run a server by uvicorn server:erver --reload, it returns
ModuleNotFoundError: No module named 'sqlalchemy'
When I pip freeze, the list of installed libraries are:
anyio==3.6.2
beautifulsoup4==4.11.1
certifi==2022.12.7
charset-normalizer==3.0.1
fastapi==0.89.1
FastAPI-SQLAlchemy==0.2.1
greenlet==2.0.1
idna==3.4
pydantic==1.10.4
PyMySQL==1.0.2
requests==2.28.2
sniffio==1.3.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.46
starlette==0.22.0
typing_extensions==4.4.0
urllib3==1.26.14
As you can see, SQLAlchemy is installed. When I run the code, the code is running fine too. Here is my code:
server.py
from fastapi import FastAPI
import sqlalchemy
server = FastAPI()
#server.get('')
async def home():
return 'hi'
print('hi')
What could be a possible issue in this case? Google has not helped me well so far.

Related

"No module named google.cloud" error when Python script is imported into a test, but not when run directly

Not sure if this is more google-cloud-related or pytest-related. See files below.
When I run either python app/my_script.py or python -m app.my_script, the script runs fine.
But when I run pytest, the line in the script from google.cloud import vision throws "ModuleNotFoundError: No module named 'google.cloud'".
I have tried unsuccessfully to add various package names into the requirements.txt file and/or run pip install google-cloud and pip install google-cloud-language with and without --upgrade flags. What steps can I take to overcome this error?
conftest.py: (empty)
requirements.txt:
google-cloud-vision
app/my_script.py:
from google.cloud import vision
from google.cloud.vision import types
def new_client():
client = vision.ImageAnnotatorClient()
return client
if __name__ == "__main__":
client = new_client()
# etc...
test/test_my_script.py:
from app.my_script import new_client
# tests here...

I can't run my python code -> ModuleNotFoundError: No module named 'flask_sqlalchemy'

I don't know why my code doesn't work, I Import the library in the first line
from flask_sqlalchemy import SQLAlchemy
Knowing that I've already install flask_sqlalchemy
If it is a flask app you should run the app in virtualenv, otherwise simply running by python appName.py will give that error.
The other way is "set FLASK_APP=appName.py" then run flask run.
The first thing I'd check would be can you import the whole flask_sqlalchemy module:
import flask_sqlalchemy
If that works, it could be a case or spelling issue as that command works in my local environment.
It seems like you installed the wrong package. If you are using python3 you have to use python3 -m pip install <module>

Google Compute Engine firebase is not a module

trying to use the VM as a server to host some python code but it has a problem with the files import of 'firebase'
Output: ImportError: No module named firebase
Has anyone had this or anything like this before?
The file which I'm trying run the app from is (serveme.py):
from flask import Flask, request, render_template
from firebase import firebase
import json
import requests
import os.path
firebase = firebase.FirebaseApplication('https://***********.firebaseio.com/')
app = Flask(__name__)
#app.route('/')
def index():
return 'Method was %s' % request.method
#app.route('/firetest', methods=['GET', 'POST'])
etc etc. It has a problem with the import at line 2.
I am using gunicorn to do
gunicorn -w 2 -b :5000 serveme:app
You have to run
sudo easy_install pip
then you're able to do
sudo pip install requests
sudo pip install python-firebase
Python was already installed, apparently you have to install pip again, brew doesn't like multiple downloads of the same package. So use the easy install for just pip.
Hope this helps someone else if they ever come across this.

cannot import name 'Flask'

Im using window 10, I did the command pip install flask but I kept getting a ImportError: cannot import name 'Flask'. When I worked with flask couple months back it was running fine. Came back to run my old programs today and i get this error? I was just trying to run a simple html website.
from flask import Flask, render_template
from flask import request
app = Flask(__name__)
app.static_folder = 'static'
#app.route('/')
def index():
return render_template('index.html')
if __name__=='__main__':
app.run(debug = True)
Also before this error I had No module named 'Flask' so I did(found this in other stackoverflow post):
1. virtualenv
2. pip install flask (getting output that requirements are already satisfied)
3. Then I just try to run my flask which is called i.py and I get cannot import name 'Flask'. Went through many solutions on here still no idea how to fix it.
Basically i had 2 versions of python 3.6 and 3.5.2 when I deleted 3.6 its working fine.

Python Tornado: ImportError: No module named 'tornado'

This is my tornado file::
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop
from tornado.web import FallbackHandler, RequestHandler, Application
from flasky import app
class MainHandler(RequestHandler):
def get(self):
self.write("This message comes from Tornado ^_^")
tr = WSGIContainer(app)
application = Application([
(r"/tornado", MainHandler),
(r".*", FallbackHandler, dict(fallback=tr)),
])
if __name__ == "__main__":
application.listen(5000)
IOLoop.instance().start()
Basically I'm running a flask server in Tornado. But I'm getting this error:
from tornado.wsgi import WSGIContainer
ImportError: No module named 'tornado'
I've already gone through this post: Python Tornado: WSGI module missing?
But my file is not named Tornado.py so that doesn't apply to me.
Please help.
A common problem is having multiple Python interpreters, or multiple Python environments, installed. "pip" and "python" may use different environments. Try installing Tornado like this:
python -m pip install tornado
I got rid of this by using following command.
sudo python3 -m pip install tornado
I faced the issue while testing tornado for the first time. It was because I named the file as tornado.py (as also mentioned by Mohamed Abdelijelil). I renamed it to tornado_test.py and it worked.
check if tornado module is installed with pip and if you are using a virtualenv check if it's activated

Categories

Resources