I am trying to make a blog app using django, and i want to use environment variables to hide sensitive information. I tried putting my data in .bash_profile and access it as described in the photos attached, however, it does not seem to be working. My photos and information only loads if I hardcode the data. I don’t know how to fix this problem, please help.
How I am refering to the data in .bash_profile:
AWS_ACCESS_KEY_ID = os.environ.get(‘AWS_ACCESS_KEY_ID’)
fyi, I am using ubuntu 18 and vs code to code!
Note: I made sure to import os in settings.py
Thank you!
Hmm, how are you running Django? Do you have it in a terminal? If so, is the output of echo $DEBUG_VALUE
what you expect?
Yes, @takkaria has a point - environment variables in ~/.bash_profile
are only going to work if you run django via runserver
in a Bash shell. Run it any other way and they won’t carry over - you’ll need to set them a different way (how you set them depends on how you are running it).
The env
command in a shell can show you what you have set in that shell too, so you can check you set things in the profile correctly. Remember to exec bash
when you change the file so it reloads it.
So the way I am currently running is through python3 manage.py runserver in terminal. I cd into my project folder and run the command, to launch the server. Does that prevent the bash_profile settings from being carried over?
Running the command you stated gives me no output in terminal. However, when i make a scratch .py file and using print to print the values of os.environ.get(’’) for one of the values it prints the correct value
This might be a case of bashrc
and bash_profile
confusion:
-
bashrc
gets loaded when you are in a “non-login” session (e.g. a terminal)
-
bash_profile
is when you are in a “login” session (either at the console or via ssh)
If you are putting these exports in ~/.bash_profile
then they won’t be loaded when you open a desktop terminal, but if you put them in ~/.bashrc
they should load OK.
However… do you really want those variables in there? If you ever have more than one project it’s going to get confusing. It’s probably better to have a file in your project directory with the environment variables you want to define and then load them with source <filename>
, like so:
echo "export DJANGO_DEBUG=True" > env_vars.sh
source env_var.sh
echo $DJANGO_DEBUG
# Should output "True"!
1 Like
Ohh i see. Thanks a lot for the reply.
Follow up question: If i were to save the details in another file, how would i refer to specific variables in that file. for example if i save them in bash.profile i access them using os.environ.get(’’). How would i do it for a specific file?
Thanks again!
Follow up question: If i were to save the details in another file, how would i refer to specific variables in that file. for example if i save them in bash.profile i access them using os.environ.get(’’). How would i do it for a specific file?
Why do you want to tell what file the variable comes from? You can’t refer to variables in a particular file because environment variables don’t really have that concept. Think of env vars as global variables for everything you run in a terminal: they can only have one value each and what sets them isn’t tracked anywhere.
Normally people want to have multiple sets of configs for different setups: so for example, one for local development and one for production (and maybe one for a testing server). Is this the kind of situation you’re thinking about?
EDIT: I wonder if I missed your point. Are you asking, if you put your environment variables in a file that isn’t bash_profile
, how do you load them? I tried to show it in the code example above but maybe it wasn’t clear!
- In Python you load them the same way, using
os.environ.get()
.
- In the terminal, you run
source <filename>
, where the file is the one that has the list of export X=Y
lines in it that you want to load into the environment.
I managed to solve the problem by using .bashrc. Thank you so much for all the help!
1 Like