Search code examples
pythonenvironment-variablestravis-cinosetests

How to set environment variables in travis-ci and access them from python script?


Our travis.yml looks like this:

language: python
python:
  - "2.7"
env: 
  - "MONGO_URL=mongodb://localhost/"
services: mongodb
# command to install dependencies
install: "pip install -r requirements.txt"
# command to run tests
script: nosetests

Then in the python script with the tests, the line

server.connect(os.environ['MONGO_URL'])

throws an error (shortened):

File "/home/travis/virtualenv/python2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)

This error only occurs on travis. If we run them locally, it works without a problem. So we assume we set the environment variable MONGO_URL in a wrong way. We already tried ommitting the quotation marks, but it did not help.

Any hints? We use the free cloud service of travis-ci.


Solution

  • It works perfectly fine now, the error was that I accidently created another travis.yml missing the . in front of it - so the actually executed .travis.yml did not contain the environment variables.