Setting and Getting Environment Variables in Python
imgsrc: itenium.be

Setting and Getting Environment Variables in Python

Environment variables are useful when you want to avoid hard-coding access credentials or other variables into code. Also if you need your code to function differently between development, staging, and production environments, use environment variables.

Store environment variables in a .env file as global constants (ALL CAPS) and accessed using Python’s dotenv module. If using git, remember to add .env to your .gitignore file so you don't commit this file of secrets to your Git Repository. You apparently do not need quotes for your string values.

In your code, you can create a .env.example file as a template for the environment variables with the same parameter keys but without the sensitive values eg.

# DB
DB_USER=
DB_PASSWORD=
# Cloud Public URL
CLOUD_URL=192.168.1.1B        

then push the .env.example to the GIT repo.

Should .env file be used in a Production environment? Some sources (see below) state that you should use the file in Development to test but it is not recommended to deploy a .env file to the production environment. Production configuration and secrets should/could be stored directly in a server environment at a /etc/environment file or via Cloud's services console. Cloud service providers also provide secret storage services to store encryption credentials, configuration endpoints such as AWS systems Manager Parameter Store, Azure Key Vault, Google Cloud Secret Manager.

I did see a post recently on medium (referenced below) which discusses searching for environmental secret variables on google.

To search for public .env files, all you need to do is google one of these terms:

DB_USERNAME filetype:en
APP_DEBUG filetype:env
DB_PASSWORD filetype:envv        

The reason these .env file are accessible and are able to get scraped is because two things: 1) misconfigured file hosting and 2) the .env file has the wrong access rights. On shared hosting make sure your root folder is not accessible from the outside and only the "public" folder should be accessible from the internet. Modify your .env file access rights to: 400 or 440 so that it can not be accessed by public users

The intent of all this was to upload an.env file with my previous python script that gathered my book reading information from my mongodb and allow individuals to generate the html to see what I’ve read BUT I realized that I can just generate the html myself and post the link to the html. So here is the link to books I’ve read recently with a semi coherent, semi short summary of the book so I can remember what the book was about since I’m old :)

I do plan on updating the code because the formatting is not the best.

https://108.175.7.38/webbrowser.html

REFERENCES: https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5

https://developers.refinitiv.com/en/article-catalog/article/how-to-separate-your-credentials--secrets--and-configurations-fr

https://medium.com/@marcelpociot/protect-your-env-files-387b4f66d809

https://www.askpython.com/python/python-dotenv-module

要查看或添加评论,请登录

Ronnie Livingston的更多文章

  • Wanna Play?

    Wanna Play?

    I have a 6 year old and although sometimes I need to do something else or I’m tired, I do end up saying yes to my son…

  • Adabox 8: a crickit manifesto

    Adabox 8: a crickit manifesto

    I've squandered my free time while Adabox was on a hiatus/hibernation during the pandemic. I just received my Adabox 21…

  • p5.js

    p5.js

    I ran into four videos on YouTube HACKADAY labelled as “HackadayU: Art + Code” classes. They used p5.

  • 95 books

    95 books

    I’m in the middle of a book now, Shadow of the Giant, here on New Year’s Eve but will not complete the book until the…

    1 条评论
  • Yfinance fail!

    Yfinance fail!

    I was hoping to analyze and automate the prices of what little stocks I have with the Python Yfinance library. The…

  • Python Turtle

    Python Turtle

    While I normally like drawing with a ball point pen (check out my instagram: https://www.instagram.

  • Webhooks: easy and light and uses less resources as syslog?

    Webhooks: easy and light and uses less resources as syslog?

    I can’t find the video anymore but I heard about webhooks from a GrimmCon youtube video that I watched previously…

  • Eisenhower Matrix

    Eisenhower Matrix

    I’m a big proponent of using lists, planners, checklists to track tasks that I’m working on. Previously I’ve used the…

  • My Vietnam war story

    My Vietnam war story

    A few months ago I saw a USA Today news piece about the US troops officially withdrawing from Vietnam on January 27…

  • Cyber resilience blog using ChatGPT

    Cyber resilience blog using ChatGPT

    I'm not sure how I feel about this but using the below notes, I prompted ChatGPT to “CREATE A BLOG ABOUT CYBERSECURITY…

社区洞察

其他会员也浏览了