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?

PowerShell環境でできるだけUTF-8で生活したい

Posted at

はじめに

最近macOSのシェルからWindowsに開発環境を移行しつつあります。これまで何気なく使っていたCLIのツールがいろいろと動かなかったりしているのですが、とりあえずPythonで困ったので書き残しておこうと思います。

現象

Python実行中に

UnicodeEncodeError: 'cp932' codec can't encode character

というエラーが出力されてしまいました。これをなんとかしたいのです。

PowerShellのプロファイルファイルに設定

ということで、以下の内容を$PROFILEファイルに書きます。

まず環境変数$PROFILEの値を調べます。"C:\Users\seiken\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"みたいなファイル名です。

このファイルに以下の内容を書きます。

# コンソールの入出力をUTF-8にする
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# パイプなどで渡すデータもUTF-8にする
$OutputEncoding = [System.Text.Encoding]::UTF8

# PythonをUTF-8モードで動作させる
$env:PYTHONUTF8 = 1

最後にPowerShellを再起動!

どうでしょう。まだ問題があったり、もっと設定した方が良いこともあるかと思います。

お・ま・け

へ~~、Select-Stringってのがあるんですか。環境変数はディレクトリみたいなもんあんですね。"ls env:"ってやるんですか。"cat env:path"ですか!

え。

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?