Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 873
schreon Avatar asked Jan 10 '14 14:01

schreon


People also ask

How do I set an environment variable in Python terminal?

Environ and set default:With the environ dictionary variable value of the environment variable can be set by passing the key in the dictionary and assigning the value to it. With setdefault a default value can be assigned to the environment variable. Bypassing the key and the default value in the setdefault method.

How can you set environment variables from the command line?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


1 Answers

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.

like image 83
schreon Avatar answered Sep 30 '22 10:09

schreon