Anaconda tutorial, Python version control
Article directory
A few days ago, a fan asked me about Anaconda. I found that many people were not very clear about the concept of Anaconda, so I decided to write an article to introduce Anaconda.
There are many kinds of python version control, two commonly used version control methods are Pyenv and Anaconda.
Pyenv: is a powerful Python version management tool that allows you to install and manage multiple Python versions on the same machine
Anaconda: is a Python distribution for scientific computing that includes a powerful package management and environment management system.
Which one should I use?
If you just want to have multiple Python versions installed on your computer, then using Pyenv will do the trick. If you want to install multiple Python versions on your computer and also want to manage the dependencies of these versions, then use Anaconda. Mainly to develop Python applications, Pyenv may be a good choice, if your work mainly involves data science, machine learning or big data processing, then Anaconda may be more suitable for you.
Here mainly introduces the installation and use of Anaconda.
1: Anaconda installation
I will introduce the installation of Anaconda on three platforms, namely Windows, Linux and MacOS.
1.1:Windows
Visit Anaconda's official website to download the Anaconda installer for Windows, download link: https://www.anaconda.com/download
For example: Click to download Anaconda3-2023.03-1-Windows-x86_64
Double-click the downloaded .exe file and follow the prompts to install it.
After the installation is complete, open the command line and use the following command to check whether the installation is successful:
conda --version
conda list
1.2:Linux
Visit Anaconda's official website to download the Anaconda installer for Linux, download link: https://www.anaconda.com/download
For example:
wget https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
According to the example, you will download a file named Anaconda3-2023.03-1-Linux-x86_64.sh, this file name is not fixed, it will change with the version of Anaconda. Please note the file name you downloaded for use in subsequent commands.
This is a shell script that you can run with the bash command:
bash Anaconda3-2023.03-1-Linux-x86_64.sh
If you have no special needs, just press Enter and Yes all the way.
Restart the terminal, or use the following command for the installation to take effect:
source ~/.bashrc
After the installation is complete, open the command line and use the following command to check whether the installation is successful:
conda --version
conda list
1.3:MacOS
Visit Anaconda's official website to download the Anaconda installer for MacOS, download link: https://www.anaconda.com/download
For example: Click to download Anaconda3-2023.03-1-MacOSX-arm64
Open the downloaded .pkg file and follow the prompts to install it.
After the installation is complete, open the command line and use the following command to check whether the installation is successful:
conda --version
conda list
2: Anaconda uses
2.1: Create a new environment
Anaconda can create multiple environments, and different versions of Python and different packages can be installed in each environment.
conda create --name myenv
This will create an environment called myenv that contains Python and some core dependencies.
Additionally, the Python version can also be specified:
conda create -n myenv python=3.9
If the following error is displayed, try changing the Python version.
PackagesNotFoundError: The following packages are not available from current channels:
- python=3.9
Current channels:
2.2: Install Python packages
conda install -n myenv numpy
Here we have installed a package called numpy in the myenv environment. This package is used for scientific computing. It provides multidimensional array objects and various functions for processing arrays.
2.3: Activate the environment
conda activate myenv
It should be noted that this is a new version of the command. If you are using an old version of Anaconda, please use the following command:
windows:activate myenv
Linux and MacOS:
source activate myenv
When you successfully activate the environment, the environment name will be displayed in front of the command line, for example:
(myenv) $
At this point, you can use the pip command to install Python packages, for example:
pip install numpy
You can also use the conda command to install Python packages, for example:
conda install numpy
You can also use the python command to run Python programs, for example:
python hello.py
Check the python version of the current environment:
python --version
In short, when you activate an environment, any operation you do is performed in this environment. You can use pip, conda, and python commands as you would in a normal Python environment.
2.4: Exit the environment
When you are done working in the current environment, you can exit the environment with the following command:
conda deactivate
It should be noted that this is a new version of the command. If you are using an old version of Anaconda, please use the following command:
windows:deactivate
Linux and MacOS:
source deactivate
2.5: Delete environment
When you no longer need an environment, you can use the following command to delete the environment:
conda remove -n myenv --all
2.6: Check the environment
conda info --envs
2.7: Export environment
conda env export > environment.yaml
2.8: Import environment
conda env create -f environment.yaml
References
next...
Next, what should we do?