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

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.

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

failed getting value from django .env

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')

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.

Where is the luigi config file?

I have installed luigi by pip command and I would like to change the port for the web UI. I tried to find the config file but I couldn't. Do I need to create one?
You can start luigid with the --port option.
luigid --port 80
Configuration file locations are:
/etc/luigi/luigi.cfg
luigi.cfg (or its legacy name client.cfg) in
your current working directory
LUIGI_CONFIG_PATH environment variable
in increasing order of preference. You do need to create one. e.g.,
[core]
default-scheduler-host=www.example.com
default-scheduler-port=8088
In spite of the documentation saying otherwise, the port configuration from the config file is not used, at least in some versions or some circumstances
Until this is resolved, you should always use the --port option of luigid:
luigid --port 12345
Also see https://github.com/spotify/luigi/issues/2235
For other configuration options a config file should be used. See https://luigi.readthedocs.io/en/stable/configuration.html
For a configuration global to the host you can create a file:
/etc/luigi/luigi.cfg
Make sure it is readable by the user that runs luigid and luig.
Alternatively a local configuration file that will be recognized is
luigi.cfg
which you would have to create in the current working directory.
If you want a custom config file location you could set the environment variable LUIGI_CONFIG_PATH to the full path of your config file.

Run supervisor in virtual environment

I installed supervisor and gunicorn in my virtual environment (venv).
I am using this tutorial: https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/
I'm confused as to where I should be creating the config file for supervisor as the default etc/supervisor won't apply to me.
The supervisorctl file is in the directory:
/home/giri/venv/py2.7/lib/python2.7/site-packages/supervisor
I noticed this line in the supervisorctl file:
Options:
-c/--configuration -- configuration file path (default /etc/supervisord.conf)
Do I need to manually set this flag each time I run the supervisorctl script or is there another way?
Thanks
As found in the docs (http://supervisord.org/configuration.html):
The Supervisor configuration file is conventionally named
supervisord.conf. It is used by both supervisord and supervisorctl. If
either application is started without the -c option (the option which
is used to tell the application the configuration filename
explicitly), the application will look for a file named
supervisord.conf within the following locations, in the specified
order. It will use the first file it finds.
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
So put the supervisor.conf in your current working directory and you're fine.

Categories

Resources