Python .env and .gitignore

What is .env file?

  • The .env file is used in projects to store configuration settings, environment variables, and sensitive information securely. It provides a convenient way to manage and organize various parameters that your project needs without hard-coding them directly into your code.

Importance of .env file

  • .env file improves security, flexibility, and maintainability, while ensuring good development practices by externalizing configuration settings from the codebase.

What is .gitignore file?

  • The . gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. The .gitignore file itself is a plain text document.

Importance of .gitignore

  • The .gitignore file is essential for protecting sensitive data, maintaining a clean and efficient repository, improving team collaboration, and ensuring that unnecessary or temporary files are not included in the version control system.

Python Flask .env and .gitignore step-by-step guide on my Activity 37

Step # 1 - Make a folder where you will save your virtual environment and also your python flask with .env and .gitignore files

Step # 2 - Use command prompt and go to your folder using cd or change directory and dir or directory commands

Step # 3 - Now do this command to make virtual environment and also check it if it’s now exist inside the folder

Step # 4 - Now run the virtual environment using this command and install python flask and python dotenv and you can update it to latest version

Step # 5 - Now use this command to make a .gitignore file and .env file

Step # 6 - Now freeze the dependencies requirements needed for your application so that if someone use your application they don’t need to manually install the needed dependencies

Step # 7 - Now open your application to any ide or text editor you want and make app.py file and add this code inside app.py file

from flask import Flask, jsonify, request
from dotenv import load_dotenv
import os
load_dotenv()

app = Flask(__name__)

SECRET_KEY = os.getenv("SECRET_MESSAGE")

secret = [
    {
        "message" : SECRET_KEY
    }
]

@app.route("/secret", methods=['GET'])
def get_secret_message():
    return jsonify(secret)

if __name__ == '__main__':
    app.run(debug=True)

Step # 8 - Now do this to your .env file but remember all of the information inside .env file should be private and not share to public i just show this because this is only for the guide

Step # 9 - Now do this to your .gitignore file so that when you upload your application to github repository the virtual environment and .env file will not be uploaded

Step # 10 - Now go back to your command prompt and run the application using this command

Step # 11 - Now go to postman application and login your account and make add a new collection rename it to secret or whatever you want and add a request rename it to GET SECRET MESSAGE use GET request and add this link and save

Step # 12 - Now click the send button and if you receive this message it means that it is successful and working

References

https://medium.com/@sujathamudadla1213/what-is-the-use-of-env-8d6b3eb94843#:~:text=The%20.,them%20directly%20into%20your%20code.

https://www.bmc.com/blogs/gitignore/#:~:text=file(s).-,The%20.,is%20a%20plain%20text%20document.

https://github.com/BrandonLCanete/canete_env_gitignore.git

0
Subscribe to my newsletter

Read articles from Cañete,Brandon L. directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cañete,Brandon L.
Cañete,Brandon L.