Running Flask in Windows doesn't see environment variable - python

I want to set an environment variable DATABASE_URL that will be read by my Flask app to connect to the database. I use set DATABASE_URL = '...', but I get an error that the variable is not set when I do flask run. Why isn't this working?
import os
from flask import Flask
from sqlalchemy import create_engine
app = Flask(__name__)
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
engine = create_engine(os.getenv("DATABASE_URL"))
I navigate to the installed directory for project1 and do the following in Windows 10 cmd.exe:
set FLASK_APP = application.py
set FLASK_DEBUG = 1
set DATABASE_URL = 'postgres.......' (the credential given by the Heroku account)
flask run
Flask runs, I go to localhost:5000, and get the following error:
Traceback (most recent call last):
File "c:\program files\python36\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Program Files\Python36\learningPython\web_CS50\project1\application.py", line 12, in <module>
raise RuntimeError("DATABASE_URL is not set")
RuntimeError: DATABASE_URL is not set

Just remove spaces from set command
set FLASK_APP=application.py
set FLASK_DEBUG=1
set DATABASE_URL='postgres.......'
More at https://ss64.com/nt/set.html

A quick idea to try:
On Windows, use setx instead of set to modify the system environment variables.
setx FLASK_APP = application.py
Start a new command processor to see the variables reflected in your environment (They get set when the shell is initialized).

set FLASK_APP=application.py
set FLASK_DEBUG=1
set DATABASE_URL='postgres.......' (WITHOUT THE QUOTATION MARKS, because the system put it automatically, I opened a notepad and set the password and copy paste and execute in powershell

you need to set your path in your pc's environmental varibles.
You can find your path of the python here C:\Users"your default user name "\AppData\Local\Programs\Python\Python39.
after setting up path use
pip3 install

Personally, when i used flask run, i had an issue. I get this message "'flask' is not recognized as an internal or external, operable program or batch file" what could be the issue.

Related

Brownie / Rinkeby: Unable to expand environment variable in host setting

I was following the tutorial from this video and now I'm stuck during deploying a contract to rinkeby testnet.
If I run brownie run scripts/deploy.py --network rinkeby I get an error:
BrownieProject is an active project.
File "brownie/_cli/__main__.py", line 64, in main
importlib.import_module(f"brownie._cli.{cmd}").main()
File "brownie/_cli/run.py", line 44, in main
network.connect(CONFIG.argv["network"])
File "brownie/network/main.py", line 40, in connect
web3.connect(host, active.get("timeout", 30))
File "brownie/network/web3.py", line 52, in connect
uri = _expand_environment_vars(uri)
File "brownie/network/web3.py", line 183, in _expand_environment_vars
raise ValueError(f"Unable to expand environment variable in host setting: '{uri}'")
ValueError: Unable to expand environment variable in host setting: 'https://rinkeby.infura.io/v3/$WEB3_INFURA_PROJECT_ID'
I checked the brownie-config.yaml file and .env for typing errors but haven't found anything.
brownie-config.yaml
dotenv: .env
wallets:
from_key: ${PRIVATE_KEY}
I already created an infura api and add it in the .env file as export WEB3_INFURA_PROJECT_ID=abc12345656789.
If I run the command brownie run scripts/deploy.py everything works fine so I can exclude any typo. Does someone have an idea what's the problem?
I use Brownie v1.17.2
I believe you are not loading environment variable in your file. install python-dotenv
pip install python-dotenv
In your deploy.py if .env is in the same directory:
import os
from dotenv import load_dotenv
#default directory for .env file is the current directory
#if you set .env in different directory, put the directory address load_dotenv("directory_of_.env)
load_dotenv()
then use it like this:
private_key=os.getenv("PRIVATE_KEY")
Typically, this means your environment variables are not set correctly, and it looks like in this case it's your WEB3_INFURA_PROJECT_ID.
You can fix it by setting the variable in your .env file and adding dotenv: .env to your brownie-config.yaml.
brownie-config.yaml:
dotenv: .env
.env:
export WEB3_INFURA_PROJECT_ID=YOUR_PROJECT_ID_HERE
Remember to save these files.
Additionally, you should be on at least brownie version v1.14.6. You can find out what version you're on with:
brownie --version
If you know how to set environment variables you might want to check if you're setting them correctly.
if you are sure you set the .env correctly you can try sourcing your .env file in the terminal:
source .env
and then try running your script again.

ModuleNotFoundError: No module named 'hello'

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

Executing bash commands from flask application

Application Details: Ubuntu 16.04 + flask + nginx + uwsgi
I am trying to execute a bash command from flask application.
#app.route('/hello', methods=('GET', 'POST'))
def hello():
os.system('mkdir my_directory')
return "Hello"
The above code run successfully but doesn't create any directory. Also it creates directory on my local which doesn't have any nginx level setup.
I also tried following ways:
subprocess.call(['mkdir', 'my_directory']) # Throws Internal server error
subprocess.call(['mkdir', 'my_directory'],shell=True) # No error but directory not created
subprocess.Popen(['mkdir', 'my_directory']) # Throws Internal server error
subprocess.Popen(['mkdir', 'my_directory'],shell=True) # No error but directory not created
Do I need any nginx level configuration changes.
Finally I got the point. I followed Python subprocess call returns “command not found”, Terminal executes correctly
.
What I was missing was absolute path of mkdir. When I executed subprocess.call(["/bin/mkdir", "my_directory"]), it makes the directory successfully.
The above link contains complete details.
Also I would be thankful if anyone will describe the reason that why I need to specify absolute path for mkdir.
Thanks to all. :)

