No module named 'flask_restful' according to command prompt - python

I am trying to learn the basics of apis and flask restful, and was trying to run the minimal api in the flask documentation.
I installed flask and flask-restful into the scripts folder in the virtual environment using command prompt, and my ide picked up that these modules are installed.
However, having saved the script, when I try to run the python file in command prompt as an http server, it comes back with the following error:-
ModuleNotFoundError: No module named 'flask_restful'
As far as I can tell, the module is definitely actually there (since py-charm makes it pretty clear if a module referred to in the code is not actually installed).
So two questions - what is going on? And more importantly, how do I fix it?
(PS - the above is with respect to windows command prompt)
My code for reference
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello':'world'}
api.add_resource(HelloWorld,'/')
if __name__ == '__main__':
app.run(debug=True)

Do you have flask-restful installed on your system? If not just use pip for installation, and type in your command prompt this command: pip install flask-restful

Related

Deploying an app on Google Cloud Platform - ImportError for pyzbar

I am deploying an app with Flask that uses the library pyzbar (and more specifically the function pyzbar.pyzbar.decode)
It works perfectly on my machine; but when I deploy it, I have the error: ImportError: Unable to find zbar shared library.
This is because pyzbar.pyzbar runs find_library('zbar') (find_library is from ctypes.util) and nothing is returned.
Do you know what I should do?
Create this files app.yaml, main.py, requirements.txt:
1.app.yaml
runtime: python37
2.requirements.txt
Flask==1.1.1
pyzbar
3.main.py
# [START gae_python37_app]
from flask import Flask
import pyzbar
app = Flask(__name__)
#app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello World!'
Then:
gcloud app deploy -q
gcloud app browse
#Hello Word
Everything worked as expected!
The lib-zbar-dev OS package is not present in the App Engine standard environment and cannot be installed, even if pyzbar is installed. I suggest that you try to use an App Engine flexible custom runtime to install all necessary operating system dependencies. App Engine Flex offers a high level of customization, as this solution uses Debian-based docked images to run your applications.
In this link you can find more information on how to create a custom runtime for App Engine Flex, keep in mind that you need to know Docker.
Another alternative is to use a Compute Engine VM to run your application, this is not a serverless solution, but you can install all the necessary software for your application.

"ModuleNotFoundError: No module named 'django'" when trying to deploy Django server on Azure

After I tried to deploy my Django website on Azure, I got an error saying:
ModuleNotFoundError: No module named 'django'
I added a requirements.txt in the root directory of my Django project, am I missing anything else? I've tried to install Django from Kudu BASH but it gets stuck on "Cleaning Up".
Here is the full error: https://pastebin.com/z5xxqM08
I built the site using Django-2.2 and Python 3.6.8.
Just summarized as an answer for other people. According to your error information, I can see that you tried to deploy your Django app to Azure WebApp on Linux based on Docker. So there are two offical documents will help as below.
Quickstart: Create a Python app in Azure App Service on Linux
Configure a Linux Python app for Azure App Service
The error ModuleNotFoundError: No module named 'django' indicated that there is not django package installed on the container of Azure Linux WebApp.
Due to the content of Container characteristics of #2 document above as below,
To install additional packages, such as Django, create a requirements.txt file in the root of your project using pip freeze > requirements.txt. Then, publish your project to App Service using Git deployment, which automatically runs pip install -r requirements.txt in the container to install your app's dependencies.
So the possible reason is the requirements.txt file not in the corrent path of your project or container after deployed, which path should be /home/site/wwwroot/requirements.txt on the container or the root of your project like the offical sample Azure-Samples/djangoapp on GitHub.
I had the same problem. requirements.txt was in my repository, but randomly I started getting the same error ModuleNotFoundError: No module named 'django' after changing a setting or restarting the service or something. Nothing I could do, change the setting, or reboot repeatedly fixed it. Finally what worked for me was this:
Make a small change to the code and commit it, and push it up to the app service.
This fixed it for me. It has happened a couple times now and every time this solution has worked for me. It seems like the App Service sometimes gets in this state and needs to be jostled with this trick?

keep getting "No module named 'flask'"

I am trying to deploy my Flask endpoint on a AWS EC2 and after successfully installing requirements.txt which I run on ElasticBeanstalk, I try to run my scrip and it gives me:
from flask import Flask, jsonify
ModuleNotFoundError: No module named 'flask'
I tried manually to install flask with:
pip3 install flask
and still does not want to compile.
I haven't used AWS EC2 yet, but try adding shebang on top #!/usr/bin/env python

Port Virtualenv based app error

For the last couple of weeks I have programmed a flask server. I was adviced to use virtualenv to make sure that all my python dependencies are easily tracked. However, now I have deployed it on my test server and it gives me the following error:
from flask import Flask
ImportError: No module named flask
Which is about the first python module that I have installed. Is there something I am doing wrong here? Virtualenv should've taken care for this, right? How can I check for these kind of errors during development?

python lxml unavailable in dev_appserver(gae,windows)

I have installed lxml yet.
It works fine in IDLE.
But when I start an basic app described below with dev_appserver.py ,server returns error "No module named lxml".
import webapp2,lxml
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("test")
app = webapp2.WSGIApplication([("/(.*)", MainPage)],debug=True)
How can I resolve this??
Thanks!!
Assumingly you're using Python 2.7 runtime. This runtime provides a fine way for configuring libraries.
Please add libraries section in your app.yaml as follows:
libraries:
- name: lxml
version: latest
For more details, please see:
https://developers.google.com/appengine/docs/python/python27/using27#Configuring_Libraries
Any python library you use will need to be in your app folder - otherwise, it won't work when deployed, because only your app folder is deployed to App Engine. You'll need to put a copy of lxml in your app folder.
Secondly though, I don't think lxml will work at out, since it runs on top of C libraries, and only pure python projects work on App Engine.

Categories