一、参考资料
ubuntu16.04下安装&配置anaconda+tensorflow新手教程
二、安装Anaconda
下载 Miniconda镜像1 or Miniconda镜像2
# 下载wget Miniconda3-py39_4.10.3-Linux-x86_64.sh# 安装bash Miniconda3-py39_4.10.3-Linux-x86_64.sh
一路yes
安装过程中的选项
Do you accept the license terms? [yes|no]>>> yesAnaconda3 will now be installed into this location:/home/yichao/anaconda3>>> 回车If you'd prefer that conda's base environment not be activated on startup, set the auto_activate_base parameter to false: conda config --set auto_activate_base false
取消base为默认虚拟环境
conda config --set auto_activate_base false
设置环境变量
如果自动设置环境变量,则需要手动设置环境变量。
vim ~/.bashrc# 添加下面两行export PATH="/home/username/miniconda3/bin:$PATH"export PATH="/home/username/miniconda3/condabin:$PATH"# 重新加载当前用户配置文件source ~/.bashrc
Anaconda换源
Anaconda换源
anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror!
sudo gedit ~/.condarc# 清空缓存conda clean -i# 安装测试conda create -n myenv numpy
channels: - defaultsshow_channel_urls: truedefault_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
切换Python版本
# 打开配置文件vim ~/.bashrc# 添加配置alias python="/usr/bin/python"alias python3="/usr/local/bin/python3"alias pyana="/home/yoyo/anaconda3/bin/python3"# 更新配置source ~/.bashrc
卸载Anaconda
# 删除anaconda相关文件夹rm -rf ~/miniconda3rm -rf ~/anaconda3rm -rf ~/.condarm -rf ~/.condarcrm -rf ~/.anaconda# 删除环境变量# 删除关于conda部分的环境变量vi ~/.bashrc# 更新环境变量source ~/.bashrc
三、Anaconda的常用指令
# 1. 创建虚拟环境conda create -n your_env_name python=X.X(2.7、3.6等)# 1.1 在指定的位置创建虚拟环境conda create -p /PATH/TO/path# 查看所有的conda虚拟环境conda env list # 2. 激活虚拟环境source activate your_env_name(虚拟环境名称)# 3. 退出虚拟环境source deactivate your_env_name(虚拟环境名称)# 4. 删除虚拟环境conda remove -n your_env_name(虚拟环境名称) --all# 5. 安装包conda install package_name(包名)conda install scrapy==1.3 # 安装指定版本的包conda install -n 环境名 包名 # 在conda指定的某个环境中安装包# 6. 跳过安装失败的包,继续安装conda方式while read requirement; do conda install --yes $requirement; done < requirements.txtpip方式while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
查看
# 1. 查看安装了哪些包conda list# 2. 查看conda配置conda config --show# 3. 查看当前存在哪些虚拟环境conda env list 或 conda info -e或 conda info --envs
更新
# 1. 检查更新当前condaconda update conda# 2. 更新anacondaconda update anaconda# 3. 更新所有库conda update --all # 4. 更新pythonconda update python
清理conda缓存(conda报错segment fault的时候就是需要清理缓存哦)
conda clean -p //删除没有用的包conda clean -t //删除tar包conda clean -y --all //删除所有的安装包及cache
四、Anaconda的使用技巧
conda环境的复制(生成.yaml文件)和pip环境的复制(生成requirements.txt)
克隆环境
# 克隆一个BBB,环境和Tensorflow一样conda create -n BBB --clone Tensorflow
克隆环境(跨计算机)
# 跨计算机克隆# 目标计算机的环境目录 /PATH/TO/home/yoyo/miniconda3/envs/monodepth2-gpu conda create -n BBB --clone /PATH/TO/home/yoyo/miniconda3/envs/monodepth2-gpu
导入/导出环境
# 导出环境到yaml文件conda env export > env.yaml# 根据yaml文件复现环境conda env create -f env.yaml
注意:.yaml
文件移植过来的环境只安装了原来环境里用 conda install
等命令直接安装的包,使用pip安装的软件包没有移植过来,需要重新安装。
# 导出安装包pip freeze > requirements.txt# 安装pip install -r requirements.txt 或者conda install --yes --file requirements.txt
打包/解包(跨计算机)
# 将环境打包tar cvf monodepth2-gpu.tar envirement# monodepth2-gpu.tar文件,通过http、ssh等方式拷贝到目标计算机rsync -rzP */envirement.tar /home/yoyo/anaconda3/envs# 解包tar xvf monodepth2-gpu.tar# 修改conda的环境配置文件 ~/.conda/envirement.txt,在尾部添加拷贝的环境目录/home/yoyo/miniconda3/envs/monodepth2-gpu
搜索包
# 在anaconda官方仓库中搜索包(可能失效)anaconda search tensorflow-gpu -t conda #搜包[tensorflow-gpu]# 在清华源镜像中搜索包conda search tensorflow-gpu# 模糊查询conda search *scikit*或者conda search '*scikit*'# 显示包详细信息(可能失效)anaconda show aaronszs/tensorflow-gpu #查询第二条[aaronszs/tensorflow-gpu]
# 启动Anaconda Navigator 图形化界面anaconda-navigator# 导入anaconda所有库conda install anaconda
五、FAQ
anaconda 环境新建/删除/拷贝 jupyter notebook上使用python虚拟环境 TensorFlow
Anaconda 换国内源、删源最全集锦
在anaconda中安装不存在的python包并安装到指定环境中
【转】conda install和创建虚拟环境下载慢 pip下载慢 有用
TensorFlow+Faster-RCNN+Ubuntu 环境配置&代码运行过程
Q:根据yaml文件复现环境错误
Collecting package metadata (repodata.json): doneSolving environment: failedResolvePackageNotFound: - wincertstore==0.2=py36h7fe50ca_0 - jbig==2.1=h8d14728_2003 - libdeflate==1.7=h8ffe710_5 - zstd==1.5.0=h6255e5f_0 - lz4-c==1.9.3=h8ffe710_1 - lerc==2.2.1=h0e60522_0 - libtiff==4.3.0=h0c97f57_1 - jpeg==9d=h8ffe710_0 - mkl==2021.3.0=hb70f87d_564 - setuptools==52.0.0=py36haa95532_0 - tbb==2021.3.0=h2d74725_0 - certifi==2021.5.30=py36haa95532_0 - vc==14.2=h21ff451_1 - python==3.6.6=hea74fb7_0 - zlib==1.2.11=h62dcd97_1010 - numpy==1.19.5=py36h4b40d73_2 - pip==21.2.2=py36haa95532_0 - libpng==1.6.37=h1d00b33_2 - opencv==3.3.1=py36h20b85fd_1 - vs2015_runtime==14.27.29016=h5e58377_2 - intel-openmp==2021.3.0=h57928b3_3372 - xz==5.2.5=h62dcd97_1
Q:根据yaml文件复现环境错误
Collecting package metadata (repodata.json): doneSolving environment: failedResolvePackageNotFound: - wincertstore=0.2 - vc=14.2 - vs2015_runtime=14.27.29016
# 错误原因找不到相关的包# 解决办法注释对应的行
Q:NotWritableError: The current user does not have write permissions to a required path.
conda创建环境时报错:NotWritableError: The current user does not have write permissions to a required path.
tx2@tx2:~$ conda create -n efficientdet python=3.7Solving environment: failedNotWritableError: The current user does not have write permissions to a required path. path: /home/tx2/archiconda3/pkgs/urls.txt uid: 1000 gid: 1000If you feel that permissions on this path are set incorrectly, you can manuallychange them by executing $ sudo chown 1000:1000 /home/tx2/archiconda3/pkgs/urls.txtIn general, it's not advisable to use 'sudo conda'.‵
# 错误原因用户没有对anaconda3文件夹的读写权限,造成其原因可能是由于在安装anaconda时使用了管理员权限。# 解决办法cd /home/tx2sudo chown tx2:tx2 -R archiconda3
Q:subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.
subprocess.CalledProcessError: Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.
(mslite) liulinjun@LAPTOP-4DTD5D42:~/MyDocuments/mindspore$ conda info --envs# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<< Traceback (most recent call last): File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/exceptions.py", line 1079, in __call__ return func(*args, **kwargs) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main.py", line 84, in _main exit_code = do_call(args, p) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/conda_argparse.py", line 83, in do_call return getattr(module, func_name)(args, parser) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main_info.py", line 316, in execute info_dict = get_info_dict(args.system) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main_info.py", line 135, in get_info_dict _supplement_index_with_system(virtual_pkg_index) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/core/index.py", line 163, in _supplement_index_with_system dist_name, dist_version = context.os_distribution_name_version File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/auxlib/decorators.py", line 268, in new_fget cache[inner_attname] = func(self) File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/base/context.py", line 786, in os_distribution_name_version from .._vendor.distro import id, version File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 1084, in <module> _distro = LinuxDistribution() File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 599, in __init__ self._lsb_release_info = self._get_lsb_release_info() \ File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 943, in _get_lsb_release_info raise subprocess.CalledProcessError(code, cmd, stdout, stderr) subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.`$ /home/liulinjun/miniconda3/bin/conda info --envs`An unexpected error has occurred. Conda has prepared the above report.If submitted, this report will be used by core maintainers to improvefuture releases of conda.Would you like conda to send this report to the core maintainers?[y/N]: NNo report sent. To permanently opt-out, use $ conda config --set report_errors false
File "/root/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 943, in _get_lsb_release_info raise subprocess.CalledProcessError(code, cmd, stdout, stderr) subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.`$ /root/miniconda3/bin/conda info --envs`An unexpected error has occurred. Conda has prepared the above report.If submitted, this report will be used by core maintainers to improvefuture releases of conda.
# 解决方法一find / -name lsb_releasemv /usr/bin/lsb_release /usr/bin/lsb_release.bak或者rm -rf /usr/bin/lsb_release# 解决方法二如果方法一无法解决,可尝试方法二。conda config --set report_errors false