WindowsでPython

69
WindowsPython みんなのPython勉強会#22 2017/03/08 driller@patraqushe

Transcript of WindowsでPython

Page 1: WindowsでPython

WindowsでPythonみんなのPython勉強会#22

2017/03/08driller@patraqushe

Page 2: WindowsでPython

自己紹介

○ driller / どりらん @patraqushe○ - 2010: コンピュータの会社に勤務○ 2010 -: 個人トレーダー○ 2015 -: Pythonをはじめる

Page 3: WindowsでPython

fin-pyの紹介

Python + 金融のコミュニティです

毎月もくもく会をやってます

金融について詳しくない方でも興味が

あるかた大歓迎です

次回は3/26(日)11:00- です

Page 4: WindowsでPython

お申込みは今すぐ!

https://connpass.com/event/52667

Page 5: WindowsでPython

注意事項(重要)

○ 複数の環境構築方法ついて説明しますが、優劣をつける意図は全くありません

○ #stapy のハッシュタグで、特定のOSやパッケージ等についてネガティブなツイートをしようようにしてください

Page 6: WindowsでPython

宗派が違ってもお互いを尊重して仲良くしましょう

ケンカはダメ。ゼッタイ。

Page 7: WindowsでPython

Agenda

1. Windows + Pythonの状況2. 環境構築について3. 仮想環境について4. おまけ

Page 8: WindowsでPython

Windowsを取り巻く昨今の状況

おもいでぽろぽろ

Page 9: WindowsでPython

一昔前のWindows+Python環境

○ Pythonのイベントに行けば、みんな当たり前のようにMacBook

○ Linuxでしか動かないパッケージ○ Linuxの情報しかない

Page 10: WindowsでPython

Windowsユーザが増えてきた?

○ IT以外の職種● 科学技術● 統計、マーケティング● 金融

○ 機械学習ブーム○ Pythonブーム

Page 11: WindowsでPython

Windowsを使う理由

○ 以前からWindowsユーザ○ 職場がWindows

● 非IT系が多い

○ H/Wの選択肢を広げる● ( ゚∀゚)o彡° Ryzen!Ryzen!

○ Windowsソフト、win32api

Page 12: WindowsでPython

Windowsが避けられていた理由

なぜITエンジニアからは敬遠されるのか

Page 13: WindowsでPython

文字コード

○ CP932○ 1982年の規格

● 35年前!

○ 文字化けとの長い戦い・・・

Page 14: WindowsでPython

コンソール(ターミナル)環境

○ コマンドプロンプトが主流だった○ コンソール上でエディタが使いづらい○ スクリプト処理でできることが限られている○ MS-DOS時代(1981年)から殆ど進化していない

Page 15: WindowsでPython

Pythonパッケージの対応

○ Windowsが後回しにされるケースが多い○ Windows用にビルドされたwheelがない○ ビルドが大変

● scipy, lxml, mecab, opencv

Page 16: WindowsでPython

Windows環境の改善

やればできる子?

Page 17: WindowsでPython

Python3.6でのWindows対応

○ PEP529: ファイルシステムエンコーディングがUTF-8に○ PEP528: コンソールエンコーディングがUTF-8に○ ロングパス対応

● 260文字制限がなくなった

Page 18: WindowsでPython

コンソールエンコーディングを試す

下記のコマンドを実行してみましょう

python -c "print('€')"

もしくはpythonを実行して

>>> print('€')

Page 19: WindowsでPython

PowerShell

○ 130個以上の標準的なコマンドラインツール○ 管理タスクに重点を置いた新しいスクリプト言語○ 一貫性のある構文とユーティリティ○ エイリアスによるUNIXコマンドとの共通化

Page 20: WindowsでPython

コンソール

サードパーティのソフトの機能がそれなりに充実

○ ConEmu○ Cmder○ etc

Page 21: WindowsでPython

仮想環境

○ virtualenvwrapper-winはコマンドプロンプトしか使えなかった● Python2ではvirtualenvwrapper-powershellをインストールすると

PowerShellが使える● virtualenvwrapper-powershellは少しめんどくさい

○ venvでPowerShellが使えるように● Pythonの標準として使える

Page 22: WindowsでPython

ビルドが必要なパッケージ

○ Visual C++ 2015 Build Tools● Visual Studioのインストールが不要に

○ 非公式バイナリ● http://www.lfd.uci.edu/~gohlke/pythonlibs/

○ ANACONDA

Page 23: WindowsでPython

Docker for Windows

○ VirtualBox、Gitのインストールが不要に● インストールが楽になった

○ PowerShell対応

Page 24: WindowsでPython

Bash on Ubuntu on Windows

○ WindowsでBash(Ubuntu)が使えるように○ Pythonも動作○ オーバヘッドなしにLinuxのバイナリが動作○ ベータ版で発展途上だが着々と改善している

Page 25: WindowsでPython

Windowsでも

Pythonが使えるようになってきた

Page 26: WindowsでPython

環境構築の重要性

人はなぜ環境構築の話になると熱くなるのか?

Page 27: WindowsでPython

