Activate Python virtualenv for whole job

I’m in the process of upgrading to CircleCI 2.0 and am trying to figure out how to activate a Python virtual environment for an entire job. During our build steps, specific versions of Python, pip, virtualenv, etc. are installed. This means that the virtualenv isn’t available at the beginning of the build. Once the initial setup is complete, I would like to activate the virtualenv and have all remaining steps run with a shell that already has the virtualenv activated, instead of having to create a lot of multiline commands that all start with virtualenv activation.

Is there any way to do this? I think ideally I would have a specific job in the workflow that has the virtualenv permanently activated when the job starts, but I couldn’t find any way to do this in the documentation.

3 Likes

I got a response to this from CircleCI support. There is an alias for .bashrc that can be accessed at $BASH_ENV. This file is loaded in the shell for each step that is run, so you can easily add the virtualenv activation to the file like this:

echo "source /path/to/virtualenv/bin/activate" >> $BASH_ENV
2 Likes