Cannot get environmental variables in Python sometimes?

So I have a Django/Python 3.4.3 setup with nginx, gunicorn and postgres on Ubuntu Server 14.04. The server is blank and setup following this guide. I have set a few environmental variables in the /etc/environment as follows and rebooted:
DJANGO_DB_NAME="db"
DJANGO_DB_USER="username"
DJANGO_DB_PASSWORD="password"
DJANGO_SECRET_KEY="9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko#_yn6%h"
So from Django's settings.py file I try to access it in a variety of ways, but ran into unexpected behavior:
'NAME': os.getenv('DJANGO_DB_NAME') # this works correctly
'NAME': os.environ.get('DJANGO_DB_NAME') # this works correctly
'NAME': os.environ['DJANGO_DB_NAME'] # this does NOT work and yields 'key' does not exist
None of these works as it returns an empty string instead of the key value:
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
Django Error:
File "/webapps/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 120, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
From within Ubuntu, when I access the environment variables from the command line, I always get the correct result back:
root#ubuntu-512mb-sfo1-01:/webapps# echo $DJANGO_SECRET_KEY
9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko
root#ubuntu-512mb-sfo1-01:/webapps# echo $DJANGO_DB_USER
username
Yet, when I do this from command line it works!
root#ubuntu-512mb-sfo1-01:/webapps# python3 -c "import os; print(os.environ['DJANGO_SECRET_KEY'])"
9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko
Now, I am really confused. Any expert know what is going on and how to solve this?
Update 1: per comments by m.wasowski, gunicorn is running as root and running manage.py runserver works just fine again as root. Gunicorn only complains when I run 'service gunicorn start'. Security issues as running as root, or storing key in environment is temporary until I just get it working first.

"Operation not Permitted" for Redis

I am developing on a mac which already have redis installed. by default it doesn't have a redis.conf so the default settings were used when I $ redis-server
# Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'
I am trying to use redis-py and have the following
import redis
r = redis.Redis('localhost')
r.set('foo','bar')
r.get('foo')
but got the following error
redis.exceptions.ResponseError: operation not permitted
I also tried in terminal $ redis-cli ping, but then i get the following error
(error) ERR operation not permitted
I suppose since there is no redis.conf the default settings doesn't have a password right? Anyways, I also tried to create a redis.conf
$ echo "requirepass foobared" >> redis.conf
$ redis-server redis.conf
then on another window
$ redis-cli
$ redis 127.0.0.1:6379> AUTH foobared
(error) ERR invalid password
also modified the second line of the python script to
r = redis.StrictRedis(host='localhost', port=6379, db=0, password='foobared')
but then I got
redis.exceptions.ResponseError: invalid password
what could I be doing wrong??? Thanks
Without any redis.conf file, Redis uses the default built-in configuration. So the default database file (dump.rdb) and the Append Only File are created in the server directory. Maybe the user running Redis does not have write permission on this dir.
So either you give him the permission, or you define another working directory using a config file.
You'll find a default config file for redis 2.6 here
You must modify this line in the file:
# The working directory.
dir ./

Categories

Resources