Anaconda是开源的Python包管理器。既是Python各种库的大礼包集合,也是一个能创建虚拟机环境的工具。我安装它的原因是因为我要使用Python的AkShare库获取数据,要求Python 3.6 及以上版本。我电脑Python 2.7.15,又不想升级,所以安装anaconda,用anaconda创建虚拟机环境,在虚拟机环境使用Python3.6,本地电脑保持原版本不变。 下载地址www.anaconda.com选择download,

下载最新版本安装包Anaconda3-2020.02-Windows-x86_64.exe,



这里选择安装目录,将来创建的各个版本Python的环境文件也都放在这个目录的子目录下

这里我勾选了下面,没有勾选上面

开始安装
安装完成,在开始搜索ana会出现Anaconda prompt,我是以管理员身份运行的,不用管理员权限应该也没问题。

最前面的(base)表示你正处于Anaconda的基础环境,基础环境一般不真正使用,要根据自己具体项目需要建立自己的环境,否则这个工具的威力大打折扣了。对比base环境的Python版本和正常命令行工具的Python版本,可以看到两者互不影响。


建立环境的时候要下载很多包,如果下载速度慢会导致下载timeout而终止,会报错,

解决办法是把channels设置为清华大学的镜像地址,在命令行依次执行下面命令。建议先设置下面的channels,然后创建你的第一个环境。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/
建立自己环境的命令conda create -n ak_test python=3.7.3, ak_test是新建环境的名称,叫什么都可以,3.7.3是我指定的版本,需要哪个版本就写哪个。




到这里说明创建ak_test环境完成,提示你使用环境和离开环境的命令。
执行conda activate ak_test,看到(base)变成了(ak_test),说明已经进入了ak_test环境。

我建立这个环境是要使用akshare,所以安装akshare,报错time out


应该是国内网络问题,使用阿里云镜像进行安装,命令如下
pip install akshare -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com --upgrade


安装成功!
如果执行conda deactivate离开这个环境切换到(base)环境,可以看到找不到akshare模块,说明base和ak_test环境是隔离的。

下面两个小例子展示akshare库的功能。 股票策略指数曲线aksharetest.py
import akshare as ak
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
stock_df = ak.zdzk_fund_index(30, plot=False)
futures_df = ak.zdzk_fund_index(32, plot=False)
fig = plt.figure(111, figsize=(20, 10), dpi=300)
adjust_stock_df = stock_df["20150102":] / stock_df["20150102"] * 1000
adjust_stock_df.plot(linewidth=4)
adjust_futures_df = futures_df["20150102":] / futures_df["20150102"] * 1000
adjust_futures_df.plot(linewidth=4)