Tuesday Tip #48: Three ways to set your environment variables ✅


Hi Reader,

Regardless of whether you enrolled, thanks for sticking with me through the launch of my new course! 🚀

I've already started exploring topics for the next course... 😄


🔗 Link of the week

git cheat sheet (PDF)

A well-organized and highly readable cheat sheet from Julia Evans, the brilliant mind behind Wizard Zines!


👉 Tip #48: Three ways to set your environment variables in Python

I was playing around with Mistral LLM this weekend (via LangChain in Python), and I needed to set an environment variable in order to provide my API key to Mistral.

There are many common reasons you might need to set an environment variable, but they all center around providing configuration details to an application.

Below, I'll show you three ways to set an environment variable in Python:


Method 1: os.environ

The simplest method is to use Python's built-in os module. os.environ acts like a dictionary, and so this code sets an environment variable with the key MISTRAL_API_KEY and the value NotMyRealKey.

However, this method isn't secure since your API key is now stored within the code.


Method 2: os.environ & getpass

This method uses Python's built-in getpass module. When you run this code, a text field appears so that you can paste in your API key.

The API key will never be shown on screen, which makes it more secure than method #1, but it's less convenient since you need to paste in your key every time you run the code.


Method 3: dotenv

This method uses the python-dotenv library, which you can install using pip or conda. All you have to do is create a text file named .env, and then include the key and value as shown here:

The load_dotenv function reads the key-value pairs from the file and sets them as environment variables.

Assuming you exclude the .env file from your code repository (probably using .gitignore), this is a reasonable method for storing your API key secretly.


👋 See you next week!

If you liked this week's tip, please share it with a friend! It really helps me out.

- Kevin

P.S. Your Life in Weeks

Did someone AWESOME forward you this email? Sign up here to receive more Data Science tips!

Learn Data Science from Data School 📊

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School 📊

Hi Reader, Last week, I announced that a new course is coming soon and invited you to guess the topic. Hundreds of guesses were submitted, and four people who guessed correctly got the course for free! (I've already notified the winners.) I'll tell you about the course next week. In the meantime, I've got a new Tuesday Tip for you! 👇 🔗 Link of the week OpenAI just unleashed an alien of extraordinary ability (Understanding AI) If you're curious about what makes OpenAI's new "o1" models so...

Hi Reader, I'm really proud of this week's tip because it covers a topic (data leakage) that took me years to fully understand. 🧠 It's one of those times when I feel like I'm truly contributing to the collective wisdom by distilling complex ideas into an approachable format. 💡 You can read the tip below 👇 or on my blog. 🔗 Link of the week Building an AI Coach to Help Tame My Monkey Mind (Eugene Yan) In this short post, Eugene describes his experiences calling an LLM on the phone for coaching:...

Hi Reader, Last week, I recorded the FINAL 28 LESSONS 🎉 for my upcoming course, Master Machine Learning with scikit-learn. That's why you didn't hear from me last week! 😅 I edited one of those 28 videos and posted it on YouTube. That video is today's tip, which I'll tell you about below! 👉 Tip #45: How to read the scikit-learn documentation In order to become truly proficient with scikit-learn, you need to be able to read the documentation. In this video lesson, I’ll walk you through the five...