在pycharm中使用powershell7解决conda,ohmyposh报错的问题

在pycharm中使用powershell7解决conda,ohmyposh报错的问题

pycharm会默认使用系统的powershell5,在terminal中就会报错,尤其是安装了ohmyposh, conda 后

验证powershell使用版本方法:

在 PyCharm 终端中运行:

shell 复制代码
powershell$PSVersionTable
shell 复制代码
你可能会看到类似:

PSVersion    5.1.xxxxx    # Windows PowerShell


而在你的系统 PowerShell 中运行同样命令,可能看到:

PSVersion    7.x.x        # PowerShell Core

pycharm中配置路径:

单独配置一个 pycharm-terminal.ps1 给pycharm用

settings-> terminal-> shell path中配置如下:

注意是 pwsh.exe 而不是 powershell.exe

shell 复制代码
pwsh.exe -NoExit -File "C:\Users\Administrator\Documents\pycharm-terminal.ps1"

pycharm-terminal.ps1内容:

shell 复制代码
# 禁用 Conda 插件
$env:CONDA_NO_PLUGINS = "true"

# 导入 PSReadLine
Import-Module PSReadLine -ErrorAction SilentlyContinue

# 根据 PowerShell 版本选择正确的 Oh My Posh 初始化方式
if ($PSVersionTable.PSVersion.Major -ge 7) {
    # PowerShell 7+
    oh-my-posh init pwsh --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression
} else {
    # Windows PowerShell 5.x
    oh-my-posh init powershell --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression
}

# Conda 初始化
If (Test-Path "D:\ProgramData\miniconda3\Scripts\conda.exe") {
    (& "D:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}

可以通过

shell 复制代码
notepad $PROFILE

读取默认的powershell_profile配置:

shell 复制代码
# 确保 PSReadLine 模块被导入
Import-Module PSReadLine

# 使用明确的路径初始化 Oh My Posh,避免环境变量问题
oh-my-posh init pwsh --config "C:\Users\Administrator\AppData\Local\Packages\ohmyposh.cli_96v55e8n804z4\LocalCache\Local\oh-my-posh\themes\aliens.omp.json" | Invoke-Expression

# Conda 初始化
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "D:\ProgramData\miniconda3\Scripts\conda.exe") {
    (& "D:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion
相关推荐
开源技术21 小时前
Python GeoPandas基础知识:地图、投影和空间连接
开发语言·ide·python
寻梦csdn1 天前
pycharm+miniconda兼容问题
ide·python·pycharm·conda
电饭叔2 天前
Jupyter学习中的问题--FileNotFoundError
ide·学习·jupyter
noBt2 天前
Windows IDEA 卡顿严重
java·ide·intellij-idea
小疙瘩2 天前
去掉 IDEA 中 mybatis配置文件的局部背景颜色(图解)
java·ide·intellij-idea
zhanglianzhao2 天前
Win 11 WSL 配置Claude code 并在VsCode中使用
ide·vscode·编辑器·claude·cladue code
hampeter2 天前
【填坑指南】Trae/VS Code 远程连接 Ubuntu,终端总是自动激活特定的 Conda 环境?三招教你彻底解决!
linux·ubuntu·conda·trae
Keying,,,,2 天前
VScode中终端闪退问题
ide·vscode·编辑器
热爱生活的五柒2 天前
如何在vscode中使用Claude code以及插件中配置setting.json无效的解决方法
ide·vscode·编辑器
403240732 天前
【Jetson开发避坑】虚拟环境(Conda/Venv)调用系统底层OpenCV与TensorRT的终极指南
人工智能·opencv·conda