I have an import error while coding a flask web application. Here are my imports:
from flask import Flask
from flask import session, request
from flask import render_template, redirect
from flask_sqlalchemy import sqlalchemy
Although the last line of code gives me the error:
from flask_sqlalchemy import sqlalchemy
I have successfully installed Flask, SQLAlchemy, and Flask-SQLAlchemy using pip, and I have checked whether these are installed on the system using
python -c "import Flask"
etc. These checks resulted in a 0, meaning that it was correctly installed. NOTE: I have already read other posts, and tried flask.ext.sqlalchemy, flaskext_sqlalchemy, etc. as import variations. Here are the installed packages in the sitepackages directory for python 2.7.
Flask_OAuth-0.12.dist-info httplib2
Flask_SQLAlchemy-2.3.2.dist-info httplib2-0.10.3.dist-info
SQLAlchemy-1.2.0.dist-info oauth2
flask_oauth.py oauth2-1.9.0.post1.dist-info
flask_oauth.pyc sqlalchemy
flask_sqlalchemy tests
I have also structured my files, so the imports are in the init.py file. My other files included views.py, config.py, run.py, and a templates folder containing html files.
What am I doing wrong here, and how is my problem different from others?
Module imports are indeed case sensitive.
The way to check this is to first confirm if it's possible to:
import flask_sqlalchemy
If that succeeds, the package is installed correctly. Once the package is confirmed to be installed correctly, then this should succeed.
from flask_sqlalchemy import SQLAlchemy
Related
When deploying a flask application on heroku im getting the error above. The problem on heroku is that it installs dependencies and Iam not able to overwrite them then, or?
On my local server i just went to flask_uploads.py and change the imports to:
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
and this works fine.
but when deploying the flask app to heroku, how can I change the the content of flask_uploads.py after it had been installed?
flask-uploads is no longer properly maintained and has not released a fix to the updated Werkzeug API change, thus you see this error.
Just swap flask-uploads with flask-reuoloaded in your dependency list, eg requirements.txt or similar. You don't have to change your imports!
See https://github.com/jugmac00/flask-reuploaded
I have a Flask app with the following (typical, I think) structure.
repo_name
config.py
requirements.txt
run.py
/service
__init__.py
models.py
schemas.py
views.py
The run.py script has the following content
from service import app
app.run(debug=False, port=8080, host='0.0.0.0')
Inside service.__init__.py I create the Flask app, and at the end, import views.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
ma = Marshmallow(app)
import service.views
The models.py, schemas.py, and views.py are all pretty standard for simple Flask apps, and they also call from service import app or from service import models as needed.
My problem is that PyLint is throwing the error "E0401: Unable to import 'service'" for all these import calls. This is not really a "legitimate" error because I can run the code just fine.
One thing that makes the error go away is changing the import calls to from repo_name.service import .... However, this is undesirable for two reasons
The repo_name is not really related to the app. If I were to go this route, I would probably end up moving everything down a directory. Further, there's no requirement that people name the directory the same way (e.g., someone might clone it as git clone <url> foobar instead of git clone <url> repo_name, which would break code.
More importantly, this doesn't even work. When I make those changes and try to run the server, I get ModuleNotFoundError: No module named 'repo_name'.
How can I make reconcile PyLint and Flask here? Ideally I'd like to fix the issue in the "correct" way, instead of just adding a # pylint: disable= call in front of each import.
I want try to use flask-login, but have some issues with import. Maybe I put it in a wrong place?
So, I installed it with pip install flask-login, it was put in python2.7/dist-packages. And there I got the following:
python2.7/dist-packages
- flask (python package)
- flask-login.py
- flask-login.pyc
- Flask_Login-0.2.10.egg-info (folder)
- flask-wtf (python package)
- Flask_WTF-0.9.5.egg-info (folder)
- Flask-0.10.1.egg-info
- jinja2 (python package)
- Jinja2-2.7.2.egg-info (folder)
and so on
So, as I can see from the content of dist-packages, all modules have python package and *egg-info folder. But flask-login does not have python package, only two .py files. And therefore I got Unresolved import from flask.ext.login import LoginManager.
In flask package I have ext package with init.py only in it.
If anyone knows what might went wrong I`ll really appreciate any help.
Btw, all modules (flask, jinja, wtforms) I`ve installed with pip.
UPDATE
Sorry for silly question. It appears I just should import like this: from flask_login import LoginManager. As it is just a module on libraries path.
But I have met a lot of imports like this from flask.ext.login import LoginManager. Is there a way to put login inside ext and is it important?
flask.ext has some magic lookup.
When a user does ``from flask.ext.login import foo`` it will attempt to
import ``from flask_login import foo`` first and when that fails it will
try to import ``from flaskext.login import foo``
So, do you have a flask_login, or a flaskext.login?
I downloaded Flask on my Windows system using
pip install flask
it says
Requirement already satisfied.
which makes sense cause i already installed it once before using easy_install.
The official Flask website says, i just need to do this to install. Yet i get the error
ImportError: cannot import name Flask
The code i used is:
from flask import Flask
app = flask.Flask(__name__)
app.debug = True
app.run()
How can i fix this
Not sure if this is the problem, but this code should throw a NameError. If you import the Flask class like this from flask import Flask, then make your app like this app = flask.Flask(__name__), the interpreter will throw a NameError. You never imported flask, only the Flask class. The code should be changed to app = Flask(__name__) since Flask is already in the namespace.
I'm working on a Google App Engine project using Flask. Flask is then using Jinja2. When I put Flask and all its required modules into the root folder of my project, the server started up just fine. However, I wanted to clean up the directory a bit so I moved Flask and other modules (including Jinja2) to a subdirectory called 'lib'. So my project looks like:
app.yaml
main.py
myapp
__init__.py
view.py
blahblah.py
lib
flask
jinja2
OtherModules
Then in the main.py file of the app I add the directory using sys.path.insert(0, 'lib'). Flask seems to import fine using this method, but Flask does not seem to be able to find Jinja2 with them both in the lib folder. When attempting to access a view on my running dev_appserver test I get:
File "lib/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: No module named jinja2
How can I allow Flask to find Jinja2 (and allow other modules to find their requirements) while keeping them in the lib directory and not having to edit the modules to adjust paths?
Below is my main.py file in case something in there would be useful to know:
import sys
sys.path.insert(0, 'lib')
from google.appengine.ext.webapp.util import run_wsgi_app
from myapp import app
run_wsgi_app(app)
In order to include Jinja in you app engine application in your app.yaml file add these lines
libraries:
- name: jinja2
version: latest
Documentation for including more of the available libraries.
In order to use it for your local server you should install these also to your system. In a unix like system it would be
sudo easy_install jinja2
Additional information: there is an open source framework called gae-init, which combines your tech stack and provides a series of automations and good practices for app engine web services. Maybe worth having a look at it.