6👍
You can use envdir python port (here is the original) for managing the environment variables.
If you are only concerned about Django, I suggest using envdir from your settings.py
programmatically
You can update the environment programmatically (e.g.: in the wsgi file, django’s manage.py
, settings.py
, etc.)
import envdir
import os
# print os.environ['FOO'] # would raise a KeyError
path = '../envdir/prod'
if not os.path.isdir(path):
raise ValueError('%s is not a dir' % path)
envdir.Env(path)
print os.environ['FOO']
or you can run the your process through envdir
on the command line, e.g.: envdir envs/prod/ python manage.py runserver
I suggest creating aliases for python, pip, etc. (as you don’t want to overwrite the system’s own python), e.g.: alias python-mycorp="envdir /abs/path/to/envs/prod/ python"
(or if you prefer, write a full shell script instead of an alias).
2👍
This mapping is captured the first time the os module is imported,
typically during Python startup as part of processing site.py. Changes
to the environment made after this time are not reflected in
os.environ, except for changes made by modifying os.environ directly.
They all have to use the same interpreter. If they launch by the same user, they probably are.
- How to serialize custom user model in DRF
- Django-REST framework restore_object attrs parameters
- Postgresql operator class "varchar_pattern_ops" does not accept data type integer
2👍
As you can see in the documentation of sys.path, it is initialized with the environment variable PYTHONPATH
and then with an installation dependent default (site). So, they are intended to be different.
But, you can use the -S
option during the interpreter invocation: python -S script.py
in order to skip some site specific configuration hook. Nevertheless, you will still have the standard library stuff in your sys.path
.
If you really really want os.path['PYTHONPATH'] == sys.path
, you should do it explicitly, as the documentation says:
A program is free to modify this list for its own purposes
The standard places to put those kind of specific manipulations are:
- A
sitecustomize
module, typically created by a system administrator in the site-packages directory, which can do arbitrary configurations. - A
usercustomize
module, which intention is the same assitecustomize
but only executed ifENABLE_USER_SITE
is true. - Customization to the
sys.path
directly from the script. I.e:sys.path = os.env['PYTHONPATH']
.
- How can I modify a widget's attributes in a ModelForm's __init__() method?
- Serializer validate function is not called DRF
-2👍
I’m going to assume you meant os.environ[‘PYTHONPATH’] == sys.path , because otherwise I can’t understand the question.
Anyway, the solution would be to use virtualenvs.
- Setup a virtualenv
- Edit the /bin/activate and add entry PYTHONPATH=your-sys-path.
- Make sure your mod_wsgi, celery, cron jobs and shell login(bash_login?) all activate the virtualenv when they are started and use the virtualenv/bin/python for execution.
Done.
- Translating formatted strings in Django not working
- Django + Psycopg2: InterfaceError: only protocol 3 supported