查看python路径和版本、配置Conda环境和Pytorch安装

参考:

获取python及三方包的版本,安装路径等信息 – 知乎 (zhihu.com)

conda常用命令操作 – Eric小星 – 博客园 (cnblogs.com) (79条消息) conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题_阿尔发go的博客-CSDN博客_conda remove
(79条消息) conda常用命令汇总,随时查询(最全)_wmsofts的博客-CSDN博客_conda常用命令

(69条消息) 使用Anaconda下载工具包_Marho11的博客-CSDN博客

一、查看python的安装路径和版本

cmd中查看
Windows下查看python 版本与Linux查看python版本方法一致,在命令行下输入
python --version
#或
python -v
Windows 下查看python安装路径,在命令行输入:
where python

Linux下查看当前python安装路径,在命令行输入:
whichis python

或者查看所有python所在路径
whereis python

python 编辑器中查看
import  sys
print(sys.version_info) # python版本信息
print(sys.version) # python版本信息
print(sys.executable) # python安装路径

二、Conda命令

这里面一定注意,不要把anaconda装到C盘。因为你后面会有好多个虚拟环境,安装很多包;另外,建议把conda命令加入到环境变量里面去。

强调安装pytorch的环境有啥包问题一定要先切换到该环境,别一直在主环境了安装。

#查看状态
conda config --show

查看信息
conda info

查看环境
conda info -e 或者conda env list

切换环境
conda activate
列出当前环境下安装的包, 非常常用
conda list
搜索包
conda search package_name

实验过程中,如果发现某些包没有,直接安装
conda install package_name  # 也可以带版本号
pip install package_name

如果发现装错版本了,想要卸载掉包
conda remove package_name
pip uninstall package_name

更新包
conda update package_name

#新建虚拟环境 可以指定python版本和一些包的版本
conda create -n env_names package_names   # conda create -n tfenv python=3.7

进入虚拟环境,这时候面临着对一些包的操作,就是上面包的相关命令了
activate tfenv

离开虚拟环境
deactivate

删除虚拟环境
conda env remove -n env_name

配置下载通道
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
安装时PyTorch,官网给的安装命令需要去掉最后的-c pytorch,才能使用清华源
conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
bioconda
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
menpo
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
设置搜索时显示通道地址
conda config --set show_channel_urls yes

######以上和以下两者貌似等价,但下面(不一定有用)

生成文档
conda config --set show_channel_urls yes
生成文件,C:\Users\14193\.condarc
然后打开,复制粘贴以下命令,保存即可

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_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
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

试试这个
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
show_channel_urls: true

先把更新conda到最新版
conda update conda
建议使用,官方提示语句
conda update -n base -c defaults conda

然后把anaconda更新的新版,只有base这个环境更新到最新
conda update anaconda
conda update anaconda-navigator    //update最新版本的anaconda-navigator
进入到需要升级的配置环境,也更新到最新版的Anaconda
conda update --all

查看conda环境详细信息:
conda info

分享/备份环境,环境的.yml文件。
首先激活到要分享的环境,在当前工作目录下生成一个environment.yml文件。
conda env export > environment.yml

对方拿到environment.yml文件后,将该文件放在工作目录下,可以通过以下命令从该文件创建环境。
conda env create -f environment.yml

三、查看CUDA和安装Pytorch

cmd查看CUDA版本

nvidia-smi

查看python路径和版本、配置Conda环境和Pytorch安装

CUDA Toolkit 11.7 Downloads | NVIDIA Developer

pytorch官网:PyTorch

创建虚拟环境

其中,pytorch-gpu为虚拟环境名,可以自己设置。

– 切换到新建虚拟环境下

#进入虚拟环境
conda activate myTorchGpu
#切换镜像源,conda中只要这条语句就可以下载pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

#安装pytorch
#中间安装失败,再重复安装命令即可
conda install pytorch torchvision torchaudio cudatoolkit=11.3

#等待安装完成,之后进行验证,依次输入以下命令,GPU和CPU都可以使用这个验证
python
import torch
print(torch.__version__)  #注意是双下划线
torch.cuda.is_available()

如果结果为true,则表示安装成功。

查看python路径和版本、配置Conda环境和Pytorch安装

删除虚拟环境

conda activate base
conda remove -n pytorch-gpu --all

四、Conda安装包的方法

在线安装

先说在线安装,首先打开Anaconda Prompt,就是Anaconda的命令行

  1. 用conda install pkgname即可,pkgname为包名

  2. 当找不到包的时候,尝试下面的语句

conda install -c conda-forge pkgname

  1. 依旧找不到的话,用下面的搜索包名,进行最后尝试

