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.
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 am currently trying to integrate a flask backend to my react web-app. In my flask subdirectory, I have installed requests module as shown below but I keep running into an unresolved import error although I have installed the module. I have tried to follow different troubleshooting suggestions online but none seem to work. Would really appreciate if someone can help me out.
This looks like that the terminal and the IDE use two different environments.
You should either create a virtual env with PyCharm directly, or choose the existing one.
Please read the PyCharm documentation:
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
There, either choose "New environment" or "Existing environment".
This should be pretty basic as I've installed lots of python packages, but I can't for the life of my get google apis client library for python to install.
I'm pressure sure I've gone through the instructions on this website properly:
https://developers.google.com/api-client-library/python/start/installation
To summarize I've done the following:
$ easy_install --upgrade google-api-python-client
this seems to work fine, doesn't report any errors or warnings
downloaded and unzipped google-api-python-client-gae-1.2.zip into the directory where my project is
but if I open an iPython session in the folder where I unpacked the full dependencies I can't do the basic imports such as:
import google.appengine.api it just says "No Module named google.appengine.api"
I checked in my site-packages folder and google_api_python_client-1.2-py2.7.egg is there. But it doesn't show up in sys.path
when I do sys.path.append('C:\Anaconda\Lib\site-packages\google_api_python_cli
ent-1.2-py2.7.egg') it adds the correct path, but the import still doesn't work.
EDIT: This fixed my problem
Adding the Google SDK to my Python path did the trick. I don't know why the installer didn't do this when I ran it. but hey, this worked
So if I run:
sys.path.append('C:\\Program Files (x86)\\Google\\google_appengine')
from google.appengine import api
works!
Without knowing all the steps you took to install app engine and the client APIs, my only recommendation is to install (or reinstall) the Google App Engine Python SDK from here: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python. Looks like you're on windows, so you would grab the MSI.
Just make sure you launch the AppEngineLauncher application after installation as it will give you the option to create symlinks so you can run commands from terminal.
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.
Im completely new to django and Ive been trying to set up an environment from here [https://docs.djangoproject.com/en/1.1/intro/tutorial01/]. Python was already setup on my mac
Python 2.7.2. I followed the instructions and setup django using
sudo port install py27-django
And also added /etc/paths.d/macports with
/opt/local/bin
/opt/local/sbin
/opt/local/lib/python2.7/site-packages/django/bin
But I still get the ImportError: No module named django message when I import django from the python shell.
I looked at this link
importerror: No module named django but it didnt really help much. Any help is appreciated
Have a look at this article... they are using virtualenv to set up the project and this fixed a lot of problems like this for me. With virtualenv, the system libraries and the libraries you need for your python project are separated, so your not mangling them together.
http://www.jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/
If I may, it looks like the last path you added was pointing to a python2.4 directory, you might need to look for django in a python2.7 directory instead.