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 Artificial Intelligence from Data School πŸ€–

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School πŸ€–

Hi Reader, Thanks for sticking with me through last week’s course launch! πŸ™ As you may have noticed, Tuesday Tips has been on pause for a few months. I was focused on launching the course, plus I’ve been working on a book 🀫 I can’t promise a new tip every Tuesday, but I’ll do my best to provide you with valuable content as time permits. As for today’s tip, I’m trying out something new… πŸ‘‡ Sponsored by: Superhuman AI Find out why 1M+ professionals read Superhuman AI daily. AI won't take over...

Hi Reader, This week, I've got a short tip about AI agents, followed by some Data School news... πŸ‘‰ Tip #56: What are AI agents? Google is calling 2025 "the agentic era," DeepLearning.AI says "the agentic era is upon us," and NVIDIA's founder says "one of the most important things happening in the world of enterprise is agentic AI." Clearly AI agents are a big deal, but what exactly are they? Simply put, an AI agent is an application that uses a Large Language Model (LLM) to control its...

Hi Reader, Last week, I launched a brand new course: Build an AI chatbot with Python. 120+ people enrolled, and a few have already completed the course! πŸ‘ Want to join us for $9? πŸ‘‰ Tip #55: Should you still learn to code in 2025? You’ve probably heard that Large Language Models (LLMs) are excellent at writing code: They are competitive with the best human coders. They can create a full web application from a single prompt. LLM-powered tools like Cursor and Copilot can autocomplete or even...