本当にあった怖い話

○ 会社員時代に仕様書に従って三日三晩かけてシステムを構築

○ 32bitのOSじゃないとアプリケーションが動かないことが判明

○ OSから全部入れ直し・・・

Page 28: WindowsでPython

環境構築を甘く見ていると・・・

○ プラットフォームの選択を誤ると、そもそもやりたかったことができなくなる

○ 壊れたときにやり直しが大変になる○ 変更をした影響で既存のプログラムが動かなくなる

Page 29: WindowsでPython

WindowsにおけるPythonの選択肢

意外と色々ある

Page 30: WindowsでPython

WindowsにおけるPythonの選択肢

○ 公式のPython○ ディストリビューション

● 公式● ANACONDA● Intel® Distribution for Python*● etc

Page 31: WindowsでPython

その他の選択肢

○ VM● VirutualBox, VMWare

○ Docker● Docker for Windows● Doker Toolbox

○ Cygwin, MSYS, MSYS2○ Bash on Ubuntu on Windows

Page 32: WindowsでPython

公式配布のPython

○ python.orgから配布されているPython○ 公式なので色々と安心

● 情報が充実● 事例が豊富

○ 最新版のリリースが一番早い○ vanilla Pythonとも呼ばれるらしい

Page 33: WindowsでPython

32bit or 64bit

○ 32bitにする理由● OSが32bit● win32APIを呼び出す● C拡張を32bitでビルドして配布する

○ 上記以外の理由がなければ64bitでよさそう○ 両方入れるという選択肢も

Page 34: WindowsでPython

ANACONDA

○ Continuum Analytics社が提供しているPythonのディストリビューション

○ データ分析等によく使われる多数のパッケージがプリインストール

○ パッケージ管理が独自仕様○ 最小構成で入れる場合はMinicondaを選択

Page 35: WindowsでPython

公式配布のPython or ANACONDA

○ きのこたけのこ戦争並の論争に○ それぞれに利点と欠点があるので、それを踏まえた選択

を○ 両方入れるという選択肢も

Page 36: WindowsでPython

ANACONDA

利点

○ 必要そうなパッケージ

が大抵入ってる

○ すぐにはじめられる

○ パッケージがビルド済

欠点

○ パッケージが独自仕様

○ 最新版とのラグ

○ 問題が会った場合のト

ラブルシュートが困難

Page 37: WindowsでPython

Intel® Distribution for Python*

○ 様々な点で高速化されている● MKL: Numpy, Scipy, scikit-learn● Threading Building Blocksライブラリ● JITコンパイル(Numba)● CythonとMPIライブラリを使用したmpi4pyの高速化● pyDAALを介したデータ解析アクセラレーションライブラリ

○ pip, condaを使用したパッケージ管理が可能○ 最新のPythonがビルドされるまでのラグがある

● 2017年02月時点ではPython3.5

Page 38: WindowsでPython

その他のディストリビューション

○ Enthought Canopy● scipy.orgのスポンサー

○ Python(x,y)● 2系のみ、開発終了?

○ WinPython● ポータブル● バージョンが古い

Page 39: WindowsでPython

複数のPythonを入れる場合のTips

○ インストール時にPathを設定しないようにする● システムで使いたいPythonだけ設定する

○ Pathを切り替える方法を用意する● コマンドプロンプト

■ バッチファイル■ doskey

● PowerShell■ スクリプト■ Set-Alias

Page 40: WindowsでPython

PATHの通し方(コマンドプロンプト)

例: "C:\Python36"にpython.exeがある場合

○ 既存のPATHを含める

PATH=C:\Python36;C:\Python36\Scripts

○ 既存のPATHを含めない

PATH=C:\Python36;C:\Python36\Scripts;%PATH%

Page 41: WindowsでPython

PATHの通し方(PowerShell)

例: "C:\Python36"にpython.exeがある場合

○ 既存のPATHを含める$Env:Path = "C:\Python36;C:\Python36\Scripts;" + $Env:Path

○ 既存のPATHを含めない$Env:Path = "C:\Python36;C:\Python36\Scripts"

Page 42: WindowsでPython

環境を切り替えるコマンドを登録

Windows PowerShell プロファイルを作成して編集

New-item –type file –force $profile notepad $profile

Set-AliasでPATHを通すコマンドを登録しておく

Page 43: WindowsでPython

Demo

コマンド一発でPython環境を切り替え

○ Python3.6(64bit)○ Python3.6(32bit)○ Python2.7(32bit)○ ANACONDA Python3.6(64bit)○ ANACONDA Python3.6(32bit)○ Intel® Distribution for Python*

Page 44: WindowsでPython

Bash on Ubuntu on Windows

○ Windows Subsystem for Linux(WSL)上で動作するUbuntu

○ 14.04LTS→Build 14943以降から16.04LTSに○ PythonはUbuntuと同じ方法でインストール

● apt-get● ソースからビルド● pyenv

○ 大抵のパッケージは動作● GUI系は少し工夫が必要

Page 45: WindowsでPython

仮想マシン、コンテナ

