# Use conda to create and manage python virtual environments ## Table of Contents - [Download miniconda](#Download-miniconda) - [Windows 10](#Windows-10) - [Ubuntu](#Ubuntu) - [Install miniconda](#Install-miniconda) - [Windows 10](#Windows-10) - [Ubuntu](#Ubuntu) - [Command](#Command) - [Check the current version](#Check-the-current-version) - [Create a virtual environment](#Create-a-virtual-environment) - [Enable and enter virtual environment](#Enable-and-enter-virtual-environment) - [Leave the virtual environment](#Leave-the-virtual-environment) - [Install package in virtual environment](#Install-package-in-virtual-environment) - [Delete virtual environment or package](#Delete-virtual-environment-or-package) ## Download miniconda #### Windows 10 Go to the[Miniconda website](https://docs.conda.io/en/latest/miniconda.html) and download the Windows installer for the latest version of Miniconda (64-bit or 32-bit, depending on your system architecture). #### Ubuntu Just chick the [Google Drive](https://drive.google.com/file/d/1U0YTPq6tyowcIlOzeNBx6MGJ6kHtPMkP/view?usp=sharing) to Download install shell script. ## Install miniconda #### Windows 10 By default, click Next ![image](https://hackmd.io/_uploads/BkDzD9Hup.png) Press I Agree to agree to Licence ![image](https://hackmd.io/_uploads/HkaFD9rua.png) By default, click Next ![image](https://hackmd.io/_uploads/SkLBd9SOa.png) By default, click Next ![image](https://hackmd.io/_uploads/B1MwtqBua.png) Select Add Miniconda3 to my PATH environment variable ![image](https://hackmd.io/_uploads/SkgGEo9SOp.png) Wait for the installation progress bar to complete and press Next ![image](https://hackmd.io/_uploads/rygnFcH_T.png) Don't select anything and skip the default tutorial. Press Finish to complete the installation. ![image](https://hackmd.io/_uploads/rk4bc9Hda.png) #### Ubuntu Enable shell script permission ```sh sudo chmod 777 Install_miniconda.sh ``` Execute shell script to install ```sh ./Install_miniconda.sh ``` ## Command #### 1. Check the current version ```sh conda -V ``` If you want to upgrade ```sh conda update conda ``` #### 2. Create a virtual environment - Show virtual environments installed ```sh conda env list ``` - Suppose we want to create a virtual environment called myenv and install python 3.5 version, then we can type the following command ```sh conda create --name myenv python=3.5 -y ``` #### 3. Enable and enter virtual environment **Windows 10** ```sh activate myenv ``` **Ubuntu** ```sh conda activate myenv ``` #### 4. Leave the virtual environment ```sh conda deactivate ``` #### 5. Install package in virtual environment - If you want to install the required packages, such as numpy, in this virtual environment ```sh pip install numpy ``` - Maybe for requirements.txt ```sh pip install -r requirements.txt ``` #### 6. Leave the virtual environment **Windows 10** ```sh deactivate ``` **Ubuntu** ```sh conda deactivate ``` #### 7. Delete virtual environment or package - Delete a package in the virtual environment (such as numpy in the newly created virtual environment myenv) ```sh conda remove --name myenv numpy -y ``` - Delete the entire virtual environment ```sh conda env remove --name myenv -y ``` #### 7. Backup environment - Export all dependencies (including name, version, and dependencies) from the current Conda environment to a file named `environment.yml`. This file can be used to create the same environment in another system ```sh conda env export > env.yaml ``` - If you move this file to another machine and run the command, conda will create a new environment based on the information in the file, including all required packages and their dependencies. ```sh conda env create -f env.yaml ```