When I try to install anything with Pip, it gives me the "Fatal error in launcher:" error. That wouldn't be too bad, since I know how to update Pip differently. However, the same error occurs when I try to run the "flask run" command.
I'm using Windows 10, Python 3.8.2 and I have previously set the FLASK_APP variable to flaskblog.py. This is its content:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
Since you haven't posted the error clearly, if Fatal error in launcher: Unable to create process using ‘”‘ is your error,
then you might want to update pip using python -m pip install --upgrade pip and try installing your package again using python -m pip install <pkg-name>. You should get it working.
if the error persists after the above mentioned steps, the try importing pip in your python console(pull up your terminal and type python then type import pip) and try pip.main([‘install’,’<pkg-name>’]) in the console.
Hope this helps.
referred from here
Edit
Alternatively, you can run your flask app by adding app.run() in your script.
Like:
if __name__ == __main__:
app.run()
and then in the terminal, run python -m flaskblog.py.
Note: if you want to run your app in debug mode, consder giving debug = True to app.run().
Related
After typing
export FLASK_APP=use_flask.py
And then typing
printenv
which outputs inter alia:
TERM_SESSION_ID=AA805368-CF19-4631-AABB-A0112AD535CE
FLASK_APP=/users/me/documents/pcode/color/use_flask.py
USER=me
CONDA_EXE=/Users/me/Applications/miniconda3/bin/conda
I then place in the file use_flask.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'hey man'
And then type
ls
which outputs inter alia
use_flask.py
I then type:
flask run
I get the error
-bash: flask: command not found
I have also tried exporting the full path of the use_flask.py file but that did not work either. What am I doing wrong?
I ran
pip install flask flask-alchemy
I thought I already had flask installed because when I typed
from flask import Flask
into my Pycharm editor and then ran the code no error message occurred. So although I solved the problem I don't understand why it didn't work the first time.
I'm new to working with azure functions and tried to work out a small example locally, using VS Code with the Azure Functions extension.
Example:
# First party libraries
import logging
# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest
def main(req: HttpRequest) -> HttpResponse:
seed = req.params.get('seed')
if not seed:
try:
body = req.get_json()
except ValueError:
pass
else:
seed = body.get('seed')
if seed:
np.random.seed(seed=int(seed))
r_int = np.random.randint(0, 100)
logging.info(r_int)
return HttpResponse(
"Random Number: " f"{str(r_int)}", status_code=200
)
else:
return HttpResponse(
"Insert seed to generate a number",
status_code=200
)
When numpy is installed globally this code works fine. If I install it only in the virtual environment, however, I get the following error:
*Worker failed to function id 1739ddcd-d6ad-421d-9470-327681ca1e69.
[15-Jul-20 1:31:39 PM] Result: Failure
Exception: ModuleNotFoundError: No module named 'numpy'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound*
I checked multiple times that numpy is installed in the virtual environment, and the environment is also specified in the .vscode/settings.json file.
pip freeze of the virtualenv "worker_venv":
$ pip freeze
azure-functions==1.3.0
flake8==3.8.3
importlib-metadata==1.7.0
mccabe==0.6.1
numpy==1.19.0
pycodestyle==2.6.0
pyflakes==2.2.0
zipp==3.1.0
.vscode/settings.json file:
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": "worker_venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen"
}
I tried to find something in the documentation, but found nothing specific regarding the virtual environment. I don't know if I'm missing something?
EDIT: I'm on a Windows 10 machine btw
EDIT: I included the folder structure of my project in the image below
EDIT: Added the content of the virtual environment Lib folder in the image below
EDIT: Added a screenshot of the terminal using the pip install numpy command below
EDIT: Created a new project with a new virtual env and reinstalled numpy, screenshot below, problem still persists.
EDIT: Added the launch.json code below
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
SOLVED
So the problem was neither with python, nor with VS Code. The problem was that the execution policy on my machine (new laptop) was set to restricted and therefore the .venv\Scripts\Activate.ps1 script could not be run.
To resolve this problem, just open powershell with admin rights and and run set-executionpolicy remotesigned. Restart VS Code and all should work fine
I didn't saw the error, due to the many logging in the terminal that happens
when you start azure. I'll mark the answer of #HuryShen as correct, because the comments got me to the solution. Thank all of you guys
For this problem, I'm not clear if you met the error when run it locally or on azure cloud. So provide both suggestions for these two situation.
1. If the error shows when you run the function on azure, you may not have installed the modules success. When deploy the function from local to azure, you need to add the module to requirements.txt(as Anatoli mentioned in comment). You can generate the requirements.txt automatically by the command below:
pip freeze > requirements.txt
After that, we can find the numpy==1.19.0 exist in requirements.txt.
Now, deploy the function from local to azure by the command below, it will install the modules success on azure and work fine on azure.
func azure functionapp publish <your function app name> --build remote
2. If the error shows when you run the function locally. Since you provided the modules installed in worker_venv, it seems you have installed numpy module success. I also test it in my side locally, install numpy and it works fine. So I think you can check if your virtual environment(worker_venv) exist in the correct location. Below is my function structure in local VS code, please check if your virtual environment locates in the same location with mine.
-----Update------
Run the command to to set execution policy and then activate the virtual environment:
set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1
I could solve my issue uninstalling python3 (see here for a guide https://stackoverflow.com/a/60318668/11986067).
After starting the app functions via F5 or func start, the following output was shown:
This version was incorrect. I have chosen python 3.7.0 when creating the project in the Azure extension. After deleting this python3 version, the correct version was shown and the Import issue was solved:
I am attempting to just run the Hello World code from Tornado docs
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Except I am getting an error: AttributeError: module 'test' has no attribute '__path__'
I am just using IDLE to run test.py
I thought this was due to my Windows 10 computer not having Python accessible to PATH but even with adding in the python 3.6 to PATH I am still getting the same error. Any ideas?
The screenshot is how I added python to PATH and I think I got it correct..
------EDIT------
Ill add some screenshots of the errors/tracebacks I am running into. 1st one is the command prompt below when the test.py is ran in IDLE 3.6 in Windows 10.
If there is an import error, I can import Tornado just fine thru IDLE interpreter.
I also tried running this hello World code in IPython 3.7, and I get this error:
Solution: Run your file without the -m argument.
Another solution would be to provide the file name without the .py extension:
python -m test
This will also work.
Explanation:
The -m argument tells Python to run a module (file) present in the Python path. It doesn't take the name of the file, it takes the name of the module. The difference is that the file name contains .py suffix, whereas the module name doesn't.
So you can run the test.py file like this, too: python -m test.
When to use -m argument:
The -m argument is there for convenience. For example, if you want to run python's default http server (which comes with python), you'd write this command:
python -m http.server
This will start the http server for you. The convenience that -m argument gives you is that you can write this command from anywhere in your system and python will automatically look for the package called http in your the system's Path.
Without the -m argument, if you wanted to run the http server, you'd have to give it's full path like:
python C:\path\to\python\installation\http\server.py
So, -m argument makes it easy to run modules (files) present in the Path.
With Tornado would you happen to know how to kill the Python interpreter? A CNTRL-C doesn't do anything.
I use Linux and Ctrl-C works fine for me. On Windows you can try Ctrl-D or Ctrl-Z. Or here are some answers: Stopping python using ctrl+c
I'm trying to use scss with Flask and get it to auto-compile.
I've tried using Flask-Scss — unfortunately, when I set it up, I get Scanning acceleration disabled (_speedups not found)! errors, and no CSS file. Anyone know how to fix this, or get it to generate CSS files?
The error results from an error in the installation process. If you install through pip on an Ubuntu system and you get this warning:
==========================================================================
WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python installation succeeded.
==========================================================================
Then you should make sure you have the libpcre3-dev library pre-installed (this is the module that contains pcre.h, the module that the C-installation fails on):
apt-get install libpcre3-dev
After doing this, re-install Flask-Scss:
pip install Flask-scss --force-reinstall -I
After restarting the Flask server, the error should now be a thing of the past.
But, please note
Even though the above will solve the problem of the _speedups not found error showing up, there is another likely reason for your files not getting compiled. If you have code like this:
app = Flask(__name__)
from flask.ext.scss import Scss
Scss(app, static_dir='static', asset_dir='assets')
...
if __name__ == "__main__":
app.run(debug=True)
, and you are not setting debug anywhere else, then you should make sure to put
app.debug = True
before the invocation of the Scss object:
app.debug = True
Scss(app, static_dir='static', asset_dir='assets')
Happiness! That should do the trick for getting your .scss files to compile every time you load a page in debug mode.
I'm new to programming, and tried to work through the flask tutorial.
http://flask.pocoo.org/docs/tutorial/
I'm stuck on this part (from the readme on github) when trying to run the app:
https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/
Fire up a python shell and run this:
from flaskr import init_db; init_db()
I get this error when I try to run the command in python shell:
Import error: No module named flaskr
And I get this error when I try to run app locally:
sqlite3.OperationalError
OperationalError: unable to open database file
I've been looking for solution for several hours now, but to no avail.
Any thoughts on what I could check? Thank you.
The thing that fixed it for me was changing
export FLASK_APP=flaskr
to
export FLASK_APP=flaskr.py
Taken from here
The simplest way to accomplish what you need is to fire up the Python shell in the same folder as you have flaskr:
# I'm assuming that python is available on the command line
$ cd path/to/flaskr
$ python
# Python then runs and you can import flaskr
>>> from flaskr import init_db; init_db()
>>> exit()
The trick is that when you run Python it only looks in a certain number of places for modules and packages - you can see which places by running:
>>> from sys import path
>>> for fp in path:
... print fp
from the Python interpreter. If the path to flaskr is not in that list flaskr can't be imported. By default Python adds the directory it is started in to its search path (which is why we start Python in the directory that contains flaskr.)
Once you have run init_db you should be able to run the application and see everything working.
If you're using a version of Flask < 0.11, the flask command is not available. Install the flask-cli package in that case.
pip install flask-cli
To anyone else who finds this, add init_db() to the main executer to the end of your flaksr app as follows:
if __name__ == '__main__':
init_db()
app.run()
That should solve the sqlite error and stop you from having to run init_db() manually.
When we say :
export FLASK_APP=flaskr
by no means python understands where the package "flaskr.py" exists.
One of the way to solve the issue is choosing the right path where the "flaskr.py" resides. For eg, change your current working directory to where the file exists and :
export PYTHONPATH=`pwd`
Then you can execute "flask run" anywhere you want.
PS: flask tutorial seems broken. :)
Sean Viera's answer was very good, although I'd like to add that I was encountering the same problem and want to add to the solution. Running Python from the same flaskr folder was not enough for me. It was also necessary to activate Flask before running $Python by running the ". venv/bin/activate" command, like so:
$ cd path/to/flaskr
#active
$ . venv/bin/activate
(venv)$ python
# Python then runs and you can import flaskr
>>> from flaskr import init_db;
>>> init_db()
>>> exit()
$
Hope that extra bit of info helps!
$set FLASK_APP=flaskr
$python -m flask initdb
$python -m flask run
Try this:
OS: Windows
(venv)$pip install -I --no-deps Flask
(venv)$set FLASK_APP=flaskr
(venv)$set FLASK_DEBUG=1
(venv)$flask run
Explain:
$where flask will help to locate flask.exe
if virtual-env inherits flask from system-env
(venv)$where flask
>> /system/environment/path/to/flask.exe
(root)$where flask
>> /system/environment/path/to/flask.exe
obviously it calls flask.exe which is installed for system-env
(venv)$pip install -I Flask can force to (re)install flask for virtual-env
(venv)$pip install -I Flask
(venv)$where flask
>> /virtual/environment/path/to/flask.exe
>> /system/environment/path/to/flask.exe
I have same problem, and I have fixed it by.
step1:
sudo ln -sf /usr/bin/python3.4 /usr/bin/python
easy_install_3.4 flask.
PYTHONPATH=pwd
step2: using easy_install_3.4 to install falsk.
step3:
Heading