From 12c91d26809e6b19fed9d52585050d646e523e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Wed, 28 Dec 2022 18:29:01 -0300 Subject: [PATCH] =?UTF-8?q?Vari=C3=A1veis=20de=20ambiente=20com=20Python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env-example | 4 ++++ aula184.py | 22 ++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 27 insertions(+) create mode 100644 .env-example create mode 100644 aula184.py diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..9831f04 --- /dev/null +++ b/.env-example @@ -0,0 +1,4 @@ +BD_USER="CHANGE-ME" +BD_PASSWORD="CHANGE-ME" +BD_PORT=CHANGE-ME +BD_HOST="CHANGE-ME" diff --git a/aula184.py b/aula184.py new file mode 100644 index 0000000..097dd1c --- /dev/null +++ b/aula184.py @@ -0,0 +1,22 @@ +# Variáveis de ambiente com Python +# Para variáveis de ambiente +# Windows PS: $env:VARIAVEL="VALOR" | dir env: +# Linux e Mac: export NOME_VARIAVEL="VALOR" | echo $VARIAVEL +# Para obter o valor das variáveis de ambiente +# os.getenv ou os.environ['VARIAVEL'] +# Para configurar variáveis de ambiente +# os.environ['VARIAVEL'] = 'valor' +# Ou usando python-dotenv e o arquivo .env +# pip install python-dotenv +# from dotenv import load_dotenv +# load_dotenv() +# https://pypi.org/project/python-dotenv/ +# OBS.: sempre lembre-se de criar um .env-example +import os + +from dotenv import load_dotenv # type: ignore + +load_dotenv() + +# print(os.environ) +print(os.getenv('BD_PASSWORD')) diff --git a/requirements.txt b/requirements.txt index b49d4bf..28f2742 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ mypy-extensions==0.4.3 pycodestyle==2.10.0 pyflakes==3.0.1 python-dateutil==2.8.2 +python-dotenv==0.21.0 pytz==2022.6 six==1.16.0 tomli==2.0.1