1

I am working on a Slack Bot and mostly followed this tutorial, except for some things that were different on Windows. One of these was setting the BOT_ID and SLACK_BOT_TOKEN environment variables. I used set BOT_ID=xcdsfsdf in a command prompt that was in a virtualenv session. Now that I've closed everything and returned to the project, BOT_ID and SLACK_BOT_TOKEN were no longer variables.

How can I set these variables so that I do not have to re-create them each time?

I could simply add these as system variables, but what if I was creating multiple slack bots? What is the standard practice? SLACK_BOT_TOKEN_1, SLACK_BOT_TOKEN_2, etc?

Additionally, when I go to reopen the project to run the python code, do I need to activate the virtualenv each time? Or can I just execute the python code?

Matt
  • 1,792
  • 5
  • 21
  • 33
  • In your virtual environment, usully in `bin` there should be a couple of script like `post_activate` where you can put that. – Klaus D. May 12 '17 at 03:01
  • I'm running virtualenv on windows. There is no `bin`, just a Scripts folder where the activate.bat file was. I don't see any post-activate script. There is a deactivate.bat file. – Matt May 12 '17 at 03:06
  • The put it at the end of `activate.bat`. – Klaus D. May 12 '17 at 03:09
  • What is the purpose of doing that as opposed to just putting the API token and ID in the python code? I thought this was to avoid having the token in plain text in a file for security? – Matt May 12 '17 at 03:17
  • You have two options: store it or enter it every time. Your choice. – Klaus D. May 12 '17 at 03:19

2 Answers2

1

Create a .env file in the root directory of your virtualenv and store your variables. I use it for django in this manner:

EMAIL_HOST_PASSWORD='some_password'
DJANGO_DEBUG=True
SECRET_KEY='a_complex_key'
diek
  • 657
  • 7
  • 16
  • Are .env files for Linux? I tried creating one, but it didn't work. I forced the extension to be .env and it wasn't recognized. I can just put the environment variables directly in the batch file. – Matt May 12 '17 at 14:33
  • Apparently this doesn't work. The batch file ran the activate.bat for virtualenv and didn't set the variables. – Matt May 12 '17 at 15:18
  • I use it on linux and mac, but I read that it can be used on Windows too. That is why I posted. – diek May 12 '17 at 15:25
  • I suggest trying this package top help, https://github.com/theskumar/python-dotenv – diek May 12 '17 at 15:40
1

The solution described in this other answer worked for me. Add the export lines to your bin/activate file.