How to import and export Conda Environments to a New Machine (Windows/Mac) Without Losing Your Sanity

Marcia CrippsMarcia Cripps
4 min read

I got two NEW Laptops!

So I recently switched to not one but two new machines — exciting, right? That is, until I realized just how many environment variables, Conda environments, and subtle machine-specific settings I had to migrate. What seemed like a simple conda env create -f environment.yml turned into a mini saga of missing passwords, weird errors, and a "CorruptedEnvironmentError" that made me regret getting two new laptops.

Here’s the full breakdown of what worked, what didn’t, and how to migrate your Conda setup the right way, based on lessons I learned the hard way. 🙃 For reference I have an M4 Macbook Air, and Lenovo Thinkpad T14 Gen 5 for the windows machine. Other then the stack overflow post I mention I also used the conda documentation to get myself out of this mess: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

Step 1: Export Your Conda Environment from the Old Machine

This part was smooth:

conda activate my-env

conda env export > environment.yml

Transfer that environment.yml to the new machine.

Step 2: Export Your Environment Variables

This is where it got tricky. I wanted to bring over all my secrets, API keys, and DB passwords — but they weren’t part of the environment.yml.

I used this Stack Overflow gem to give me an idea on how to export all current environmental variables but careful using what’s here, I’ll explain below.

https://stackoverflow.com/questions/559816/how-to-export-and-import-environment-variables-in-windows

You have two options here:

Option A — Dump everything (messy but complete)

This grabs every environment variable from your current session:

SET > all_env_vars.txt

If you open it up and see a bunch of crap or weird OS/system-level variables like USERNAME, TEMP, PATH, etc. you did it right. It’s messy, but for one of my environments, I had so many secrets that I couldn't remember how I named half of them — this full dump was a lifesaver. I just opened it in VS Code and manually deleted anything system-related. I personally opened up notepad and just deleted everything that wasn’t my environmental variables on the conda environments where I just put too many in it.

Option B — Filtered export (cleaner, easier for smaller envs)

If your environment only has a few key variables and you follow a naming convention, you can do:

On a windows machine:

SET | findstr /I "password secret key token api db auth" > env_vars.txt

This filters out system variables and only includes ones that likely matter. Pretty much this will look for anything like password, secret, key, token, api, db, auth you can add

On macOS, the equivalent command would be:

env | grep -i 'password\|key\|secret\|token\|api\|db\|auth' > env_vars.txt

Pick the option that matches your machine.

Step 3: Import the Conda Environment on the New Machine

Straightforward:

conda env create -f environment.yml

conda activate my-env

Step 4: Import the Environment Variables (The Right Way)

What worked:

🪟 If you're on Windows open up cmd:

for /F "tokens=1,* delims==" %A in (env_vars.txt) do conda env config vars set %A=%B

🍏 If you're on macOS open up terminal:

while IFS== read -r key value; do

conda env config vars set "$key"="$value"

done < env_vars.txt

Then reload the environment:

conda deactivate

conda activate my-env

Success! 🎉 No errors, and all my secrets loaded properly. You can verify with:

conda env config vars list

WARNING At first, I tried using the version from the Stack Overflow post:

for /F %A in (env_vars.txt) do SET %A

But this doesn’t separate the key and value, and when used with Conda, it can cause issues. I got this error:

Environment variable B not defined

And when I ran conda env config vars list, I saw:

%A = %B

So I had to change it.

Final Thoughts

Moral of the story? Exporting Conda envs is only half the battle — environment variables can be sneaky, and not all of them should be moved. But when you do want to bring them over, be precise, clean them up (manually if needed), and use the right method to persist them in Conda.

Huge thanks to this Stack Overflow post for pointing me in the right direction: 👉 https://stackoverflow.com/questions/559816/how-to-export-and-import-environment-variables-in-windows

Hopefully, this post saves you a few hours of hair-pulling. 😅

Got questions or ran into your own Conda nightmare? Drop it in the comments — I’ve been there.

Looking for more farm, horse, passive income, and tech insights from a farmer-turned-software engineer?

You’ll find me most active on Twitter and Medium, but if you’d like to connect on other social media platforms or discover more about my diverse interests, feel free to explore all of my links here!

https://stan.store/marciacripps

For even more valuable content, subscribe to my newsletter, where I share the latest news, exclusive deals, and weekly tips on passive income, farm life, horse care, tech innovations, coding, and insights on agtech. My content is designed to benefit farmers, software engineers, and small business owners alike, so join the conversation and let’s grow together!

*disclaimer I do make a commission off Amazon purchases and Stan store.

0
Subscribe to my newsletter

Read articles from Marcia Cripps directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Marcia Cripps
Marcia Cripps

I'm a farmer turned software engineer living in the middle of nowhere in Michigan.