pythonpython-venvnox

how to use environment variable in a nox session?


I want to use nox to run my test suit in a controlled environment. To perfectly work I need to pass an environment variable from my normal env to the virtual one is it possible and if yes how ?

@nox.session(name="test", python="3.8")
def test(session):
    
    # intall the venv
    if _should_install(session):
        session.install(".[test]")
        
        # install specific version of earthengine API for the SEPAL env
        if "sepal" in session.posargs:
            session.posargs.remove("sepal")
            session.install("git+https://github.com/custom_lib")

           # add a EE_DECRYPT_KEY env variable e.g. from the posargs
           # but how ?
    
    # run the tests
    session.run("pytest", *session.posargs)

Solution

  • As mentioned by @equatorial_daydreamer in the comments, session.run() accepts env as a keyword argument to pass in environment variables (see docs) which in this case will transform into:

    @nox.session(name="test", python="3.8")
    def test(session):
        
        # intall the venv
        if _should_install(session):
            session.install(".[test]")
            
            # install specific version of earthengine API for the SEPAL env
            if "sepal" in session.posargs:
                session.posargs.remove("sepal")
                session.install("git+https://github.com/custom_lib")
    
        
        # run the tests
        session.run("pytest", *session.posargs, env={"EE_DECRYPT_KEY":"some_value"})