一、在开始之前,确保已经将pip、conda、ubuntu源更换为清华源
说明:在终端中执行相应的命令即可。
1 更换pip源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U\
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
2 更换conda源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
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/msys2/
# 安装pytorch还需要再添加如下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/pytorch/
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
3 更换ubuntu源
通过图形界面更换最简单:显示应用程序–>软件和更新–>ubuntu软件–>下载自–>
二、在anaconda上创建虚拟环境
win10下打开CMD,或者在linux终端上执行以下代码
conda create --name tf2 python=3.7
# “tf1”是你建立的Conda虚拟环境的名字,python=3.6是再这个虚拟环境里安装python3.6
conda activate tf2
# 进入名为“tf1”的虚拟环境
找到你需要的tensorflow版本号并安装:
# 查找可用的TensorFlow版本号
conda search --full --name tensorflow
# 在虚拟环境中安装TensorFlow CPU版本,可修改为任意版本号
pip install tensorflow==1.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 在虚拟环境中安装TensorFlow GPU版本,可修改为任意版本号
conda install cudatoolkit==9.0 cudnn
pip install tensorflow-gpu==1.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
三 、在Pycharm上使用虚拟环境中的python解释器
1 点击左上角的文件,找到设置(setting)
2 在项目中找到Project Interpreter,然后选择相应的python解释器即可, 或者直接新建项目,选择Existing interpreter
附录
在 Python 开发中,很多时候我们希望每个应用有一个独立的 Python 环境(比如应用 1 需要用到 TensorFlow 1.X,而应用 2 使用 TensorFlow 2.0)。这时,Conda 虚拟环境即可为一个应用创建一套 “隔离” 的 Python 运行环境。使用 Python 的包管理器 conda 即可轻松地创建 Conda 虚拟环境。常用命令如下:
conda create --name [env-name] # 建立名为[env-name]的Conda虚拟环境
conda activate [env-name] # 进入名为[env-name]的Conda虚拟环境
conda deactivate # 退出当前的Conda虚拟环境
conda env remove --name [env-name] # 删除名为[env-name]的Conda虚拟环境
conda env list # 列出所有Conda虚拟环境