anaconda search -t conda pkgname 找到需要的pkgname全名后,输入下面的指令
anaconda show pkgname(全名),会列出channel_url
最后根据列出来的url(就是看着像网址的那些)选择相应版本,输入下面的指令进行下载
conda install –channel channel_url pkgname

本地安装

如果上述都不可行,尝试下载后进行本地安装(zip/.tar.gz)

  1. 从github(或者其他来源)中下载zip

解压后里面有一个setup.py的文件

在命令行中进入解压路径,输入python setup.py install

然后会出现dist文件夹,其中会生成一个.tar.gz类型文件,后续同下种方式

  1. 下载.tar.gz类型文件

根据文件的绝对路径执行命令conda install –use-local pkg

其中,pkg为绝对路径

加载yaml文件

activate tfenv #进入环境

安装所有包
conda env update -f=/path/to/environment.yaml

附录:系统信息代码

coding:utf-8
import platform
global var
是否显示日志信息
SHOW_LOG = True
def get_platform():
    '''获取操作系统名称及版本号'''
    return platform.platform()

def get_version():
    '''获取操作系统版本号'''
    return platform.version()

def get_architecture():
    '''获取操作系统的位数'''
    return platform.architecture()

def get_machine():
    '''计算机类型'''
    return platform.machine()

def get_node():
    '''计算机的网络名称'''
    return platform.node()

def get_processor():
    '''计算机处理器信息'''
    return platform.processor()

def get_system():
    '''获取操作系统类型'''
    return platform.system()

def get_uname():
    '''汇总信息'''
    return platform.uname()

def get_python_build():
    ''' the Python build number and date as strings'''
    return platform.python_build()

def get_python_compiler():
    '''Returns a string identifying the compiler used for compiling Python'''
    return platform.python_compiler()

def get_python_branch():
    '''Returns a string identifying the Python implementation SCM branch'''
    return platform.python_branch()

def get_python_implementation():
    '''Returns a string identifying the Python implementation. Possible return values are: 'CPython', 'IronPython', 'Jython', 'PyPy'.'''
    return platform.python_implementation()

def get_python_version():
    '''Returns the Python version as string 'major.minor.patchlevel'
    '''
    return platform.python_version()

def get_python_revision():
    '''Returns a string identifying the Python implementation SCM revision.'''
    return platform.python_revision()

def get_python_version_tuple():
    '''Returns the Python version as tuple (major, minor, patchlevel) of strings'''
    return platform.python_version_tuple()

def show_python_all_info():
    '''打印python的全部信息'''
    print('The Python build number and date as strings : [{}]'.format(get_python_build()))
    print('Returns a string identifying the compiler used for compiling Python : [{}]'.format(get_python_compiler()))
    print('Returns a string identifying the Python implementation SCM branch : [{}]'.format(get_python_branch()))
    print('Returns a string identifying the Python implementation : [{}]'.format(get_python_implementation()))
    print('The version of Python : [{}]'.format(get_python_version()))
    print('Python implementation SCM revision : [{}]'.format(get_python_revision()))
    print('Python version as tuple : [{}]'.format(get_python_version_tuple()))

def show_python_info():
    '''只打印python的信息,没有解释部分'''
    print(get_python_build())
    print(get_python_compiler())
    print(get_python_branch())
    print(get_python_implementation())
    print(get_python_version())
    print(get_python_revision())
    print(get_python_version_tuple())

def show_os_all_info():
    '''打印os的全部信息'''
    print('获取操作系统名称及版本号 : [{}]'.format(get_platform()))
    print('获取操作系统版本号 : [{}]'.format(get_version()))
    print('获取操作系统的位数 : [{}]'.format(get_architecture()))
    print('计算机类型 : [{}]'.format(get_machine()))
    print('计算机的网络名称 : [{}]'.format(get_node()))
    print('计算机处理器信息 : [{}]'.format(get_processor()))
    print('获取操作系统类型 : [{}]'.format(get_system()))
    print('汇总信息 : [{}]'.format(get_uname()))

def show_os_info():
    '''只打印os的信息,没有解释部分'''
    print(get_platform())
    print(get_version())
    print(get_architecture())
    print(get_machine())
    print(get_node())
    print(get_processor())
    print(get_system())
    print(get_uname())

def test():
    print('操作系统信息:')
    if SHOW_LOG:
        show_os_all_info()
    else:
        show_os_info()
    print('#' * 50)
    print('计算机中的python信息:')
    if SHOW_LOG:
        show_python_all_info()
    else:
        show_python_info()

def init():
    global SHOW_LOG
    SHOW_LOG = True

def main():
    init()
    test()

if __name__ == '__main__':
    main()

Original: https://blog.csdn.net/qq_40925617/article/details/125061184
Author: 稳健的不高冷的强哥
Title: 查看python路径和版本、配置Conda环境和Pytorch安装

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/651017/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球