A tutorial on creating a python virtual environment based on Anaconda under Windows

A tutorial on creating a python virtual environment based on Anaconda under Windows

Anaconda is the most popular data science platform and the foundation of modern machine learning . At the same time, Anaconda is also a Python distribution that focuses on artificial intelligence and is naturally suitable for scientific computing, data analysis and machine learning. Its package manager is Conda. Conda is an open source package management system and environment management system that runs on Windows, macOS, and Linux. Conda quickly installs, runs, and updates packages and their dependencies. Conda makes it easy to create, save, load and switch environments on your computer. It was created for Python programs, but it can package and distribute software in any language, in other words, Conda's design philosophy - Conda treats almost all tools, third-party packages as packages, even python and Conda itself Anaconda It is a packaged collection, which is pre-installed with Conda, a certain version of python (several virtual environments), many packages, scientific computing tools , etc.

1. Install Anaconda (if it has been successfully installed, you can ignore this step), click the link below to open the article, and install it according to the fifth major module of the article. Note: Anaconda's python virtual environment can be connected to Pycharm's compilation environment.

Install Anaconda article: Click to open the article "Building Anaconda+Pytorch (Cuda+Cudnn)+Pycharm Tools and Configuration Environment Based on Windows Learning Deep Learning Complete and Minimal Edition" article

2. Open Anaconda Prompt after the installation is complete.

insert image description here
insert image description here

3. Basic commands of Conda

  • Verify that Anaconda is installed and the current installed version of Conda.
conda - V
  • 1

insert image description here

  • Check and update the current conda version (note: generally do not update frequently).
conda update conda
  • 1
  • View the virtual environments currently created by the installed Anaconda (Note: each virtual environment is stored in the envs folder of Anaconda , the base in the picture below represents the original environment of the installed Anaconda, and the rest are virtual environments created by users , if it does not indicate that it has not been created yet, there is no need to worry about creating it later).
conda env list
  • 1

insert image description here

  • Create a virtual environment (note: " conda create -n + the name of the virtual environment to be created ", the blogger gives an example of the virtual environment name to be created is xr, and you need to enter y in the process of creating the virtual environment xr below).
conda create - n xr
  • 1
Y
  • 1

insert image description here

  • Activate the virtual environment (note: " conda activate + the name of the virtual environment to be activated ", the blogger gives an example of the name of the virtual environment to be activated is xr, whether the activation is successful or not can be observed through the following figure. The default environment base in the parentheses before the command is replaced by xr).
conda activate xr
  • 1

insert image description here

  • Exit the current virtual environment (Note: If you enter and run the following command in other virtual environments, it will return to the default environment, and in the default environment, enter and run the following command will return to the control platform CMD ).
conda deactivate
  • 1

insert image description here

  • View the installed python libraries and packages in the environment (virtual environment) (Note: At this time, you are viewing the python libraries and packages installed in the default base environment. There is a base in the parentheses in front of the command line below the picture. This is the understanding. If you want to To view other created virtual environments, you need to activate the virtual environment first, and then enter this command to view, as shown in the second picture below, first activate the virtual environment xr, and then view the installed python libraries and packages under xr ).
conda list
  • 1

insert image description here
insert image description here

  • Environment copy clone backup (Note: After installing a lot of software, the software installation may destroy the current running environment due to some operations, so it is necessary to copy the clone backup environment; " conda create -n + virtual environment to be cloned Name + --clone + name of the virtual environment to be cloned ", the blogger for example, the name of the virtual environment to be cloned is xr, and the name of the virtual environment to be cloned is xrs, in other words, copying xr becomes xrs ).
conda create -n xrs --clone xr
  • 1

insert image description here
insert image description here

  • Delete the virtual environment (note: " conda remove -n + the name of the virtual environment to be deleted + --all ", the blogger exemplifies the name of the virtual environment to be activated is xrs).
conda remove -n xrs --all
  • 1

insert image description here
insert image description here

4. After learning the basic commands of Conda in the previous step, now start to create a python virtual environment (note: " conda create -n + the name of the python virtual environment to be created + python= + python version number ", the blogger gives an example of the The name of the python virtual environment is rothschildlhl, and the python version number is 3.6. The creation time is about 5 minutes, please wait patiently, the creation of the python virtual environment is successfully completed in this step, and then be sure to add the content of the Scripts file of the newly created virtual environment to In the environment variable PATH, the blogger example is "D:\Anaconda\envs\rothschildlhl\Scripts" path).

conda create -n rothschildlhl python=3.6
  • 1
Y
  • 1

insert image description here
insert image description here
insert image description here

Fifth (negligible), then the blogger is importing and configuring the python virtual environment DeepPurpose to create a deep learning model library DeepPurpose, which is equivalent to practice. Just want to create a python virtual environment and it has been successfully completed in the fourth step. Note: If users enable VPN, they need to close VPN before installation to avoid unsuccessful installation.

  • Download the project code base to the E:/DeepPurpose path file.
git clone https://github.com/kexinhuang12345/DeepPurpose.git E:/DeepPurpose
  • 1

insert image description here

  • Change the path to the path where the git project was downloaded to the local in the previous step, that is, the E:/DeepPurpose path.
AND:
  • 1
cd DeepPurpose
  • 1