Linuxの環境と合わせることができる

○ 仮想マシン● VMWare● VirtualBox● Vagrant

○ コンテナ● Docker for Windows● Docker Toolbox

Page 46: WindowsでPython

なぜわざわざLinux環境にするのか

○ 殆どのパッケージが動作○ 豊富な事例、ノウハウ○ シェルスクリプト○ 文字コード○ 高い互換性

● manylinux wheel

Page 47: WindowsでPython

Demo

○ 数多の挑戦者を退けてきたmecabを試す● Bash on Ubuntu on Windows● Docker for Windows

Page 48: WindowsでPython

自分に合ったものを入れよう

Page 49: WindowsでPython

仮想環境のすゝめ

最強のリスクヘッジ

Page 50: WindowsでPython

なぜ仮想環境を使うのか?

○ スクラップアンドビルドが容易○ 変更が全体に影響を及ぼさない○ 複数のバージョンを扱える○ 必要最小限の環境をつくれる

Page 51: WindowsでPython

スクラップアンドビルド

○ 変更を加えるとおかしくなることがある● パッケージ、拡張機能のインストール● 環境変数の変更

○ 場合によっては再インストールが必要に○ 仮想環境にしておけば、削除して作り直しが簡単にできる

Page 52: WindowsでPython

変更に対する影響

○ PATHの上書き○ アップデートしたパッケージの変更○ Python自体のバージョン変更の影響

Page 53: WindowsでPython

複数のバージョン

○ Pythonのバージョンを複数に○ パッケージのバージョンを複数に○ PATHを自動で設定できる

Page 54: WindowsでPython

最小環境

○ 単体でテストができる● 依存パッケージの確認● パッケージの配布

○ 問題の切り分けが容易

Page 55: WindowsでPython

Windows + Pythonの仮想環境

virtualenv venv conda env

Page 56: WindowsでPython

virtualenv

○ Python2の仮想環境の主流○ virtualenvwrapperと組み合わせることで利便性が向上○ Windowsではコマンドプロンプトのみ対応

● PowerShellは一部対応

Page 57: WindowsでPython

venv

○ Python3.3から標準ライブラリとして使用可能○ PowerShellに対応○ 任意のディレクトリに仮想環境を作成できる

Page 58: WindowsでPython

venv超入門

○ 仮想環境の作成<python-path> -m venv <envname>

○ 仮想環境に切り替え<envdir>\Scripts\Activate.ps1コマンドプロンプトの場合はActivate.bat

○ 仮想環境から抜けるdeactivate

Page 59: WindowsでPython

ANACONDAの仮想環境(conda env)

○ ANACONDA独自の仮想環境○ 複数バージョンのPythonを構築できる○ PowerShellで使う場合はPSCondaEnvsのActivateスクリ

プトを使用する● https://github.com/Liquidmantis/PSCondaEnvs

Page 60: WindowsでPython

conda env超入門

○ 仮想環境の作成conda create -n <envname> python=<python-version>

○ 仮想環境に切り替えactivate <envname>

○ 仮想環境から抜けるdeactivate.ps1コマンドプロンプトの場合はdeactivate.bat

Page 61: WindowsでPython

Demo

○ venvによる仮想環境の切り替え○ conda envによる仮想環境の切り替え

Page 62: WindowsでPython

仮想環境からはじめよう

Page 63: WindowsでPython

おまけ

ベンチマーク

Page 64: WindowsでPython

動作環境(プラットフォーム)Host OS Guest OS Python

Windows-10-10.0.15025-SP0(build15025.1000)

Python3.6.0 64bit

Python3.6.0 32bit

ANACONDA Python3.6.0 64bit

ANACONDA Python3.6.0 32bit

Intel® Distribution for Python*3.5(Version 2017 (Update 2))

Linux-4.4.0-43-Microsoft-x86_64-with-Ubuntu-16.04-xenial

Python3.6.0 (BoW)

Linux-4.9.8-moby-x86_64-with-debian-8.7

Python3.6.0 (Docker for Windows 10072)

ArchLinux Python3.6.0

Page 65: WindowsでPython

プログラム

○ コードはPure Python○ 与えられた自然数(今回は100,000,000)から

素数を探索するアルゴリズム● https://github.com/famzah/langs-performance

○ 計測はBenchmarkerを使用● 5回施行し、最大値と最小値を除く3回の実行時間の平

均値

Page 66: WindowsでPython

ベンチマーク結果

※ Arch Linuxは同一のH/Wで実施(参考値)

Page 67: WindowsでPython

ベンチマークについて

○ 懸念されていたBoWのオーバヘッドはなかった● むしろ他より速かった

○ 個別パッケージのベンチマークをしたほうがよさそう● Numpy, scipy, 機械学習系, 画像処理系

○ パッケージのビルド方法によって差が出る可能性も

Page 68: WindowsでPython

速度が気になる人は測ってみよう

Page 69: WindowsでPython

まとめ

○ WindowsでもPythonできる○ 自分に合った環境構築を○ 仮想環境を活用しよう○ 速度が気になる人は比べてみよう