在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
相关推荐
史丹利复合田12 小时前
【无标题】vscode远程连接,服务器端配置
ide·vscode·编辑器
gusijin14 小时前
解决idea启动报错java: OutOfMemoryError: insufficient memory
java·ide·intellij-idea
吨~吨~吨~14 小时前
解决 IntelliJ IDEA 运行时“命令行过长”问题:使用 JAR
java·ide·intellij-idea
HAPPY酷14 小时前
为啥双击 .sln 文件即可在 Visual Studio 中加载整个解决方案
ide·visual studio
summer_du14 小时前
IDEA插件下载缓慢,如何解决?
java·ide·intellij-idea
iwanghang15 小时前
Android Studio 2023.2.1 新建项目 不能选择Java 解决方法
android·ide·android studio
Monly2117 小时前
Java:Linux部署java程序
java·linux·pycharm
爱喝可乐的老王18 小时前
Jupyter Notebook 的内核切换
linux·ide·jupyter
玄同76518 小时前
TRAE Rules 高效使用指南:让 AI 更听话的秘密
ide·人工智能·ai编程·claude·cursor·trae
fenglllle19 小时前
macOS安装python并在PyCharm下启用自由线程
ide·python·pycharm