insert image description here

  • Import the yml file in the E:/DeepPurpose path, and then create a new virtual environment according to the content of the yml file (Note: Before running the following command, you must go to the path of the yml file , and this is a newly created virtual environment, then if it is the same one The computer must delete the virtual environment with the same name or change the name of the original virtual environment).
conda env create -f environment.yml
  • 1

insert image description here
insert image description here

conda activate DeepPurpose
  • 1

insert image description here

  • The previous operation will only create a virtual environment and import the packages installed directly by the conda command, but most of the packages installed by pip in the original virtual environment need to be imported and installed in the new virtual environment through the requirements.txt exported by the original virtual environment pip (note that : The virtual environment has been created in the previous step. If you report some installation errors in this step, you can open the requirements.txt file to delete the packages that were installed with errors. At that time, you need to install the error-reported packages before downloading pip to this virtual environment; in order to ensure smooth operation pip download, enable https://pypi.douban.com/simple, this mirror can be downloaded quickly and smoothly, of course, it can also be installed without a mirror, but it will take a long time to install and an error will be reported).
pip install -r requirements.txt -i https://pypi.douban.com/simple
  • 1

insert image description here

  • A python virtual environment created before is named DeepPurpose (Note: After successfully creating a python virtual environment, be sure to add the contents of the Scripts file of the newly created virtual environment to the environment variable PATH ).

insert image description hereinsert image description here

  • Activate the python virtual environment DeepPurpose.
conda activate DeepPurpose
  • 1

insert image description here

  • Install related python packages and libraries, etc.
conda install - c conda - forge rdkit
  • 1

insert image description here

conda install -c conda-forge notebook
  • 1

insert image description here

conda install -c conda-forge scikit-learn
  • 1

insert image description here

pip install rdkit-pypi
  • 1

insert image description here

pip install git+https://github.com/bp-kelley/descriptastorus 
  • 1

insert image description here

pip install DeepPurpose
  • 1

insert image description here

6. If an error similar to "pip-script.py' is not present." appears in the python virtual environment (for example, the blogger's python virtual environment is rothschildlhl), the reason is that the pip update was unsuccessful and the original pip version was also damaged.

  • update pip command
pip install --upgrade pip
  • 1

insert image description here

  • To solve the error, reinstall the latest version of pip according to the following command
conda activate rothschildlhl
  • 1
python -m ensurepip
  • 1
python -m pip install --upgrade pip
  • 1

insert image description here

Seven, Conda export, import and update virtual environment

  • Activate (switch) to the virtual environment that needs to be exported (the blogger takes the virtual environment transformers created before as an example).
conda activate transformers
  • 1

insert image description here

  • Save the virtual environment transformers export as a yml file and name it xrlhl.yml. This operation will only export the packages installed directly by the conda command. Most of the packages installed by pip in the original virtual environment need to be exported by pip to pip.txt, and then imported again. Go to the new virtual environment (note: the default export file is to the path in front of the command line, the blogger's example is C:\Users\25216, as shown in the figure below).
conda env export > xrlhl.yml
  • 1
pip freeze >pip.txt
  • 1

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

  • Import the yml file, and then create a new virtual environment according to the content of the yml file (Note: Before running the following command, you must change the path to the path of the yml file , and this is a new creation, then if it is the same computer, you must delete the original virtual environment with the same name or copy Change the name of the original virtual environment).
conda env create -f xrlhl.yml
  • 1
conda activate transformers
  • 1
pip install -r pip.txt
  • 1

insert image description here
insert image description here

  • Update the created virtual environment according to the yaml (yml) file (note: the example of the blogger is to update the virtual environment DeepPurpose with the xrlhl.yml file).
conda env update -n DeepPurpose --file xrlhl.yml
  • 1

insert image description here

Tags: A tutorial on creating a python virtual environment based on Anaconda under Windows

python artificial intelligence tool python Development language Anaconda Conda virtual environment

Related: A tutorial on creating a python virtual environment based on Anaconda under Windows

Navigation
  1. A tutorial on creating a python virtual environment based on Anaconda under Windows
    1. 1. Install Anaconda (if it has been successfully installed, you can ignore this step), click the link below to open the article, and install it according to the fifth major module of the article. Note: Anaconda's python virtual environment can be connected to Pycharm's compilation environment.
    2. 2. Open Anaconda Prompt after the installation is complete.
    3. 3. Basic commands of Conda
    4. 4. After learning the basic commands of Conda in the previous step, now start to create a python virtual environment (note: "conda create -n + the name of the python virtual environment to be created + python= + python version number", the blogger gives an example of the The name of the python virtual environment is rothschildlhl, and the python version number is 3.6. The creation time is about 5 minutes, please wait patiently, the creation of the python virtual environment is successfully completed in this step, and then be sure to add the content of the Scripts file of the newly created virtual environment to In the environment variable PATH, the blogger example is "D:\Anaconda\envs\rothschildlhl\Scripts" path).
    5. Fifth (negligible), then the blogger is importing and configuring the python virtual environment DeepPurpose to create a deep learning model library DeepPurpose, which is equivalent to practice. Just want to create a python virtual environment and it has been successfully completed in the fourth step. Note: If users enable VPN, they need to close VPN before installation to avoid unsuccessful installation.
    6. 6. If an error similar to "pip-script.py' is not present." appears in the python virtual environment (for example, the blogger's python virtual environment is rothschildlhl), the reason is that the pip update was unsuccessful and the original pip version was also damaged.
    7. Seven, Conda export, import and update virtual environment