conda、pip使用Tips

 

  环境都从base开始激活,不要套娃似的一个接一个激活环境,各个环境间会相互影响。base中不要新装任何东西。

  安装完conda环境后,各个包建议用pip安装,更快。

 Anaconda

1.  安装anaconda后Terminal 出现(base) username@xxx: ~$

base环境不要做任何修改

conda config --set auto_activate_base false   # 更改配置
source .bashrc                    # 更新上面的修改
conda create -n env-name packages_to_install  # 创建新环境,package eg:python=3.8
conda remove -n env-name --all          # 删除虚拟环境

conda [de]activate [env-name]           # 激活之后才有效
conda env list
conda env remove -n env-name
conda update/install/remove packagename1[=version] [package2] ...
conda clean -a                    # 删除没用的包、缓存、tar包等,比较安全  ——参考博客
** 如果安装出问题,就clean一下,会将旧包的备份删掉,一般也能解决安装包时出的问题

   ** 如果环境出了很大问题,暂时没办法解决,可以回退,base环境回退方法如下:  ——参考博客

conda list --revisions    # 查看历史版本
conda install --rev 0     # 回退到版本0(效果不一定好)

   ** 如果要重新安装anaconda,要把之前的安装目录先删除,否则会影响后面的安装。

 

如果出现找不到想要版本的包,可以换源,下面是清华源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
# 北外源
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/peterjc123/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/r/

conda config --set show_channel_urls yes

 * 可以去~/.condarc中将channels中的 default 注释,防止从conda官网拉包影响速度

 

** 目录含义

anaconda3/ 中,目录'pkgs'是下载、存放缓存,以及提取下载的conda包的地方,在构建新的虚拟环境时,如果pkgs目录下的包符合要求,可以优先链接到该目录下,避免重复的下载;anaconda一起携带的包也会放在这里。  ——参考博客

 

2. 安装pytorch

可以去torch官网看看命令,官网

默认源一般比较慢,去掉命令中的 -c pytorch 就可以用自己配置的源。

3. 安装sklearn,skimage,opencv-python,yaml

conda install scikit-learn
conda install scikit-image conda
install opencv
conda install pyyaml

 4. 问题:Warning: >10 possible package resolutions

原由:在base环境进行了修改和包安装,影响了其它环境

解决:卸掉base环境中新安装的包,去除修改

 

 

在虚拟环境中,conda安装的包在anaconda3/pkgs/目录下,相当于可以共用;而pip则安装在env_name/python x.y/site-packages/目录下

注意:

1)换了conda源不等于换了pip源,注意区分

2)每个虚拟环境的pip是不互通的,需要分别给它们设置国内源

 

Pip

1. pip使用国内源

# 以 清华源 为例
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package       # 临时使用

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple    # 设为默认源

 

# 中科大源
https://pypi.mirrors.ustc.edu.cn/simple/
# 北外源
https://mirrors.bfsu.edu.cn/pypi/web/simple/
# 阿里源
https://mirrors.aliyun.com/pypi/simple/
# 豆瓣源
https://pypi.douban.com/simple/

 难以自动安装库,可以手动根据pip给出的链接下载whl文件,自行安装

 

2. 重新安装pip

  由于一些操作导致pip损坏不能使用,可以重新安装  ——参考stackoverflow 或 简书(未验证)

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py     # 获取脚本
python get-pip.py --ignore-installed    # 重新安装

 

3. pip install matplotlib 出现错误 TypeError: string argument expected, got 'NoneType'

原因:未知,可能是安装前置依赖时出了问题

解决:用下面的命令,前置依赖手动安装——参考

pip install xxx --ignore-install pexpect

 

4. pip出现uninstall错误:Cannot uninstall'certifi'. It is a distutils installed project and thus we cannot accurately determine which files belong to...

  1)进入anaconda3/ 目录,执行下面的命令,找到相关文件后,删除不需要的就行了(如site-packages中的)。

find '.' -name "*certifi*.egg-info"          # 找到目录下的所有certifi相关的文件,有可能找不到

* 注意,不要直接用 参考博客 中的命令,那样会误删其它环境的certifi相关包,并损坏pip

   2)或者在安装命令中添加选项 --ignore-installed certifi 直接跳过该问题

pip install xxxx  --ignore-installed certifi

 

5. 删除 pip 缓存

  Windows下,删除 %LocalAppData%\pip\Cache 目录下的所有内容即可。

6.  错误/bin/python: bad interpreter: No such file or directory

一般是路径错误,如从一个系统复制到另一个系统导致的路径不一致,按下面的方法执行 ——参考

which pip
# 输出如:path/to/3.7/bin/pip

which python3
# /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

vim /Library/Frameworks/Python.framework/Versions/3.7/bin/pip
# 修改第一行指向的python路径为 上面得到的路径

可以解决当前的报错。

若整个环境比较复杂,其他库的路径也会有问题,最好还是想办法在本地自行安装。

 

7. 安装包

pip install opencv-python==4.4.0.44    # opencv

 

posted @ 2020-06-23 18:38  谷小雨  阅读(543)  评论(0编辑  收藏  举报