Skip to content

Instantly share code, notes, and snippets.

@birkin
Last active April 17, 2017 15:41
Show Gist options
  • Save birkin/48dbcc68d3ebbf355e69 to your computer and use it in GitHub Desktop.
Save birkin/48dbcc68d3ebbf355e69 to your computer and use it in GitHub Desktop.
Python2: how to use a single shell-file to load settings for _both_ scripts, and web-apps. Keywords: activate, django, env, shellvars, settings.
## To use django's runserver, or if your django project includes code for queued jobs,
## you or your code will be sourcing your environment: `source ./env/bin/activate`
##
## To have your sourced-environment access the settings you need, simply add the following
## to the bottom of your `activate` file.
PROJECT_ENV_SETTINGS="/path/to/settings_dir/project_x_settings/env_settings.sh"
source $PROJECT_ENV_SETTINGS
## Note: this is for python2.x
## python3.x doesn't need an activate_this.py file; python 3.x instructions will be added.
##
## example of problem:
## You want to run a django web-app under mod_wsgi or passenger, but need to run
## `python ./manage.py xyz` for testing or a shell or runserver or whatever.
##
## For terminal `python ./manage.py xyz` use, the easiest thing is to source the
## `env/bin/activate` file, to which you've added the line
## `source settings_dir/project_x_settings/env_settings.sh`
## -- then you're all set with your environment.
##
## BUT, you need all those env_settings.sh settings duplicated in another env_settings.py file
## when you run the mod_wsgi or passenger web-app -- because url access triggers your project's
## wsgi.py file, which hits the `env/bin/activate_this.py` file, to which you've added an import
## of your project_settings/env_settings.py file.
##
## The solution: a terrific little library -- shellvars-py -- which allows your webapp
## to programmaticaly go through your env_settings.sh file and load them all into the
## environment via python. It's pip-installable, too.
##
## To use, simply add the line below to your activate_this.py file, which is
## called by `project_x_code/config/wsgi.py`, and see the associated wsgi.py
## gist fragment for a few lines to add there.
##
## more shellvars info: <https://github.com/aneilbaboo/shellvars-py>
## OLD: Settings loaded from `settings_dir/project_x_settings/env_settings.py`.
# SETTINGS_DIR = '/path/to/settings_dir'
# sys.path.append( SETTINGS_DIR )
# from project_x_settings.env_settings import *
## NEW
os.environ['project_x__SETTINGS_PATH'] = '/path/to/settings_dir/project_x_settings/env_settings.sh'
## One of the main purposes of the wsgi.py file is to activate the virtual environment.
## It does that in a line something like `execfile( ACTIVATE_FILE, dict(__file__=ACTIVATE_FILE) )`
## Once that's done, we'll have access to shellvars
##
## Near the bottom of your wsgi.py file will be a line that includes something like:
## `import app as application`
##
## Just before that line, add the following lines.
## Notes:
## - The settings path is set in the activate_this.py file, as shown above. It could be explicitly
## set here, but this keeps hardcoded settings out of version control.
## - The line `import shellvars` works because the wsgi file has already run execfile()
## - Complete wsgi.py example: `https://github.com/birkin/django_template_project/blob/master/config/passenger_wsgi.py`
## load up env vars
SETTINGS_FILE = os.environ['project_x__SETTINGS_PATH']
import shellvars
var_dct = shellvars.get_vars( SETTINGS_FILE )
for ( key, val ) in var_dct.items():
os.environ[key] = val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment