LoginSignup
1
4

More than 3 years have passed since last update.

【Python3】開発環境構築《Windows編》

Last updated at Posted at 2019-11-02

はじめに

Windows で Python を使用する際の、開発環境構築についてまとめました。

システム構成

  • Windows10 Pro 64bit
  • Windows PowerShell
  • Python 3.8.1
  • pip 20.0.2

仮想環境(venv)

仮想環境を利用することで、仮想環境毎にPythonパッケージ群を管理することができる。

※Python3.4まではpyvenvが推奨されていたが、Python3.8現在ではvenvが推奨されている。
(pyvenvはPython3.6から非推奨)

作成

PowerShell
# python -m venv 作成する環境名
python -m venv venv
  • プロジェクトルート(アプリケーションルート)で実行する

有効化

PowerShell
.\venv\Scripts\activate
PSSecurityException が発生した場合

powershell.png
PowerShellのデフォルトの実行ポリシーでは、外部ファイルのスクリプト実行が制限されているため、実行ポリシーの変更が必要。

PowerShell
# -Scope Process : 現在のプロセスの実行ポリシーのみ適用
Set-ExecutionPolicy -Scope Process RemoteSigned

無効化

PowerShell
deactivate

パッケージ管理(pip)

pip(インストーラ・プログラム)を使用し、PyPI(The Python Package Index)のパッケージを管理する。

インストール

PowerShell
# pip install パッケージ名
pip install black

# pip install パッケージ名==バージョン
pip install black==19.3b0

アンインストール

PowerShell
# pip uninstall パッケージ名
pip uninstall black

パッケージ一覧

PowerShell
# インストール済パッケージ一覧の表示
pip list

# インストール済パッケージ一覧の出力
pip freeze > requirements.txt

# パッケージ一覧からの一括インストール
pip install -r requirements.txt
PowerShell でファイル出力する際の注意点

PowerShell でリダイレクト(>)を使用した場合、出力されるファイルの文字コードは「UTF-16 LE」になる。
「UTF-8」でファイル出力するためには、下記コマンドレットを使用する必要がある。

PowerShell
pip freeze | Out-File -Encoding utf8 requirements.txt
1
4
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
1
4