This question already has answers here:
Why isn't PyCharm's autocomplete working for libraries I install?
(2 answers)
Closed last year.
I'm new on python, and use 3.10.2 version. I wrote this in vs code;
from flask import Flask
app=Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
but the result is;
Traceback (most recent call last):
File "blog.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I tried everything that written on this site but the result is the same. I am using mac and waiting for your help. Thanks.
Try to install the Flask first, use pip.
pip install Flask
reference :
https://flask.palletsprojects.com/en/2.0.x/installation/
Related
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 8 days ago.
Using Flask (0.8) and Werkzeug (0.8.1) when attempting to run code with app.run(debug=True) I get the below described error. There are no errors when using app.run()
The error
Traceback (most recent call last):
File "code2.py", line 9, in <module>
app.run(debug=True)
File "/<snip>/env/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 703, in run
run_simple(host, port, self, **options)
File "/<snip>/env/lib/python2.7/site-packages/Werkzeug-0.8.1-py2.7.egg/werkzeug/serving.py", line 587, in run_simple
from werkzeug.debug import DebuggedApplication
File "/<snip>/env/lib/python2.7/site-packages/Werkzeug-0.8.1-py2.7.egg/werkzeug/debug/__init__.py", line 14, in <module>
from werkzeug.debug.tbtools import get_current_traceback, render_console_html
File "/<snip>/env/lib/python2.7/site-packages/Werkzeug-0.8.1-py2.7.egg/werkzeug/debug/tbtools.py", line 19, in <module>
from werkzeug.debug.console import Console
File "/<snip>/env/lib/python2.7/site-packages/Werkzeug-0.8.1-py2.7.egg/werkzeug/debug/console.py", line 144, in <module>
class _InteractiveConsole(code.InteractiveInterpreter):
AttributeError: 'module' object has no attribute 'InteractiveInterpreter'
The code (code.py)
from flask import Flask
app = Flask(__name__)
#app.route('/news/')
def news():
pass
if __name__ == '__main__':
app.run(debug=True)
Steps taken to recreate the error
$ cd <project directory>
$ . env/bin/activate # Activates virtuanlenv environment (see below for packages)
$ python code.py
Contents of my env/lib/python2.7/site-packages (versions of various library used) via virtualenv
Flask-0.8-py2.7.egg
Jinja2-2.6-py2.7.egg
pip-1.0.2-py2.7.egg
setuptools-0.6c11-py2.7.egg
Werkzeug-0.8.1-py2.7.egg
Things I have tried to solve this problem so far, that have not helped (unfortunately)
Extensive googling/SO searching
Heavy simplification of my code
Deleting created virtualenv and all libraries and reinstallation via easy_install
The strange thing here is that last night, this code worked fine. This morning, without changing anything (that I'm aware of) the code failed to properly run.
Thanks very much for your help!
The problem is that you have named your module code.py. code is a built in Python module that werkzeug uses.
To fix the problem, rename your code.py to something else, and make sure you delete the code.pyc file.
Solution to your problem is is your filename; don't name your python file name as code.py, main.py, Flask.py, os.py, system.py.
Instead, you can use code1.py or something.
from waitress import serve # error line
import app # python file where flask code is written
serve(app.app,port=8000,thread=55)
Python version : 3.7
waitress version : try with 1.4.1 and 1.3.0*
Stack Trace
C:\Users\ashish.kamble\ModelDeployment\Scripts\python.exe C:/Users/ashish.kamble/PycharmProjects/ModelDeployment/waitress.py
Traceback (most recent call last):
File "C:/Users/ashish.kamble/PycharmProjects/ModelDeployment/waitress.py", line 1, in <module>
from waitress import serve
File "C:\Users\ashish.kamble\PycharmProjects\ModelDeployment\waitress.py", line 1, in <module>
from waitress import serve
ImportError: cannot import name 'serve' from 'waitress' (C:\Users\ashish.kamble\PycharmProjects\ModelDeployment\waitress.py)
Process finished with exit code 1
Your Error occurred because of your file name is waitress.py.
When you run your code your waitress keyword work as an object of your file. There for if you want to get rid of from that error rename your python file into which not represent your libraries.
Addition -: It is better to install libraries using Pycharm project interpreter. Follow these steps to install libraries using interpreter.
I'm really new to Python and I'm trying to do a tutorial on using flask. When I run the below code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, World!"
I get the following error message:
Traceback (most recent call last):
File "/Users/myname/Documents/PycharmProjects/flask/test_webapp.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask
This doesn't make any sense to me as when I run:
python -m pip list
Flask 1.1.1 is on the list.
I am using Python version 3.7.4.
I realise this questions has been asked before but after carefully reading through I can't find the correct answer. I realise this is quite basic but any help would be really appreciated!
python -m pip list answers in terms of Python 2.x
If you're using Python 3.x, you need to do pip3 install flask
Module Flask is installed for Python 2 (Python), but not for Python 3 (Python3) that you are using.
So, install it as pip3 install Flask
This question already has answers here:
Python import modules from a higher level package
(2 answers)
Closed 7 years ago.
I've got a system built in Python in which I now want to run some functions periodically with a regular cron. So I made a file which starts off like this:
#!/usr/bin/env python
from app.models import User
When I import the file from the interactive Python command line I there are no problems:
>>> from app.crons import scrapeChannels
>>>
But when I run the file directly from the terminal I get the ImportError:
$ /home/kramer65/app/crons/scrapeChannels.py
Traceback (most recent call last):
File "/home/kramer65/app/crons/scrapeChannels.py", line 2, in <module>
from app.models import User
ImportError: No module named app.models
Now I assume this has something to do with the fact that app.models not in the sys.path is, but from this point I'm kinda lost. Am I right in my assumption? And how can I solve this issue?
All tips are welcome!
I think if you want to run the script from cron as well, the best would be to manually append the parent directory of app (which seems to be /home/kramer65/ to your sys.path , before the line from app.models import User .
Example -
import sys
sys.path.append('/home/kramer65/')
Add the directory containing the app package to your python path, using PYTHONPATH:
$ set PYTHONPATH=/home/kramer65
$ /home/kramer65/app/crons/scrapeChannels.py
Is CherryPy broken? I just set it up and tried to use the routes dispatcher but it has an import error, my code is as follows:
import cherrypy
mapper = cherrypy.dispatch.RoutesDispatcher()
The error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jwesonga/environments/cherrypy/lib/python2.6/site-packages/CherryPy-3.2.2-py2.6.egg/cherrypy/_cpdispatch.py", line 463, in __init__
import routes
ImportError: No module named routes
I'm on a Mac and I tried both 3.2.2 and 3.0 using virtualenv for the latter.
I have successfully used CherryPy with the routes dispatcher under OS X.
The error you've shown is:
ImportError: No module named routes
This is pretty clear -- Python can't find the routes modules. Have you installed it? This is not part of CherryPy, it's a separate module that you will need to install. If you're using MacPorts, you should be able to:
port install py-routes
(Or py25-routes or py26-routes depending on which Python you're using). If you're using virtualenv, you can simply run:
easy_install routes