#🔒 Storing persistent env vars

36 messages · Page 1 of 1 (latest)

near lichen
#

Im using the spotipy library and want to store my client id and other necessary passwords so that i dont need to enter the credentials each time i run the script. any ideas on hoiw to do this?

stray spearBOT
#

@near lichen

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tender comet
#

i recommend using a dotenv file and this library to read/write to it

#

if you're using git, make sure the dotenv file is in the .gitignore

near lichen
tender comet
#

is spo.py your project?

near lichen
#

yes

tender comet
#

ok 2 things:
your project should be a folder containing your virtual environment and your source code (and whatever else is needed for the project), your source code shouldn't be inside your virtual environment (that's what spotify-env is)

#

so it should be something like

my-spotify-project
    env
        bin
        blah blah
    spo.py
near lichen
#

oh right, i just watched the spotipy tutorial on how to set it up

#

ok ill sort that out

tender comet
#

secondly, the virtual environment is a totally different thing that a .env file (I know it's confusing with the similar terms)

near lichen
#

like this?

tender comet
#

yeah

tender comet
#

your OS keeps track of some environment variables, which you can use to store private credentials
the dotenv module allows you to load a .env file as if its contents were from the OS environment

near lichen
#

right ok

#

got to try and get my head around it lol

tender comet
#

storing them in a file this way makes them a little easier to see and edit and keeps from polluting the global environment

#

adding the .env file to .gitignore makes sure you dont publish your private credentials along with your source code

near lichen
#

alright

#

where does gitignore go

tender comet
#

that would go in the top level of your project folder, but its only relevant if you're using git

near lichen
#

im not using git atm

#

so ill leave it out

#

where should the dotenv go

tender comet
#

also at the top level

near lichen
#

within the spotify-env?

#

or out of it

tender comet
#

you shouldnt put anything inside the virtual env, that's just a copy of a python interpreter for your project to use

near lichen
#

oh alright

stray spearBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.