0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows11で環境変数を変更する方法。

Posted at

はじめに

PowerShellを開いたときに

Invoke-Expression : At line:2 char:22
+ $Env:CONDA_PREFIX = "C:\Users\user\anaconda3"
+                      ~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\Users\user\anaconda3"
$Env:CONDA_SHLVL = "1"
$Env:CONDA_DEFAULT_ENV = "base"
$Env:CONDA_PROMPT_MODIFIER = "' in expression or statement.
At line:10 char:69
+ . "C:\Users\user\anaconda3\etc\conda\activate.d\openssl_activate.ps1"
+                                                                     ~
The string is missing the terminator: ".
At C:\Users\user\anaconda3\shell\condabin\Conda.psm1:77 char:9
+         Invoke-Expression -Command $activateCommand;
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand

というエラーが発生。どうも環境変数の設定に文法上おかしいところがあったらしい。condaを再インストールしても意味がなかった。
#対処法
C:\Users\user\anaconda3\shell\condabin\Conda.psm1を適当なエディタファイルで開く。
関数Enter-CondaEnvironmentを下記のように改良。

function Enter-CondaEnvironment {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false)][switch]$Stack,
        [Parameter(Position=0)][string]$Name
    );

    begin {
        If ($Stack) {
            $activateCommand = (& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA shell.powershell activate --stack $Name | Out-String);
        } Else {
            $activateCommand = (& $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA shell.powershell activate $Name | Out-String);
        }

        # デバッグ用: $activateCommand の内容を出力
        Write-Host "activateCommand: $activateCommand";
        Write-Verbose "[conda shell.powershell activate $Name]`n$activateCommand";
        Invoke-Expression -Command $activateCommand;
    }

    process {}

    end {}
}

Write-Host "activateCommand: $activateCommand";が追加した部分。
保存してPowerShellを開くと、環境変数の内容を吐いてくれる。
間違っている部分を修正して、PowerShellを管理者権限で開く。

PowerShellで永続的に環境変数を設定する方法

PowerShellを管理者として実行:

「スタートメニュー」→「PowerShell」と検索し、右クリックして「管理者として実行」を選ぶ。
システム環境変数を設定するコマンド: PowerShellで以下のコマンドを実行して、PATH環境変数に新しいパスを追加または変更する。

$ [System.Environment]::SetEnvironmentVariable("PATH", "変更したいパス", [System.EnvironmentVariableTarget]::Machine)

上記コマンドでは、SetEnvironmentVariableを使用して、システム全体に適用される環境変数PATHを設定します。[System.EnvironmentVariableTarget]::Machineを指定することで、システム全体の環境変数に変更を加えます。

ユーザー環境変数として設定する場合: ユーザー個別の環境変数を変更したい場合は、以下のコマンドを使用します。

$ [System.Environment]::SetEnvironmentVariable("PATH", "変更したいパス", [System.EnvironmentVariableTarget]::User)

この方法で、PowerShellを使って永続的に環境変数を変更することができる。システム全体に影響を与える場合はMachineを、ユーザーごとの設定にしたい場合はUserを指定する。
PowerShellを再起動すると、エラーが解決していた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?