I'm using Django environ and when I try to get a value from .env it raise the following exception
django.core.exceptions.ImproperlyConfigured: Set the FRONTEND_URL environment variable
I load with the command:
env = environ.Env()
environ.Env.read_env()
frontend_url = env('FRONTEND_URL')
in the .env file I have:
FRONTEND_URL=http://127.0.0.1:8000
I tried to add " to the value and the .env file is in the same folder manage.py.
What is the problem here and how can I solve it?
You need to move .env file next to the same directory which attached commands:
env = environ.Env()
environ.Env.read_env()
frontend_url = env('FRONTEND_URL')
Related
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.
I'm trying to build a website with django and I get this error
(venv) C:\Users\Admin\Desktop\Python\py_website_django>django-admin startproject pyshop
(venv) C:\Users\Admin\Desktop\Python\py_website_django>python manage.py runserver
C:\Users\Admin\AppData\Local\Programs\Python\Python39\python.exe: can't open file 'C:\Users\Admin\Desktop\Python\py_website_django\manage.py': [Errno 2] No such file or directory
Because you create a project folder inside your py_website_django folder. manage.py will be inside pyshop folder not in py_website_django.
I suggest you to create your project like this:
django-admin startproject pyshop .
adding a dot at the end will create the project inside the current directory, also, manage.py will be inside your current directory.
you need to cd into pyshop and then run the command. thats where manege.py should be
Just do :
cd pyshop
Then do
python manage.py runserver
You know, sometimes it's just a conflicting naming issue you might have
Try this: Redo all the configuration from scratch and when typing a command line in the terminal:
django-admin startproject 'your project name'
ADD A "SPACE" AND A "." the line would be:
django-admin startproject 'your project name' .
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.
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.
I've a Django project which runs in virtual environment.
Also there is line export ENV=staging in .bashrc file.
And in settings I try to read this using os.getenv('ENV') but it returns None.
settings_staging.py
...
ENV = os.getenv('ENV')
...
.bashrc
...
export ENV=staging
...
Error
[dev.gipi] out: File "/home/ubuntu/projects/deeyoon/settings/settings.py", line 61, in <module>
[dev.gipi] out: raise Exception('Environment variable ENV is requried!')
[dev.gipi] out: Exception: Environment variable ENV is requried!
What may cause the problem or what is goin wrong with?
Sultan.
There is one more case when fabric ignores .bashrc.
Often .bashrc contains following line:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
Comment it out.
Common problem is that .bashrc file is never executed. Type env and check if ENV variable is there.