I did something really bad. I don't know what I did. I created a test project with hello.py where I did some mistake when running with some command. Now, I have deleted that and back to the real project, and I got the following error:
File "/home/bhojendra/anaconda3/envs/myenv/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app
import(module_name)
ModuleNotFoundError: No module named 'hello'
I don't have even the word hello anywhere in the project.
I have removed all pycaches from the project, cleaned the conda conda clean -a, removed myenv environment and removed pip cache directory. Then, I re-created the environment and and re-installed the requirements and when launching the project with flask run, it throws that error again. Not sure what's happening.
It might be the issue with flask cache, I don't know how to overcome this issue.
In your environment, you likely left your FLASK_APP set to the file hello, so it was trying to run that, although it doesn't exist. You just have to export or set your flask app based on what machine you are using.
Unix Bash (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
Windows CMD:
> set FLASK_APP=hello
> flask run
Windows PowerShell:
> $env:FLASK_APP = "hello"
> flask run
You could also unset the export:
unset FLASK_APP
And then set the flask app
Related
I was trying to systemd a flask app. I tried to write a script like this:
#!/bin/bash
cd /path/to/app
source venv/bin/activate
python start.py
and just ExecStart this script in the .service file. But this doesn't quite work as starting the service errors with
python: command not found
I actually ran into quite a few issues, but eventually resolved with service file:
[Service]
WorkingDirectory=/path/to/app
ExecStart=/path/to/app/venv/bin/python start.py
Without WorkingDirectory, the file paths do not seem to work as the static file can't even be found.
So my question is actually why doesn't script above in the beginning work? The cd took effect, but the venv activate didn't?
I have this script:
import os
secret = os.environ.get('MFS')
print(secret)
For some reason, it returns None. I set that environment variable to a secret key that I want to use to set app.config['SECRET_KEY']. But, if I run this command: python3 -c 'import os;print(os.environ.get("MFS"))', it returns the correct environment variable. It doesn't matter if I cd to the project folder and run that or not, it just works. Even though I am using a venv to run the Flask app, that command still works when I run the venv Python. Why is this happening?
This question already has answers here:
How to run a flask application?
(6 answers)
Closed 9 months ago.
i have installed flask and i am trying to run flask using the windows power shell
but i am unable to do so
PS C:\path\to\app> $env:FLASK_APP = "hello.py"
the format is given the documentation what should we type in $env?
p.s. I was able to run flask using command prompt using the code
first, you have to activate the virtual environment for your Flask app (i'm assuming you are using venv the default virtual environment module for python : py -m venv venv)
PS C:\myapps\flask\helloflask> .\venv\Scripts\activate
(venv) PS C:\myapps\flask\helloflask>
set the FLASK_APP environment variable:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP="helloflask:create_app('development')"
note the double quotes wrapping the app name "helloflask:create_app('development')" otherwise Power Shell triggers a red error.
refer to this doc on using app factory pattern and i would recommend you this good reads on How to Run a Flask Application
maybe you want to check if FLASK_APP has been set:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP
helloflask:create_app('development')
now you can run your Flask app:
(venv) PS C:\myapps\flask\helloflask> flask run
* Serving Flask app "helloflask:create_app('development')"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Jun/2020 12:47:20] "←[37mGET / HTTP/1.1←[0m" 200 -
finally i recommend you using .flaskenv under the root of your project to store your Flask app related environment variables (FLASK_APP among others):
/.flaskenv
FLASK_APP=helloflask:create_app('development')
# FLASK_APP=helloflask:create_app('testing')
# FLASK_APP=helloflask:create_app('production')
FLASK_ENV=development
FLASK_DEBUG=0
# FLASK_RUN_EXTRA_FILES=
# FLASK_RUN_HOST=
# FLASK_RUN_PORT=8080
# FLASK_RUN_CERT=
# FLASK_RUN_KEY=
To take advantage of this option you have to install the python-dotenv package (have a look at this doc). To do so :
(venv) PS C:\mypps\flask\helloflask> pip install python-dotenv
I don't know if this will still be usefull but this is how I got mine to work.
In powershell type:
.venv/Scripts/activate and press Enter
In the comand line make sure you see something like: (.env) if so then type:
$env:FLASK_APP = "yourappname.py" and press Enter then type
$env:FLASK_ENV = "development" and finally type
flask run
I download django-project Linux. Now, I try run it Windows. Project don't have setting.py end I don't know how to run it? When I stara t project I have message module not found, and I don't know what need to do.
Manage.py
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings.dev")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Error
File "", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'website.settings'
You can follow following steps to open this project in windows.
Install git bash:
Download git for windows from here: https://git-scm.com/downloads.
By default, the python command runs extremely slow or never actually opens up the python interpreter on git bash. To fix this, we need to create an alias for the python command.
Open or create the .bashrc file. This will open the nano editor.
nano .bashrc
Install pip
Create a virtual environment
Install dependencies
Run the following command in the project path to install dependencies.
(venv) $ pip install -r requirements/base.txt
Run the project
We are all set to run the project.
Activate environment variables. I create an xport file with environment variables like this.
set -a
MAILSERVER="https://MAILURL"
DJANGO_SETTINGS_MODULE="myproject.settings.local2"
set +a
This enables variables inside this file to be exported. Run the following command in the project path.
$ source xport
Since it is a lot more trouble to get the actual database to be set up, I create another version of the settings file and configure sqlite as the database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Run the migrate command to create database structure
$ python manage.py migrate
Run the built-in webserver
$ python manage.py runserver
I'm trying to run a Python script I've uploaded as part of my AWS Elastic Beanstalk application from my development machine, but can't figure out how to. I believe I've located the script correctly, but when I attempt to run it under SSH, I get an import error.
For example, I have a Flask-Migrate migration script as part of my application (pretty much the same as the example in the documentation), but after successfully SSHing to my EB instance with
> eb ssh
and locating the script with
$ sudo find / -name migrate.py
when I run in the directory (/opt/python/current) where I located it with
$ python migrate.py db upgrade
at the SSH prompt I get
Traceback (most recent call last):
File "db_migrate.py", line 15, in <module>
from flask.ext.script import Manager
ImportError: No module named flask.ext.script
even though my requirements.txt (present along with the rest of my files in the same directory) has flask-script==2.0.5.
On Heroku I can accomplish all of this in two steps with
> heroku run bash
$ python migrate.py db upgrade
Is there equivalent functionality on AWS? How do I run a Python script that is part of an application I uploaded in an AWS SSH session? Perhaps I'm missing a step to set up the environment in which the code runs?
To migrate your database the best is to use container_commands, they are commands that will run every time you deploy your application. There is a good example in the EBS documentation (Step 6) :
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
The reason why you're getting an ImportError is because EBS installs your packages in a virtualenv. Before running arbitrary scripts in your application in SSH, first change to the directory containing your (latest) code with
cd /opt/python/current
and then activate the virtualenv
source /opt/python/run/venv/bin/activate
and set the environment variables (that your script probably expects)
source /opt/python/current/env