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!
Related
I've been using this tutorial to setup flask on ubuntu server.
It worked fine for one project, now I try to setup it on the other one.
This time the app works fine without SQLAlchemy, but once I use it, it gives 'Internal server error'.
checked the installation of flask_sqlalchemy - looks fine
made sure I don't have a file named 'sqlalchemy'
I can launch the app with sqlalchemy but without virtualenv, it works fine!
I double-checked that flask_sqlalchemy has the same configuration in virtualenv library, as in the python library
error that I receive:
Traceback (most recent call last):
File "myproject2.py", line 4, in <module>
from flask_sqlalchemy import SQLAlchemy
File "/root/myproject2/myprojectenv2/lib/python3.8/site-packages/flask_sqlalch emy/__init__.py", line 16, in <module>
from sqlalchemy import event, inspect, orm
ImportError: cannot import name 'inspect' from 'sqlalchemy' (/root/myproject2/my projectenv2/lib/python3.8/site-packages/sqlalchemy/__init__.py)
Thank you for any suggestions!
It seems your installation is broken in the virtualenv. Try reinstalling by running this from within the virtualenv:
pip install --upgrade --force-reinstall flask-sqlalchemy sqlalchemy
I have run into same problem but with different kind of issue. While upgrading to recent flask_sqlalchemy and sqlalchemy libraries, I have noticed that construct has been changed from:
from flask_sqlalchemy import inspect
this got broken, and new construct is:
from sqlalchemy import inspect
just want to state something that may be easily overseen
apart from that the issue may be wrong libraries in venv on specific installations
I have system python 3.10 however I run app under python 3.7 for compatibility reasons. On that line, I have noticed that it is easy to mix python library versions and get 3.10 library under 3.7 venv - pay attention during installation of SQL libraries - pip may go wild.
hope these hints help someone as these issues drove me nuts for days
I'm more or less new to computer science and can not even manage to run my first "Hello World" in Flask (Python 3; on my Windows-10 computer). I have tried some different tutorials on how to get started with Flask but did not find them really helpful so I did not finish them. Eventually I found a tutorial that seemed comprehensive and fitting my needs, but Visual Studio Code Terminal could not execute my code but gave me back an Error Message. Maybe I created a global virtual environment before I started this course that I'm following along now, and that's why it gives me that Error, but am not sure.
So this is the tutorial that I eventually picked and am currently following along: https://www.youtube.com/watch?v=QjtW-wnXlUY.
The tutorial showed me how to set up and activate a local virtual environment in my project folder and pip install Flask as well as save app.py, with the help of my command batch and VSCode.
Then the user should try and run the following code (see below this paragraph). On my browser it really worked perfectly fine, but the Visual Studio Code-Terminal gave me an error message called "ModuleNotFoundError: No module named 'flask'" (I know there are some similar-named threads out there in stackoverflow, but the answers given did not help me with my problem and also it seems to me that my case is slightly different). So this is the code I tried to run in Visual Studio Code:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello world!"
And this is the complete ErrorMessage that Visual Studio Code-Terminal gave me:
PS C:\Program Files\DB Browser for SQLite> & C:/Users/User/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/User/Desktop/Flask_app/app.py
Traceback (most recent call last):
File "c:/Users/User/Desktop/Flask_app/app.py", line 1, in <module>
from flask import Flask
**ModuleNotFoundError: No module named 'flask'**
PS C:\Program Files\DB Browser for SQLite>
Any ideas what's wrong with my code? Maybe it has to do with the path I stored it in? I got no idea; am very thankful for help!
Cheers.
Make sure the virtual environment you created is activated before running the app.
Also make sure it is activated when you run 'pip install flask'.
first install the flask in your system
pip install flask
run the flask server then try it
> set FLASK_APP=hello
> flask run
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'm new at this and have no idea of what I'm doing wrong:
I have installed the flask_login module with
pip install flask_login
It apparently worked: if see it installed in c:\users\~\python\python36-32\lib\site-packages
And when I use pip list, I can see it listed as Flask-Login (0.4.0)
Despite of this, when I try to run from flask_login import LoginManager, I'm getting ImportError: No module named flask_login.
What am I doing wrong?
I figured out what was happening.
For some reason, Anaconda installed Python 2.7 in a path that was colliding with the one I use normally (w/ Python 3). Once I uninstalled Python 2.7, everything worked fine.
For some reason, only flask_login was being affected. My other flask_** libraries were working fine.
In case you are experiencing this in a Python environment with Apache2 and mod_wsgi: you have to restart the Apache2 server after installing new Python modules, as otherwise Apache2 will not "see" them even though the Python path is correct. So (assuming Debian / Ubuntu Linux):
service apache2 restart
Of course, this issue could happen with any module not being found, not just with flask_login. It just happened to me with flask_login, which is why I'm here …
I had the same problem and was able to solve using sudo:
sudo pip install flask_login
I think import errors are so common in flask because flask users normally work in virtual environments. Running pip install inside a VE or outside leads to different installations in different paths and can cause confusion. sudo installation works globally and can be a solution if you don't need a certain dependency in a specific VE.
I was using VS code. In the .venv there was another copy of flask_login so basically just check if another copy of flask_login exists within your lib
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.