How to set a environment variable with virtualenv?

14,081

if you add to opus-env/bin/activate

OPUS_HOME="some value"
export OPUS_HOME

It should work (your solution 3). Of course it will not be applied to currently running processes.

Try to exit current virtualenv shell session, start new shell session, activate modified virtualenv in that new session. Before starting your program, verify environment:

env | grep OPUS_HOME
Share:
14,081
Touki
Author by

Touki

Updated on June 04, 2022

Comments

  • Touki
    Touki almost 2 years

    I'm trying to set up an external program / lib written in python - on debian squeeze.

    The error I get when I try to launch it that the "environment variable is not set" - it's an exception throw by the developer. So I guess, I have to define it, but where ?

    1. I've thought virtualenv and path were the same, so I've written a .pth loaded at the launch. It solved some mistake but not all.
    2. I've read use export in ~/.bashrc but I'm not sure it is read in a virtualenv, and I don't want those export be read even when I don't use this virtualenv.
    3. I've read too to use export in virtualenv/bin/activate, but it doesn't seem to work correctly.

    I'm trying to install Opus and Urbansim.

    Here is what I get when launching:

    (opus-env)touki@touki:~/Projects/opus-env$ ./src/opus_gui/opus.py  
    Traceback (most recent call last):
      File "./src/opus_gui/opus.py", line 14, in <module>
        from opus_gui.main.controllers.opus_gui_configuration import OpusGuiConfiguration  
      File "/home/touki/Projects/opus-test/src/opus_gui/main/controllers/opus_gui_configuration.py", line 12, in <module>
        from opus_core import paths  
      File "/home/touki/Projects/opus-test/src/opus_core/paths.py", line 44, in <module>
        OPUS_HOME = _safe_getenv('OPUS_HOME', _get_default_opus_home)  
      File "/home/touki/Projects/opus-test/src/opus_core/paths.py", line 33, in _safe_getenv
        return os.environ[key] if key in os.environ else default_func()  
      File "/home/touki/Projects/opus-test/src/opus_core/paths.py", line 36, in _get_default_opus_home  
        raise Exception('OPUS_HOME environment variable must be set.') 
    Exception: OPUS_HOME environment variable must be set.
    

    NB.: I've searched, autoenv combine virtualenvwrapper seems to provide an answer, but I would prefer not use external programs.