I'm writing a Flask application in IntelliJ, and for some reason I can't get IntelliJ to recognize Flask extensions. And so this works just fine:
from flask import Flask, Response, request
But this does not:
from flask.ext.wtf import Form
It will recognize and autocomplete flask.ext but not any submodules.
Does anyone know what the problem could be?
More info:
I'm using virtualenv and installing dependencies via bin/pip install <dependency>.
I'm using the Python 2.7.5 SDK
The modules that I'm trying to get IntelliJ to recognize have all been successfully installed.
PyCharm has difficulty completing flask.ext... because all flask.ext does is proxy imports at runtime, so the built-in static detection is faulty.
For future reference, flask.ext is deprecated in favor of using the actual package import such as from flask_wtf import Form. As of Flask 1.0 it has been removed completely.
Related
I have only python 3.8.5 installed.
I started diving into Python and Flask for the first time (I'm coming from the javascript side of things). I've already ran into an interesting issue in just my first few lines of code.
I was able to run pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy and they all seemed to have installed just fine. They all appear in the pipfile.lock. flask-marshmallow is version 0.13.0.
pipfile.lock
When I started coding, I was able to import flask and flask_sqlalchemy with no issues. Intellisense even helped me out with them. But from flask-marshmallow import Marshmallow didn't seem to work.
When I ran python app.py I get the following error
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_marshmallow import Marshmallow
ModuleNotFoundError: No module named 'flask_marshmallow'
I've tried uninstalling flask-marshmallow and marshmallow and reinstalling. The console always says the installation is successful but I can't seem to import it when all the other packages seem to work fine.
What a great start to a new language xD Here is the whole file so far for reference, if I remove the marshmallow line it starts without any issues
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
Make sure that, because you have created a pipenv, you have set the Python interpreter path correctly in your IDE.
I was facing the exact issue (and that is how I reached this question). I am using VS code, was using python3.8 and pipenv.
Even though I have installed the packages using pip3, I was facing import issues while running the code. After a futile search on the Internet, I realised that the issue was very silly.
The Python interpreter path (Cntrl+Shift+P -> Select Interpreter) was not set to the newly created pipenv. After I have set the interpreter path properly, the code resumed to function as expected.
i would suggest to check the site packages file and make sure that it is installed in it if not then it is installed in other directory
Also try working it in another normal python file and check if that works too
Check syntax also i know i know its obvious but just saying
In my case I was trying to run the command flask db init, and it would give the error above. I got it to work by running the command with sudo privileges sudo flask db init. I hope this can help someone else out in a similar situation!
I'm learning a little bit how function python, more specific Flask.
I'm creating a new project using flask, but when I create a new file and import Flask I receive this error: No name 'Flask' in module 'flask'pylint(no-name-in-module)
I did read that, I need create a file called: __init__.py but I have this and the error keeps showing up.
This is my actual code (very simple code, and folder/files structure):
Thanks for the help! :D
I tried this: Flask ImportError: No Module Named Flask, but this solution does not works for me.
I installed flask on python 2.7 and run simple hello world script and it worked fine. Then I updated python to python 3.8 and I got an error similar to yours:
Unable to import 'flask'pylint(import-error)
I simply uninstalled flask and installed again and it worked for me. Run this two commands:
pip uninstall flask
pip install flask
Then export the FLASK_APP environment variable and run flask
set FLASK_APP=file.py
flask run
I solved this changing my python interpreter. I did uninstall pylint and install pylama and now all is fine!
I just recently launched my local Google App Engine sandbox Python application after not touching it in a while. It seems the following import is giving me problems, but this didn't happen before.
from googleapiclient.discovery import build
This results in the import error:
ImportError: No module named six
I'm not sure what changed with the GAE launcher but it seems the six module is no longer included. I checked on my system and this module is installed globally.
The problem was introduced by some updated libraries included with Google App Engine launcher. To avoid the import error, you'll need to include module six into you project. It can be found here
If I use the following format in my application, everything works, except PyCharms resolving / autocomplete feature:
from flask.ext.sqlalchemy import SQLAlchemy
If I use the following format in my application, everything works. But, alas, it is not the correct way to import the libraries:
from flask_sqlalchemy import SQLAlchemy
Is there any way to make PyCharm resolve the first syntax correctly?
The flask.ext namespace is a transistion namespace, see the Extension Import Transition section of the Flask Extension Development docs:
For a while we recommended using namespace packages for Flask extensions. This turned out to be problematic in practice because many different competing namespace package systems exist and pip would automatically switch between different systems and this caused a lot of problems for users.
and
Flask extensions should urge users to import from flask.ext.foo instead of flask_foo or flaskext_foo so that extensions can transition to the new package name without affecting users.
So to transition between versions, the flask.ext alias was added, which will automatically try to import flask_[name] packages when importing flask.ext.[name]. But that transition is now moot; you no longer will find packages that still rely solely on flask.ext.
As such, it is perfectly fine to use the actual module name and have PyCharm autocomplete the module contents.
You only really have to use flask.ext if you are still using an older version of the extension and need to be future compatible. That future is already here.
FYI. flask.ext is deprecated, and the right way is:
from flask_sqlalchemy import SQLAlchemy
In case anyone found this SO question on Google.
Use a virtualenv and set that virtualenv for your project in PyCharm. I had the same problem as you do and after setting the correct virtualenv (which contains the flask and flask_sqlalchemy extension) my problem is solved.
To set a virtualenv for your project in PyCharm (from JetBrains Web Help):
To add an existing virtual environment to the list of available
interpreters
In the Project Interpreter page of the project settings, click .
In the drop-down list, choose Add local.
In the Select Python Interpreter dialog box that opens,
choose the desired Python executable, and click OK.
Moreover try adding requirements.txt to the root of your project, afterward PyCharm will notify you to install missing dependencies which might help.
Why am I getting this error when running my application which is trying to OAuth with Foursquare?
import httplib2
ImportError: No module named httplib2
I have installed httplib2 by downloading it and $ python setup.py install on the command line as instructed here
Am I missing something? Thanks
You will need to include the library in your project so that the App Engine runtime knows what you're importing.
From here:
You can include other pure Python libraries with your application by putting the code in your application directory. If you make a symbolic link to a module's directory in your application directory, appcfg.py will follow the link and include the module in your app.
I've created a simple example on how to do this when using Google App Engine. (https://github.com/muanis/foursquare-oauth-bootstrap) basically you need Google oAuth2 Client (http://code.google.com/p/google-api-python-client/) and httplib2 (http://code.google.com/p/httplib2/)
Add httplib2 as a third party library as documented here:
https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring
Install with:
pip install -t lib httplib2
https://developers.google.com/api-client-library/python/start/installation
To install the library and all of its dependencies in an App Engine
project, download the file named google-api-python-client-gae-N.M.zip
from the list of downloads, where N.M is the version number of the
latest release. Unzip that file into your project.