Register customizable environment variables with anaconda

Viewed 606

By running conda env export > environment.yml I can make it easy for people to clone and replicate my environment.

But I also need them to set some environment variables. When using PHP (Laravel), I had a .env file (ignored by git) where the user could put account details, passwords, tokens etc. A file .env.example was provided allowing the user to see the required values. So I implemented that with a python class but it was frowned upon in r/learnpython ("...to give your user rope to hang themselves with").

After further reading I did a file activate in my project root

export \
    GITHUB_ACCESS_TOKEN="your value goes here", 
    BENNO="test",

So the user now just runs source activate to register the variables. But I see several problems

  • activate is committed, how to protect the user from accidently publishing this?
  • After exiting my conda environment, the variable GITHUB_ACCESS_TOKEN was still active. I expected the conda environment to keep a separate set of environment variables?
  • The user have to run the activate script every time they relaunch the terminal
  • The activation script does not support windows usage
  • The principle still is the same as the .env.example in PHP which is bad??

To summarize I would like a clean simple way to store both the dependencies AND customizable environment vars, allowing for simple installation for conda users, but also if possible a wider set of python users. What are some good practices here? Can I somehow list the vars in environment.yml?

2 Answers

I prefer your original solution. Just as long as there are enough warnings in the README file and in the comments, ignore the "rope" arguments. It is impossible to create a fool-proof system. If you want a check, add an Environment variable that prevents your default environment from running as is. That will ensure the environment has been modified for use, or at least the user has had to look at the file.

Forcing the user to rename the example file and check it, as described above, resolves the issues presented here.

Like @Strom says my original solution is not that bad. Did end up refining it as detailed below.


First lets install python-dotenv

conda install -c conda-forge python-dotenv -n my-environment-name

Then make a file env.example in your root with the values your user need to provide

GITHUB_ACCESS_TOKEN=PUT_YOUR_TOKEN_HERE

Edit .gitignore (I choose to ignore any .env recursively)

**/.env

In your script entry point, bootstrap file or where you seem fit do

from dotenv import load_dotenv
load_dotenv()

Finally, now you should be able to reach the variables from anywhere in your app by doing

import os
print(os.getenv("GITHUB_ACCESS_TOKEN"))


Note on this! (Thanks Alex & Henrik)

It's strongly advised against because you generally don't want to risk some dingus overriding the gitignore and committing a file full of secrets to your repo. Typically you'll want to let your deployment solution or a service like HashiCorp Vault handle injecting your env vars rather than store a file on your environments' filesystems.

So don't mess around with your env files in production.


Reference: https://github.com/theskumar/python-dotenv

Another possible solution that I didnt try yet: https://github.com/direnv/direnv

Related