1

I am using a python API module called SpaceTrackTools. However, it requires the script to have the password and username stored as variables in the script. I was wondering if there is a safer way of saving the details?

Thanks.

1
  • Are you familiar with keyring? Commented May 18, 2020 at 2:53

3 Answers 3

3

You can use keyring to store your username and password outside your script. A command-line interface allows you to get, set, or delete a stored password. You can store your username as if it were a password to keep it secure.

$ keyring --help
Usage: keyring [get|set|del] SERVICE USERNAME

Options:
  -h, --help            show this help message and exit
  -p KEYRING_PATH, --keyring-path=KEYRING_PATH
                        Path to the keyring backend
  -b KEYRING_BACKEND, --keyring-backend=KEYRING_BACKEND
                        Name of the keyring backend
  --list-backends       List keyring backends and exit
  --disable             Disable keyring and exit

Set the username and password for the service from the command line.

$ keyring set spacetracktools username
Password for 'username' in 'spacetracktools': 
$ keyring set spacetracktools password
Password for 'password' in 'spacetracktools': 
import keyring

# get username and password from keyring
username = keyring.get_password("spacetracktools", "username")
password = keyring.get_password("spacetracktools", "password")

print("My username is:", username)
print("My password is:", password)
Sign up to request clarification or add additional context in comments.

Comments

3

One option is to use dotenv.

This way you keep your private details in a separate file named .env.

Then you read the private details in your script at run time.

For Example:

File .env values:

USERNAME=MyUserName
PASSWORD=Spec!alP@ssw0rd*19?

Script Usage:

import os  # also need os
from dotenv import load_dotenv

load_dotenv()  # blank if .env file in same directory as script
# load_dotenv('<path to file>.env') to point to another location
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')

# your code

Comments

1

Adding to the .env approach please make sure you have a .gitignore file that is set so that git will not include the .env file in the repo. You can find a .gitignore file for python at the Github gitignore repo here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.