Tuesday Tip #35: Get started with conda environments 🤝

Hi Reader,

Happy (belated) new year! 🥳

I just updated my email settings in order to comply with the latest anti-spam rules. Would you reply to this email so that I know you received it?

You don’t even need to write anything… just click Reply, then Send. Thank you! 🙏

🔗 Link of the week

Can One Episode Ruin a TV Show?

This is an excellent example of a thoughtful and engaging statistical analysis. It has a strong narrative and relevant visualizations. See if you can incorporate some of those elements into your next analysis!

👉 Tip #35: Get started with conda environments

In the previous tip, I explained the differences between conda, Anaconda, and Miniconda.

I said that you can use conda to manage virtual environments:

If you’re not familiar with virtual environments, they allow you to maintain isolated environments with different packages and versions of those packages.

In today’s tip, I’m going to explain the benefits of virtual environments and the how to use virtual environments in conda.

Let’s go! 👇

Why use virtual environments?

A virtual environment is like a “workspace” where you can install a set of packages with specific versions. Each environment is isolated from all other environments, and also isolated from the base environment. (The base environment is created when you install conda.)

So, why use virtual environments at all?

  • Different packages can have conflicting requirements for their dependencies, meaning installing one may cause the other to stop working.
  • If you put them in separate environments instead, then you can switch between the environments as needed, and both will continue to work.

Thus by using environments, you won’t breaking existing projects when you install, update, or remove packages, since each project can have its own environment.

You can also delete environments once you’re done with them, and if you run into problems with an environment, it’s easy to start a new one!

Six commands you need to know

conda environments have a lot of complexity, but there are actually only six commands you need to learn in order to get most of the benefits:

1️⃣ conda create -n myenv jupyter pandas matplotlib scikit-learn

This tells conda to:

  • Create an environment named myenv (or whatever name you choose)
  • Install jupyter, pandas, matplotlib, and scikit-learn in myenv (or whatever packages you need installed)
  • Install all of their dependencies in myenv
  • Choose the latest version of every one of those packages (as long as it works with every other package being installed)

That last point is a mouthful, but it basically means that conda will try to avoid any conflicts between package dependencies.

Note: conda stores all of your environments in one location on your computer, so it doesn’t matter what directory you are in when you create an environment.

2️⃣ conda activate myenv

This activates the myenv environment, such that you are now working in the myenv workspace.

In other words:

  • If you now type python or jupyter lab (for example), you’ll be running the Python or JupyterLab that is installed in myenv.
  • If you then type import pandas, you’ll be importing the pandas that’s installed in myenv.

Note: Activating an environment does not change your working directory.

3️⃣ conda list

This lists all of the packages that are installed in the active environment (along with their version numbers). If you followed my commands above, you’ll see python, jupyter, pandas, matplotlib, scikit-learn, and all of their dependencies.

4️⃣ conda env list

This lists all of the conda environments on your system, with an asterisk (*) next to the active environment.

5️⃣ conda deactivate

This exits the active environment, which will usually take you back to the “base” environment (which was created by Anaconda or Miniconda during installation).

6️⃣ conda env remove -n myenv

This permanently deletes the myenv environment. You can’t delete the active environment, so you have to deactivate myenv (or activate a different environment) first.

Going further

If you want to learn more about conda environments, check out this section of the user guide:

🔗 Managing environments

If you want a broader view of conda and its capabilities, check out this section:

🔗 Common tasks

Or, just shoot me an email and I’m happy to help! 💌

✌️ Peace out

Did you like this week’s tip? Please forward it to a friend, or share this link on social. It really helps me out! 🙌

See you next Tuesday!

- Kevin

P.S. How to Pass the Pepper While Social Distancing (YouTube)

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

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, 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...

Hi Reader, happy Tuesday! My recent tips have been rather lengthy, so I'm going to mix it up with some shorter tips (like today's). Let me know what you think! 💬 🔗 Link of the week A stealth attack came close to compromising the world's computers (The Economist) If you haven't heard about the recent "xz Utils backdoor", it's an absolutely fascinating/terrifying story! In short, a hacker (or team of hackers) spent years gaining the trust of an open-source project by making helpful...