(目录)
一、Python3在Windows上的安装
- 借助mambaforge(miniforge)安装python。mamba可以为python建立独立的虚拟环境。事实上,mamba也是基于conda的。mamba采用c++重写了部分conda得解析过程。
Anaconda 现在个人不收费,企业是要收费的——如果使用企业的IP也是要收费的。所以为了避免麻烦,使用开源的mambaforge(miniforge)来替换。
- mamba国内镜像站:https://mirrors.nju.edu.cn/help/miniforge
- 安装包的下载地址在:
- https://mirror.nju.edu.cn/github-release/conda-forge/miniforge/LatestRelease/
- https://mirrors.tuna.tsinghua.edu.cn/github-release/conda-forge/miniforge/LatestRelease/
- https://github.com/conda-forge/miniforge/
- 下载:Mambaforge-Windows-x86_64.exe
自2023年9月起,Miniforge3-23.3.1-0发布后其功能与Mambaforge已完全一致,两者的区别仅在安装器名称和安装路径。后续Mambaforge相关的bug reports将不再维护,因此官方建议安装Miniforge3。即可以下载:==Miniforge3-Windows-x86_64.exe== 如果是linux环境可以使用:==bash Miniforge3-Linux-x86_64.sh==
- 开始安装
配置环境变量
把mamba的安装目录和安装目录下的Scripts/目录配置到Path环境变量中。
在shell中运行:
E:\tmp>python -V
Python 3.11.8
# 初始化mamba,使得可以在shell终端中运行mamba的命令
E:\tmp>mamba init
no change D:\mambaforge\Scripts\conda.exe
no change D:\mambaforge\Scripts\conda-env.exe
no change D:\mambaforge\Scripts\conda-script.py
no change D:\mambaforge\Scripts\conda-env-script.py
no change D:\mambaforge\condabin\conda.bat
no change D:\mambaforge\Library\bin\conda.bat
no change D:\mambaforge\condabin\_conda_activate.bat
no change D:\mambaforge\condabin\rename_tmp.bat
no change D:\mambaforge\condabin\conda_auto_activate.bat
no change D:\mambaforge\condabin\conda_hook.bat
no change D:\mambaforge\Scripts\activate.bat
no change D:\mambaforge\condabin\activate.bat
no change D:\mambaforge\condabin\deactivate.bat
modified D:\mambaforge\Scripts\activate
modified D:\mambaforge\Scripts\deactivate
modified D:\mambaforge\etc\profile.d\conda.sh
modified D:\mambaforge\etc\fish\conf.d\conda.fish
no change D:\mambaforge\shell\condabin\Conda.psm1
modified D:\mambaforge\shell\condabin\conda-hook.ps1
modified D:\mambaforge\Lib\site-packages\xontrib\conda.xsh
modified D:\mambaforge\etc\profile.d\conda.csh
modified C:\Users\dell\Documents\WindowsPowerShell\profile.ps1
modified HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
==> For changes to take effect, close and re-open your current shell. <==
当出现上述回显后,表示mamba和python都安装成功了。
二、mamba的使用
# 注意mamba命令与conda命令是可以互换兼容的
# 获得帮助
mamba -h
mamba list -h
# 创建虚拟环境(使用默认路径 ~/.conda/envs/env_name)
mamba create -n env_name python=3.11
# 指定路径创建虚拟环境(注意-n和-p参数不能一起用)
# 使用下边的方法创建的虚拟环境是没有name的,只能带路径使用。下边所有使用name的地方都要替换为[-p] path。
mamba create -p D:\python_envs\env_name python=3.11
# 如何解决没有name的问题?
conda config --append envs_dirs D:\python_envs\
# 添加路径之后就可以用-n env_name在D:\python_envs\下根据name=env_name来检索虚拟环境了。
conda config --get envs_dirs
conda config --show-sources
# 显示所有的配置参数值
conda config --show
# 删除虚拟环境
mamba env remove -n env_name
conda env remove -n env_name --all
# 显示已创建的虚拟环境(默认就一个base)
mamba env list
(D:\python_envs\py3d11) E:\AppPrjs\python_prjs\test1>mamba env list
# conda environments:
#
base D:\mambaforge
py3d11 * D:\python_envs\py3d11 星花表示当前使用的虚拟环境
d:\mambaforge
py3d11 d:\python_envs\py3d11
# 激活虚拟环境
mamba activate env_name
E:\AppPrjs\python_prjs\test1>mamba activate py3d11
(D:\python_envs\py3d11) E:\AppPrjs\python_prjs\test1> 这里test1是当前的工作目录,小括号里是当前的虚拟环境所在目录——工作目录可以和虚拟环境目录不在一起
# 去激活虚拟环境
mamba deactivate
# 显示虚拟环境中已安装的软件包
mamba list -n env_name
# 导出环境配置
conda env export -f d:\myemv.yml -n myenv
# 导入环境配置
conda env create -f d:\myemv.yml
# conda环境备份
conda create -n python35copy --clone python35
# 使用conda-pack(linux下的参考)
# pip install git+https://github.com/conda/conda-pack.git
conda install -c conda-forge conda-pack
tar -xzf my_env.tar.gz -C anaconda_root/envs/my_env
source my_env/bin/activate
conda-unpack
source deactivate
# 加速channel
# 安装目录下的.condarc是全局channel配置文件
# ~/.condarc 是用户默认的channel配置文件,在Windows下可用下述命令生成用户默认channel配置文件
conda config --set show_channel_urls yes
# Miniforge可如下配置.condarc
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirror.nju.edu.cn/anaconda/cloud/conda-forge/
# 清除索引缓存,保证用的是镜像站提供的索引。
conda clean -i
# 或者 conda clean -a
# 显示已有的通道源
conda config --show-sources
# 也可以添加清华的源
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/
# 查询软件包
mamba search numpy
# 安装软件包(pip,conda,mamba都可以用来安装软件包)
mamba install <package_name>
mamba install numpy=1.7.2 -y
三、配置VSCode
- 需要安装的插件 Python,Python Debugger
一般安装完Python插件后Python Debugger插件也会自动安装。
然后选择解释器(Interpreter)
当前的解释器
运行一下
Windows下选择cmd作为VSCode的默认终端
. : 无法加载文件 C:\Users\dell\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.
microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\dell\Documents\WindowsPowerShell\profile.ps1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
上述问题的解决
四、Anaconda的清华源channel
帮助:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
# 生成~/.condarc
conda config --set show_channel_urls yes
# 配置.condarc
channels:
- defaults
show_channel_urls: true
default_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/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
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
# 清除索引缓存,保证用的是镜像站提供的索引。
conda clean -i
# 显示一下配置
conda config --show-sources
五、pip的清华源channel
帮助:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple