Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Anaconda Tutorial

Anaconda Tutorial

Anaconda is a Python distribution for data science and machine learning; it bundles common tools and libraries so you can program, analyze data and build models more easily.

The tool for managing Anaconda packages and their dependencies is the conda command, which will be covered in detail later in this article.

Compared with traditional Python pip By comparison, conda makes it more convenient to switch between different environments, and environment management is simpler.

Why Choose Anaconda

For quantitative learning, Anaconda solves three pain points:

Advantage Description
Out of the box Install it like ordinary software, with essential quant libraries such as NumPy, Pandas, Matplotlib, SciPy and Scikit-learn preinstalled
Environment Isolation You can create multiple independent Python environments (for example one for quant libraries, one for a Web framework) to avoid library version conflicts
Integrated Tools It ships with the Jupyter Notebook interactive programming environment and the Spyder development environment, especially suited to data analysis
Cross-platform Supports Windows, macOS and Linux

Jupyter Notebook deserves a special mention: it lets you run code piece by piece in the browser and see chart output immediately, making it the most common way of working in quantitative research.


Installing Anaconda

Anaconda installer download address:https://www.anaconda.com/download.

Download the installer matching your operating system:

macOS Platform

Double-click to open the downloaded installer, select Install for me only:

Click Install button to start the installation:

After installation, click Continue button to see the installation-complete screen:

For more details on installing on macOS, see the official documentation:https://docs.anaconda.com/free/anaconda/install/mac-os/

Windows Platform

Windows is similar to macOS: double-click the installer, agree to the license, then keep clicking Next with the default settings.

Choose the installation directory:

in Advanced Installation Options , do not check Add Anaconda to my PATH environment variable(Add Anaconda to the environment variables).

Checking this option affects how other programs use the system PATH; after installation, just use the command-line tools bundled with Anaconda.

Click the Install button to install; on success the following screen appears:

Click Next button to finish the installation:

For more details on installing on Windows, see the official documentation:https://docs.anaconda.com/free/anaconda/install/windows/

Linux Platform

On Linux, install via the command line; you can replace the version number in the command:


# 下载安装脚本(版本号可替换为官网最新版本)
curl -O https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh

# 运行安装脚本
bash Anaconda3-2023.09-0-Linux-x86_64.sh

For more details on installing on Linux, see the official documentation:https://docs.anaconda.com/free/anaconda/install/linux/


Anaconda Graphical Interface

After installation, open the Anaconda graphical management interface to view and manage different environments:

Click Environments You can view the installed environments:

At the bottom of the interface there are buttons to create and delete environments, so you can operate freely.

The graphical interface is good for beginners to browse, but for daily development the conda command-line approach below is recommended - it is faster and easier to automate with scripts.


Common conda Commands

conda is Anaconda's package manager, used to install, update and uninstall packages, and to create and manage different Python environments.

Environment Management

Create an environment named myenv :


# 创建默认版本的环境
conda create --name myenv

Specify the Python version when creating an environment:


# 创建名为 myenv 的环境,并指定 Python 版本为 3.11
conda create --name myenv python=3.11

Activate the environment:


# 激活名为 myenv 的环境
conda activate myenv

Exit the current environment:


conda deactivate

View all created environments:


# 当前激活的环境会标记 * 号
conda env list

Create a new environment by cloning an existing one:


# 复制 myenv 环境为 myclone
conda create --name myclone --clone myenv

Delete an environment:


conda env remove --name myenv

Package Management

conda's package management commands are similar to pip:

Command Function
conda install package_name Install a specified package
conda install package_name=1.2.3 Install a specified version of a package
conda update package_name Update an installed package
conda remove package_name Uninstall a package
conda list View all packages and their versions installed in the current environment
conda search package_name Search for a package in the conda repository

Other Common Commands


# 查看帮助
conda --help

# 查看 conda 版本
conda --version

# 清理缓存与不再需要的包
conda clean --all

Hands-on: Create a Dedicated Quant Environment

After learning the commands above, let's set up the environment used in later chapters of this tutorial: create a standalone quant environment and install the libraries needed for quant work.

Isolating the environment from the project means that even if you freely upgrade other libraries later, the quant environment will not be broken.

Example

1. Create an environment named quant, specifying Python 3.11

conda create

--name

quant

python

=

3.11

2. Activate the quant environment

conda activate quant

3. Install the three basic quant libraries: data processing, numerical computing and visualization

conda

install

pandas numpy matplotlib

4. Install the financial data retrieval library (used in later chapters, installed here in advance)

pip

install

yfinance akshare

5. Install the interactive charting library (used in the K-line chart chapter)

pip

install

pyecharts

6. Verify the installation

python

-c

"import pandas, numpy, matplotlib, yfinance; print('Environment ready')"

The output is shown below:


环境就绪

Seeing 'Environment ready' confirms the quantitative environment is set up.

Jupyter Notebook

Jupyter Notebook is strongly recommended for quantitative research: it puts code, charts and notes in one document and executes them piece by piece, which is ideal for the iterative process of strategy research.


# 在 quant 环境中安装 Jupyter Notebook(可选)
conda install jupyter

# 启动 Jupyter Notebook
jupyter notebook

After running, the browser opens automatically; create a new Notebook to start writing code.

Note: be sure to activate the quant environment before starting Jupyter, otherwise the Notebook runs in the base environment and cannot find the quant libraries you installed.