I defined my environment variables inside env/bin/activate and the following came out from the output when i activated them
debug="True"
secret="RI4MgORxGb4c6zhotTHSNXc54lz1IWGbIoETfmj2VB99sPNlk9YMcg4b6qyX"
using echo in zsh. Now that tells me that the environment variables are set properly. I am getting the same inside django settings using
DEBUG = True if os.environ.get("debug") == "True" else False
The problem comes when i run
python manage.py runserver
which is unable to find those variables as opposed to
env debug='True' python manage.py run server
which works fine in debug mode and production depending on environment variables. What am i missing over here ? is this something because of using zsh shell or am i getting the variables wrong way somehow ?
In many shells, setting a variable only does so for the shell process itself. If you want to turn a shell variable into an environment variable then you need to export the variable, usually with the export command.
Related
I developed a Python package on Linux, which works and my pytest succeeds.
I am making it Windows compatible.
I have say 'my_package' that sits in say 'C:\import_path\my_package'
I set the PYTHONPATH at the command line in my virtual environment like so:
set PYTHONPATH="C:\import_path"
echo %PYTHONPATH%
"C:\import_path"
# Python script identifies this
print('PYTHONPATH = {}'.format(os.environ.get('PYTHONPATH')))
"C:\import_path"
However any Python code referencing my_package when run from a Command Prompt where this PYTHONPATH has been explicitly set, will error:
ModuleNotFoundError: No module named 'my_package'
However the code will run fine with PYTHONPATH = C:\import_path being set in the System Environment variables.
It also runs fine in VSCode with:
"python.analysis.extraPaths": [
"C:\\import_path"
]
Why is python.exe not reading the %PYTHONPATH% variable that was set in the cmd, but python.exe will read it when running in debug from VSCode or from the os system env variable?
In all probability you are running from different shell session or calling different shell sessions and the variable is not parsed to all sessions. You could try setting it like this:
setx PYTHONPATH "C:\import_path" /M
When you set a variable you should set it system-wide if you intend to use multiple sessions.
I am using Elpy on Emacs and using pipenv. When I start an Elpy Python shell or send the current buffer to the shell, I cannot get the environment variables that I set in the terminal. But I would very much like to be able to send my code and env vars over to the Elpy shell, how can I do that?
The virtual environment is works fine because all my packages are there; also, if I use the pipenv-shell, I can access my env vars.
I checked on os.environ and it shows that I'm using the correct virtual environment, and everything else looks just about right there, just none of the variables I set myself. Is there a way to set the environment variables from Elpy? I looked at the documentation, but I am still unclear on this. I would rather use the OS environment variables than "project variables" or anything like that because I need to be able to edit and run the code outside of Emacs as well.
Edit: OK, I realized that I can use M-x setenv to set the environment variable, which does what I need, but I would still like to know how to get Elpy to recognize already set environment variables.
I have an existing Python 3.7 project. When adding a new variable to the .env, it is not loading.
Here is an example of my .env file.
# Existing
DB_HOST=localhost
DB_PORT=3307
# New variable
API_BASE_URL=https://testing.mysite.com.au/api/v1/
# Load api settings
api_base_url = os.getenv('API_BASE_URL')
The database variables are working fine however, api_base_url returns None.
I don't believe it is a path/load_dotenv issue, as the existing variables are loading correctly.
Do I need to refresh/reload the .env file?
What environment management system are you using? I think the .env file is a pipenv thing, if so the .env file is only sourced when you call pipenv run ... or pipenv shell. So if you're working in a python you need exit the shell or run.
If you're on a linux machine you can also just source .env to assign the environment variables within the shell.
If you use any IDE like visual studio code / Pycharm or similiar you have probably to reload it after changing .env
Not knowing your exact environment and not knowing whether you use autoenv or any other tool it might also be a good choice to explicitly source .env as #it's-yer-boy-chet suggested.
If you're using autoenv, you just had to type
cd . and it shoud prompt you whether you want tou source the modified version of .env
I've got a flask application and it mostly runs smoothly and returns what is needed. I've got it configured with a virtual environment. and when ran in shell it works perfectly, the problems comes when I run it inside emacs. It seems that some Venv variables are not being set at the moment of running the variables are set inside the "activate" script.
The question is, why are these variables not being set.
Any help is much appreciated.
I am working with django and virtualenvwrapper. My objective is to remove all sensitive information from the settings file per the 12Factor app suggestions (http://12factor.net) and ultimately deploy to heroku. When testing this locally, to achieve this, I have created a .env file with different variable values like SECRET_KEY. I went to my virtualenv directory and added the following line to the postactivate script:
source .env
Whenever I start my virtual env for a project aka workon project_name, the environment variables from .env are available if I echo from the terminal
$ echo $SECRET_KEY
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
However when I try to access those variables from python they are unavailable
>>> import os
>>> os.environ.get('SECRET_KEY')
>>>
What is the correct way for python to access the environment variables stored in a .env file?
.env file:
WEB_CONCURRENCY=2
SECRET_KEY='XXXXXXXXXXXX'
DEBUG=True
I think your problem is that you are defining it in your current shell by doing SECRET_KEY=xxxxxxx, but when you open up a python shell, it's running in a sub process and you lost the environment variable in that shell. export will make the variable available in sub process as well.
You should have:
export SECRET_KEY=xxxxxxxx
In your .env file to make it work.
Edit:
From what I read from your links, that's just a normal linux shell environment variable. But django needs to have SECRET_KEY as a python constant in the settings. Linux environment variables and python variables are two different things, so defining a env variable SECRET_KEY doesn't let django recognize settings.SECRET_KEY. You should still consider using separate settings file, which is mostly recommended.