Ask coding questions

← Back to all posts
3
Python 3/ Secret Keys for everyone
Zertyazertyu (2)

Hello, i'm a newbie on repl.it, so I cheked the doc to see how we can hide files (with .env file). It works fine for some projects but now i want to make a kind of console that requests username and password to start (if the guy enters his logins he gets a key to decode the content of other files to read them). My code works.. only for me ! The reason is that .env files are protected for others peoples so i can't really finish my project (it's for a RP game)

Is there any alternatives to .env files ? I want that only the program can read .env files but users can't directly access to it with filetree.

I'm on python 3 but if solutions are only on others languages I can migrate
Sorry for my bad english, I'm french

Commentshotnewtop
2
heyitsmarcus (285)

You can use a package called python-dotenv. Load it from the Package Manager in the sidebar (search for python-dotenv and click the + icon).

I did a simple little .env program in Python here: https://repl.it/@heyitsmarcus/Load-from-env

#THESE LINES ARE NECESSARY TO IMPORT FROM .env FILE
from dotenv import load_dotenv
import os

load_dotenv()

#my .env has a key that is this:
#SECRET="abc"

#so now I'm grabbing SECRET from the .env file
SECRET = os.getenv("SECRET")

#enter abc as the input to test
key = input("Enter secret key: ")
if key == SECRET:
	print("Well done!")
else:
	print("Not today, Satan.")

If this helped you, please consider upvoting and selecting my answer as the accepted answer.

1
heyitsmarcus (285)

@Zertyazertyu, also here is the URL for the python-dotenv package's website: https://pypi.org/project/python-dotenv/

1
Zertyazertyu (2)

@heyitsmarcus Thanks you for your help but the problem is that when somebody launches the program on https://brouillon-test-poubelle.zertyazertyu.repl.run/ for example the .env is not imported so i've got the wrong password message (the .env contains SECRET=kok). I need a program that hide the .env or other other file for others but that can be read by the console of no owners users ( know that if the program reads the file, it's imported but this is a game soft not a nasa one so security is not very important while password file is hidden)

But thanks you for your response !

1
heyitsmarcus (285)

@Zertyazertyu The .env is not exposed when it is loaded with dotenv. And you didn't load the package. The repl you linked me to doesn't work, at least, because you have to import the package into your repl.

1
Zertyazertyu (2)

@heyitsmarcus
Hello !
I'm really sorry but your solution don't work as I want.
I created a main file with:

import dotenv
import os

dotenv.load_dotenv()

print(os.getenv("SECRET"))
print(dotenv.find_dotenv(".env"))

So the code worked for me in my own environnement, but when I try to launch it with the console link https://Brouillon-test-poubelle.zertyazertyu.repl.run the .env is hidden so there are no secret key and no file path...

I need that other peoples can use a login and password but without being able to easily read the file that contains login infos

Is there any solution for this specific case ? Or do i need to create a flask server ? It could be really annoying

Thanks for your patience

1
heyitsmarcus (285)

@Zertyazertyu Can you please post the entire contents of your .env currently? I've tested this inside repl.it and outside of it, and both are working flawlessly.

1
Zertyazertyu (2)

@heyitsmarcus

No problem, thanks for your help:

main.py

import dotenv
import os

dotenv.load_dotenv()

print(os.getenv("SECRET"))
print(dotenv.find_dotenv(".env"))

.env

SECRET=abc

requirements.txt

python-dotenv==0.10.2

When I launch the repl from my editing page i get a working output:

abc
/home/runner/.env

And "None" displays when it's used from https://brouillon-test-poubelle.zertyazertyu.repl.run/

1
Zertyazertyu (2)

@heyitsmarcus
Done, but same result; got a None (the path of .env is aslo undisplayed

On the repl that you made for me I got a similar result, even if I enter "abc" I got a wrong password message

1
heyitsmarcus (285)

@Zertyazertyu It's interesting that the repl run is having trouble with it. But, the fact of the matter is, that the code works, it just doesn't work with repl's run feature.

1
heyitsmarcus (285)

@Zertyazertyu I changed it to be USERNAME and PASSWORD. I put both in the comments of the repl.

1
Zertyazertyu (2)

@heyitsmarcus Yes that's all the problem ! This is like the .env is existing only when the owner of the repl launches it from the edit page.. so annoying... If there are no alternative to getenv i unfortunally think that i'll use a flask server :-(

1
heyitsmarcus (285)

@Zertyazertyu This is definitely a repl.it issue, not a Python or dotenv package issue. You could program this outside of repl.it and use the package, but if you don't, you may be better off with a flask server.

1
Zertyazertyu (2)

@heyitsmarcus I got the same problem when I try to launch your repl

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    print(".ENV USERNAME: " + USERNAME)
TypeError: must be str, not NoneType

The getenv function returns a None type when it fails (no .env file in this case)

1
Zertyazertyu (2)

@heyitsmarcus Okay, thanks you ! I'll hope that it will be patched in future !