Cannot get environmental variables in Python sometimes? - python

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.

Related

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

Why can't I get Environ variables within a Flask script?

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?

Running Flask in Windows doesn't see environment variable

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.

Twilio environment variable error

Twilio-Python works fine if I place my account_sid and auth_token directly into the code but will not work when I set them to environment variables. I'm using PyCharm and set them by going to edit configurations > Environment variables, just as I have done with other variables in the past with no problems. I reference them in my code with:
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
Which throws the following error:
twilio.rest.exceptions.TwilioRestException: HTTP 404 error: The requested resource /2010-04-01/Accounts/'<my account_sid>'/Messages.json was not found
I tried exporting the variable on the CLI and running the code there but got the following error:
KeyError: 'twilio_account_sid'
I have no idea what I am overlooking. Any suggestions?
For OS X El Capitan, add environment variables:
open terminal, then
cd ~/
vi .bash_profile
once in vi editor editing the file, get into insert mode (press i) then add these two lines:
export TWILIO_ACCOUNT_SID="AC0123456789abcdefabcdefabcdefabcd"
export TWILIO_AUTH_TOKEN="0123456789abcdefabcdefabcdefabcd"
get into vi command mode (press escape) save and quit
:wq
reboot your computer
To check environment variables after reboot, open terminal, type printenv, you should see them on the list.
In your script, to use the environment variables, try this
import os
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
For configuring Windows environment variables take a look at my answer
Twilio Auth windows enviro variables

"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