How to create a conda environment that imports all Python libraries in Jupiter notebooks

lizawt

Elizabeth Thuku

Posted on June 24, 2022

How to create a conda environment that imports all Python libraries in Jupiter notebooks

When I first started working with conda environments in windows 10, it took me a very long time to figure out how to import basic python libraries in Jupiter notebooks without having to install them one by one any time I needed it.

Even tried cloning the base environment to a .yaml file but that didn't work either.

so what finally worked?

Creating conda environments from a text file

The first thing to note with this method is we need to start with an existing working environment. its some sort of cloning but not really.

for me I'll be "cloning" the base environment in conda.

  • In your anaconda or command prompt activate the environment you
    need to duplicate: conda activate base

  • Run conda list --explicit to see a list of specs in the
    active environment like this:

    packages list

  • Save the list in a text file by running

conda list --explicit > pack-file.txt
Enter fullscreen mode Exit fullscreen mode

A thing to note with explicit files is that they are not cross platform. The # platform: win-64 shows where the file was created and can only be used within the same platform since we'll be installing the packages from the text file and dependency issues may arise.

  • Create a new empty environment mtest. Replace mtest with name of your choice
conda create --name mtest
Enter fullscreen mode Exit fullscreen mode
  • Locate the pack-file.txt from your directory and remove any packages you don't want installed in the new environment. Make sure ipykernel is in the list if you want Jupiter notebooks to work correctly in the environment

Remember if you are duplicating the base environment you need to locate the following packages and remove them from the file:

Remove this

  • Deactivate the active environments and make sure there is no environment enclosed in () before your folder path:

env deactivation

  • Run this to install the packages in the new environment
conda install --name mtest --file pack-file.txt
Enter fullscreen mode Exit fullscreen mode

Conda does not check architecture or dependencies when installing from a txt file. To ensure that the packages work correctly, make sure that the file was created from a working environment, and use it on the same architecture, operating system, and platform, such as linux-64,osx-64 or win-64.

  • The final step is to link your environment to jupyter using kernels. Activate your environment conda activate mtest

Run:

python -m ipykernel install --user --name=mtest
Enter fullscreen mode Exit fullscreen mode

Link jupyter

Now the kernel can be found in your Jupyter notebooks.

Available kernel

There you go! I hope this was helpful.

💖 💪 🙅 🚩
lizawt
Elizabeth Thuku

Posted on June 24, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024