In this post, I am going to explain how I failed to use the conda command on a machine with Chinese version of Windows 10, even though I have added the relevant directory to the PATH variable. The problem lies in the Chinese characters in the default path for the Profile.ps1 file. The problem is solved by copying the faulty profile file to the correct destination, and specifying the encoding.

## 问题

Powershell 无法使用 conda 命令。具体表现：

PS C:\Users\UserName> conda activate base

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

$conda init <SHELL_NAME> Currently supported shells are: - bash - cmd.exe - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init'.  此时运行 conda init powershell，效果如下： PS C:\Users\UserName> conda init powershell no change C:\ProgramData\Anaconda3\Scripts\conda.exe no change C:\ProgramData\Anaconda3\Scripts\conda-env.exe no change C:\ProgramData\Anaconda3\Scripts\conda-script.py no change C:\ProgramData\Anaconda3\Scripts\conda-env-script.py no change C:\ProgramData\Anaconda3\condabin\conda.bat no change C:\ProgramData\Anaconda3\Library\bin\conda.bat no change C:\ProgramData\Anaconda3\condabin\_conda_activate.bat no change C:\ProgramData\Anaconda3\condabin\rename_tmp.bat no change C:\ProgramData\Anaconda3\condabin\conda_auto_activate.bat no change C:\ProgramData\Anaconda3\condabin\conda_hook.bat no change C:\ProgramData\Anaconda3\Scripts\activate.bat no change C:\ProgramData\Anaconda3\condabin\activate.bat no change C:\ProgramData\Anaconda3\condabin\deactivate.bat no change C:\ProgramData\Anaconda3\Scripts\activate no change C:\ProgramData\Anaconda3\Scripts\deactivate no change C:\ProgramData\Anaconda3\etc\profile.d\conda.sh no change C:\ProgramData\Anaconda3\etc\fish\conf.d\conda.fish no change C:\ProgramData\Anaconda3\shell\condabin\Conda.psm1 no change C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1 no change C:\ProgramData\Anaconda3\Lib\site-packages\xontrib\conda.xsh no change C:\ProgramData\Anaconda3\etc\profile.d\conda.csh no change D:\%一些乱码%\Documents\WindowsPowerShell\profile.ps1 No action taken.  因为 conda init 命令没有执行任何改动，所以此时再运行 conda activate 也没啥用处。 ## 分析 观察上面的输出结果，我们发现 Profile.ps1 文件的位置不太对劲。根据文档，profile 文件的位置有赖于系统变量中的设定。如果系统变量设定的路径中存在中文字符，估计可能会有 bug。 我为了备份文件，曾一度把我的文档设定到了一个含有中文字符的目录。事实上，在其他中文版操作系统中，我的文档也很容易包含中文字符。因此，默认的 Profile.ps1 文件位置就会产生乱码。我们看一下D盘根目录，果然有一个带乱码的文件夹。 PS C:\Users\UserName> ls D:\ 目录: D:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- ...省略... d----- 2022/1/27 14:00 %一些乱码% ...省略...  而目前的 Profile.ps1 文件就在里面。conda init 命令修改了这里面的文件后，Powershell 也并找不到它，于是永远都无法产生效果。 ## 解决方案 首先，找到 Profile 文件的正确位置。 PS C:\Users\UserName>$Profile
D:\%一些正常中文%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1


#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\ProgramData\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
chcp 936


## 其它

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned