Bring Secrets Into Your Python Projects Using OS

So we’ve all seen that secrets page in the tools section but what does it actually do? Well the massive clue is in the name, It keeps data like passwords and/ or anything else you want secret. This tutorial will show you have to set a variable to the data you put in your secret.

First up your going to need to go into the tools menu and click on the secrets icon and then what you need to do is to click the New Secret button in the top left and once you’ve done that it should say Key: Secret_Key Value:… and what you need to do for Key is name your secret for later, I called mine Example and then name the Value to your Password/ Hidden data and then what you need to do is go to your main.py file and import os with the code import os and then copy and paste this code in: your_variable_here = os.environ['Example'] and once you’ve done that you need to switch out my Example key with your key and then you need to change the variable to anything you can remember/ want to use. Thanks for reading and I hope this is useful to you!

Was This Helpful?

  • Helpful
  • Not Helpful
0 voters
2 Likes

I think you should include a full code snippet.

import os

secret = os.environ['VERY SECRET SECRET']
5 Likes

Yes that is a good idea thanks.

1 Like

@HenryMiles3 You should mention that doing this is also possible:

import os
os.getenv('SECRET_KEY')

CC: @QwertyQwerty88 Maybe make the OP’s post as a wiki?

2 Likes

Only if @HenryMiles3 wants me to.

2 Likes

Yeah, and you can specify a default value

:x:

try:
    secret = os.environ['SECRET_KEY']
except KeyError:
    secret = 'value'

:white_check_mark:

secret = os.getenv('SECRET_KEY', 'value')

I mean Idk when you’d use it but it could be useful.

1 Like

Won’t this be unsafe? People can see the value.

1 Like

Well you’re not supposed to set it to the secret’s value. Like I said

I think it’s better to just leave the default value out and use if secret is not None ... else

1 Like

I have seen that way before.

2 posts were merged into an existing topic: What is a wiki post?

This could also be used to create a simple login system.

I’ve done it myself.

Me too! I made a special Admin login so that I can cheat the system of my game.
Secrets are really useful!

Yes they are! Thats funny, I’m kinda bad at python though so still working out ideas for text games.

I think I’m ok for this to be turned into a wiki.