Skip to main content

How to Add a Custom Anaconda Environment to Jupyter Notebook

This article explains how to add a custom Anaconda environment to Jupyter Notebook, so you can select that environment's kernel in the browser to run Python code.

Create a New Anaconda Environment

If you already have the Anaconda environment you want to add to Jupyter Notebook, you can skip this step.

After installing Anaconda software, enter the following command in the command line to create a new environment:

conda create -n pytorch_evn python=3.6

The pytorch_evn above is the name of the new environment (can be any name), and python=3.6 specifies the Python version for that environment.

After creating the environment, activate it with the following command:

conda activate pytorch_evn

Other commonly used commands include:

conda deactivate  # Deactivate the current environment
conda env list # List all created environments
conda env remove -n pytorch_evn # Remove a specified environment

For detailed information, please refer to the Anaconda official website.

Add the Target Environment to Jupyter Notebook

To use Jupyter Notebook in a specific Anaconda environment, you first need to ensure that the environment's IPython kernel is available. The specific steps are as follows:

  1. Activate the environment:

    conda activate pytorch_evn
  2. Install ipykernel:

    pip install --user ipykernel
  3. Add the environment to Jupyter:

    python -m ipykernel install --user --name=pytorch_evn

    After success, you will see output similar to the following:

    Installed kernelspec pytorch_evn in /home/user/.local/share/jupyter/kernels/pytorch_evn
  4. Verify:

    Enter jupyter lab in the command line, and you should see the following screen in the automatically opened browser (the custom environment is in the red box).

Remove the Target Environment from Jupyter Notebook

When you delete an environment from your computer, you may also need to remove it from Jupyter Notebook. The specific steps are as follows:

  1. View available environments in Jupyter:

    jupyter kernelspec list
  2. Delete a specified environment:

    jupyter kernelspec uninstall pytorch_evn

Additional Information

If you need to remotely access a Jupyter Notebook server on another computer within a local network, you can refer to this blog post: Two Methods for Remotely Accessing Server Jupyter Notebook.

If you need to access the Jupyter Notebook server on a server through NAT traversal, you can use software like frp or Peanut Shell. For specific operations, refer to Configuring and Installing jupyter-notebook on Ubuntu and Windows with Auto-start, Background Running, and Remote Access and Using frp for NAT Traversal.

To set up the Jupyter Notebook server to start automatically on boot, refer to Configuring and Installing jupyter-notebook on Ubuntu and Windows with Auto-start, Background Running, and Remote Access.


Copyright Notice: This article is an original blog post, following the CC 4.0 BY-SA copyright protocol. Please include the original link and this statement when reposting.

Original link: https://blog.csdn.net/qq_37430422/article/details/107844940