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, Last week, I encouraged you to experiment with different LLMs, since thereโ€™s no one model that is superior across all use cases. Specifically, I suggested you try using Chatbot Arena, which allows you to chat with multiple models at once. Itโ€™s completely free, but has two significant disadvantages: Your chats are not private and may be used for research. It lacks the feature-rich interface provided by other LLMs. Today, I want to offer you a better method for experimenting with...

Hi Reader, Over the past 50 tips, Iโ€™ve touched on many different topics: Python, Jupyter, pandas, ML, data visualization, and so on. Going forward, Iโ€™m planning to focus mostly on Artificial Intelligence. Iโ€™m announcing this so you know what to expect, and I know what to deliver! ๐Ÿ’Œ Iโ€™ll also try to make the tips shorter, so that they're easier to digest on-the-go. Finally, I plan to include an โ€œaction itemโ€ each week, so that you can practice what youโ€™re learning. I hope you like these...

Hi Reader, Next week, Iโ€™ll be offering a Black Friday sale on ALL of my courses. Iโ€™ll send you the details this Thursday! ๐Ÿšจ ๐Ÿ‘‰ Tip #50: What is a "method" in Python? In Python, a method is a function that can be used on an object because of the object's type. For example, if you create a Python list, the "append" method can be used on that list. All lists have an "append" method simply because they are lists: If you create a Python string, the "upper" method can be used on that string simply...