问题简述
在 Win 11 系统自带的终端(Powershell)和CMD窗口中,可以正常调用conda,命令行回显:
(base) PS C:\Users\DELL>
然而,在 VSCode 和 Trae 上无法正确使用 conda 报错为:
PS C:\Users\DELL> conda activate base
Invoke-Expression: Missing '{' in configuration statement.
版本号
在 CMD 窗口中,$PSVersionTable
命令无效。
在 Windows Powershell 窗口中,$PSVersionTable
命令回显:
(base) PS C:\Users\DELL> $PSVersionTableName Value
---- -----
PSVersion 5.1.26100.6899
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.26100.6899
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
在VSCode的Terminal窗口中,$PSVersionTable
命令回显:
PS C:\Users\DELL> $PSVersionTableName Value
---- -----
PSVersion 7.5.3
PSEdition Core
GitCommitId 7.5.3
OS Microsoft Windows 10.0.26100
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
尝试1:使用 conda init powershell
在 Windows Powershell 中执行后回显:
(base) PS C:\Users\DELL> conda init
no change D:\software\miniconda\Scripts\conda.exe
no change D:\software\miniconda\Scripts\conda-env.exe
no change D:\software\miniconda\Scripts\conda-script.py
no change D:\software\miniconda\Scripts\conda-env-script.py
no change D:\software\miniconda\condabin\conda.bat
no change D:\software\miniconda\Library\bin\conda.bat
no change D:\software\miniconda\condabin\_conda_activate.bat
no change D:\software\miniconda\condabin\rename_tmp.bat
no change D:\software\miniconda\condabin\conda_auto_activate.bat
no change D:\software\miniconda\condabin\conda_hook.bat
no change D:\software\miniconda\Scripts\activate.bat
no change D:\software\miniconda\condabin\activate.bat
no change D:\software\miniconda\condabin\deactivate.bat
no change D:\software\miniconda\Scripts\activate
no change D:\software\miniconda\Scripts\deactivate
no change D:\software\miniconda\etc\profile.d\conda.sh
no change D:\software\miniconda\etc\fish\conf.d\conda.fish
no change D:\software\miniconda\shell\condabin\Conda.psm1
no change D:\software\miniconda\shell\condabin\conda-hook.ps1
no change D:\software\miniconda\Lib\site-packages\xontrib\conda.xsh
no change D:\software\miniconda\etc\profile.d\conda.csh
no change C:\Users\DELL\Documents\PowerShell\profile.ps1
no change C:\Users\DELL\Documents\WindowsPowerShell\profile.ps1
no change HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
No action taken.
尝试在 VSCode 中执行相同的命令,回显:
PS C:\Users\DELL> conda init powershell
usage: conda-script.py [-h] [-V] command ...conda is a tool for managing and deploying applications, environments and packages.Options:positional arguments:commandclean Remove unused packages and caches.compare Compare packages between conda environments.config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (C:\Users\DELL\.condarc) by default. Use the --show-sources flag to display all identified configuration locations on your computer.create Create a new conda environment from a list of specified packages.info Display information about current conda install.init Initialize conda for shell interaction.install Installs a list of packages into a specified conda environment.list List installed packages in a conda environment.package Low-level conda package utility. (EXPERIMENTAL)remove (uninstall)Remove a list of packages from a specified conda environment. Use `--all` flag to remove all packages and the environment itself.rename Renames an existing environment.run Run an executable in a conda environment.search Search for packages and display associated information.The input is a MatchSpec, a query language for conda packages. See examples below.update (upgrade) Updates conda packages to the latest compatible version.notices Retrieves latest channel notifications.optional arguments:-h, --help Show this help message and exit.-V, --version Show the conda version number and exit.conda commands available from other packages:doctor - A subcommand that displays environment health report
实际上并没有被调用起来。
解决
Conda在PowerShell 7.5版本中会失效。可以通过升级 conda 软件来处理。
https://zhuanlan.zhihu.com/p/22881718447
https://github.com/conda/conda/issues/14292#issuecomment-2619710240
https://github.com/PowerShell/PowerShell/issues/24869#issuecomment-2625708821
- 首先切换到 base 环境
conda activate base
- 升级 python 版本
conda update python
(按需指定版本号) - 升级 conda 版本
conda update conda
(按需指定版本号)
即解决。