- Environment Variables
Store your passwords and other sensitive information in environment variables. You can access these variables in your Python code using the os module.
import os
password = os.getenv('MY_SECRET_PASSWORD')
- .env Files
Use a .env file to store your environment variables and load them using the python-dotenv package. Make sure to add the .env file to your .gitignore to prevent it from being committed to your repository.
Create a .env file:
MY_SECRET_PASSWORD=your_password_here
Install python-dotenv:
pip install python-dotenv
Load the .env file in your Python script:
from dotenv import load_dotenv
import os
load_dotenv()
password = os.getenv('MY_SECRET_PASSWORD')
- GitHub Secrets (for GitHub Actions)
If you're using GitHub Actions, you can store your secrets in GitHub's encrypted secrets storage.
Go to your repository on GitHub.
Navigate to Settings > Secrets and variables > Actions.
Add a new secret with your password.
In your GitHub Actions workflow, you can access these secrets:
jobs:
example-job:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run script
env:
MY_SECRET_PASSWORD: ${{ secrets.MY_SECRET_PASSWORD }}
run: python your_script.py
- Configuration Files (with .gitignore)
Store your passwords in a configuration file that is not committed to your repository by adding it to your .gitignore.
Create a configuration file, e.g., config.py:
MY_SECRET_PASSWORD = 'your_password_here'
Add config.py to your .gitignore:
config.py
Import the configuration file in your script:
from config import MY_SECRET_PASSWORD
By following these methods, you can ensure that your passwords and sensitive information remain secure and are not exposed in your GitHub repository.
Store your passwords and other sensitive information in environment variables. You can access these variables in your Python code using the os module.
import os
password = os.getenv('MY_SECRET_PASSWORD')
Use a .env file to store your environment variables and load them using the python-dotenv package. Make sure to add the .env file to your .gitignore to prevent it from being committed to your repository.
Create a .env file:
MY_SECRET_PASSWORD=your_password_here
Install python-dotenv:
pip install python-dotenv
Load the .env file in your Python script:
from dotenv import load_dotenv
import os
load_dotenv()
password = os.getenv('MY_SECRET_PASSWORD')
If you're using GitHub Actions, you can store your secrets in GitHub's encrypted secrets storage.
Go to your repository on GitHub.
Navigate to Settings > Secrets and variables > Actions.
Add a new secret with your password.
In your GitHub Actions workflow, you can access these secrets:
jobs:
example-job:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run script
env:
MY_SECRET_PASSWORD: ${{ secrets.MY_SECRET_PASSWORD }}
run: python your_script.py
Store your passwords in a configuration file that is not committed to your repository by adding it to your .gitignore.
Create a configuration file, e.g., config.py:
MY_SECRET_PASSWORD = 'your_password_here'
Add config.py to your .gitignore:
config.py
Import the configuration file in your script:
from config import MY_SECRET_PASSWORD
By following these methods, you can ensure that your passwords and sensitive information remain secure and are not exposed in your GitHub repository.