Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Astro Coke

[Conda] Add Virtual Environment to Jupyter Notebook 본문

Computer Setup

[Conda] Add Virtual Environment to Jupyter Notebook

astrodoo 2020. 6. 12. 06:05

source: https://janakiev.com/blog/jupyter-virtual-envs/#:~:text=A%20virtual%20environment%20is%20an,or%20packages%20for%20different%20projects.

 

Create Virtual Environment with Anaconda

Let’s have a look how to create an virtual environment with Anaconda. Anaconda is a Python (and R) distribution that has the goal to simplify package management and deployment for scientific computing. After the installation you can create the conda virtual environment with:

> conda create -n myenv

where myenv is the name of your new environment. If you want a specific Python version that is not your current version, you can type:

> conda create -n myenv python=3.7

The environment is then stored in the envs folder in your Anaconda directory. After you have created the enviroment, you can activate it by typing:

> source activate myenv

To check the environment, which is currently set:

> conda info -e

 

If you now run python, you’ll see that you are in your freshly created virtual environment. To deactivate the environment you can type conda deactivate and you can list all the available environments on your machine with conda env list. To remove an enviroment you can type:

> conda env remove -n myenv

After creating your environment, you can install the packages you need besides the one already installed by conda. You can find more information on how to manage conda environments in this user guide.

Add Virtual Environment to Jupyter Notebook

Jupyter Notebook makes sure that the IPython kernel is available, but you have to manually add a kernel with a different version of Python or a virtual environment. First, you need to activate your virtual environment. Next, install ipykernel which provides the IPython kernel for Jupyter:

> pip install --user ipykernel

Next you can add your virtual environment to Jupyter by typing:

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

> python -m ipykernel install --user --name=myenv

This should print the following:

Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv

In this folder you will find a kernel.json file which should look the following way if you did everything correctly:

{ 
 "argv": [ 
 "/home/user/anaconda3/envs/myenv/bin/python", 
 "-m", 
 "ipykernel_launcher", 
 "-f", "{connection_file}" ], 
 "display_name": "myenv", 
 "language": "python" 
}

 

Remove Virtual Environment from Jupyter Notebook

After you deleted your virtual environment, you’ll want to remove it also from Jupyter. Let’s first see which kernels are available. You can list them with:

> jupyter kernelspec list

This should return something like:

Available kernels: 
  myenv /home/user/.local/share/jupyter/kernels/myenv 
  python3 /usr/local/share/jupyter/kernels/python3

Now, to uninstall the kernel, you can type:

jupyter kernelspec uninstall myenv

> jupyter kernelspec uninstall myenv