通过Anaconda管理环境

转自:https://evidencen.com/how-to-create-export-and-import-anaconda-environments/


How to create anaconda environment and make it available in Jupyter lab.

There are 2 ways to create anaconda environments. You can create anaconda environment from anaconda prompt command line or from anaconda navigator.

How to create anaconda environment from anaconda navigator

Step 1: Open anaconda navigator

Step 2: click on “environment” located on the left side of the screen

Step 3: Click on “create” located on the bottom of the screen.

Step 4: Type in the name of the new environment–>choose python and/or R installation–>and click create.

Note: Make sure the name you give the new environment has never been used before.

Step 5: Add packages to the new environment by clicking the environment name

  • Search for the package you want in the search bar–>click on “ALL” from the drop down menu.
    在这里插入图片描述

  • Select the name of the package you want to install, then click “apply” located in the lower right side of the screen.

  • Clicking apply installs the selected package.
    在这里插入图片描述


How to create anaconda environment from (anaconda prompt in windows, terminal on mac) and make it available in Jupyter lab.

If you are looking to create an anaconda environment for a specific project using your command line, then follow these instructions.

Step 1: Navigate to the folder that contains the project you are working on using anaconda prompt on windows or terminal on mac

Step 2: Run conda create -n Environment-name python==3.7

Once the command completes, your conda environment should be ready

Next step is to add the required python packages. You will need to ‘activate’ the conda environment to add packages.

Step 3: To activate anaconda environment, type in source activate Environment-name on Terminal or conda activate Environment-name on Anaconda Prompt

How to install packages in anaconda environment (2 ways)

After activating your conda environment, you may want to install the packages for the project from the terminal. You can also install the packages for the project from anaconda navigator using STEP 5 FROM ABOVE.

OR…you can also use the instructions below to install package in your anaconda environment.

To install the required packages from the terminal, create a document called “requirements.txt” that has all the packages you will need to install. Here is an example of what it looks like.

This document should be on the folder/directory that has the project you are working on.
在这里插入图片描述
Step 4: Once your environment is activate, run pip install -r requirements.txt which will install the required packages into your environment.

You could alternatively install the package individually by doing pip install package name. But be warned, it is better to put the packages in a requirements.txt file and install it from there.

Adding Virtual Environment to Jupyter Lab

To make the environment available in jupyterlab, we need to add an Ipython Kernel reference to the conda environment, so we can use it from JupyterLab.

To add conda environment to jupyter lab,

Step 5: run python -m ipykernel install --user --name Environment-name --display-name "what you want jupyterlab environment called"

This will add a json object to an ipython file, so JupterLab will know that it can use this isolated instance of Python environment.

Step 6: Deactivate your conda environment and launch JupyterLab. You should now see “what you want jupyterlab environment called” in the list of available kernels on jupyter lab launch screen.

Step 7: Run conda deactivate to deactivate your environment



How to Export anaconda environments.

First, go to your terminal and follow the steps below. I have not found how to export anaconda environment using anaconda navigator, so the terminal is our only option.

Step 1: Activate the environment to export by typing: conda activate myenv

On Mac, type source activate myenv

Step 2: Export your active environment to a new file by typing: conda env export > environment.yml

On Mac, do source env export > environment.yml

Go the directory you are currently working on and you will find your environment.yml file.

Step 3: . Deactivate your conda environment after you are done by running conda deactivate.


How to Import anaconda environments.

To import anaconda environments, you can do it from anaconda navigator

Step 1: Open anaconda navigator and click on environments

Step 2: Click on import located on the bottom of the screen.
在这里插入图片描述
Step 3: Give the environment a new name,

  • Click the folder icon and select the environment.yml file you exported in the last section
  • Then click import
  • Wait a few minutes and the environment will be imported along with it’s dependencies.
    在这里插入图片描述
    更多信息参考anaconda官方文档——管理环境
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值