HOW TO: Hide Sensitive info in Python Using Environment Variables?

Опубликовано: 01 Апрель 2026
на канале: chinamatt
838
16

HOW TO: Hide Sensitive info in Python Using .env?
• You want to use secrets and tokens in your script but don’t want to hardcode them in as strings. We can use environment variables instead.
• Here are the steps:
1. Create `.env` file in the root directory of your project
2. Inside type: `export authToken='yourAuthToken';`
3. In your terminal run: `source .env`
These environment variables only persist for this session. When you open a new terminal, you will have to run source .env again to use the keys.
4. In your python project, you can use your token using the os.environ.get method, like this:
```python
import os

authToken = os.environ.get('authToken', None)

print(authToken)
```


4.1 In Jupyter Notebook
```python
from dotenv import load_dotenv

load_dotenv()
```

5. Make sure to add .env to .gitignore if you want to share your code on GitHub.


References:
https://www.zeolearn.com/magazine/how...
  / how-to-not-show-credential-in-jupyter-note...  

__________________________________________
No Copyright Background Music
Music by INOSSI - Chill Out
   • INOSSI - Chill Out (Official)  
__________________________________________