Try   HackMD

プログラミング学習の最初の鬼門

大学3年生の頃、自分の周りはプログラミングブームでした。みんなそろいもそろってMacBookを開き、Progateをやっていたことを覚えています。

しかし、1年後までプログラミングの勉強を続けている人は自分ひとりでした。もちろん継続しなかったのには様々な理由があるとは思いますが、プログラミング学習に挫折してしまう大きな理由のひとつに環境構築ができないというのがあると思います。せっかくProgateなどで勉強をしても、自分のパソコンでプログラムを動かせなかったらどうしてもモチベーションが下がってしまうことでしょう。

なので今回はPythonの環境構築をする方法をできるだけ詳しく解説しようと思います。

※OSはWindows 10を想定しています。Mac, Linuxでも同様に環境構築ができると思います。

Anacondaのダウンロード

Anacondaとは、Python本体とPythonの機能を拡張するファイルなどをまとめたものです。このようなものをディストリビューションと言ったりします。なのでAnacondaとはPythonのディストリビューションであると言うことができます。

【注意】Pythonをインストールしたことがある方はコントロールパネルからアンインストールをしておいた方がいいかもしれません。思わぬエラーにつながる可能性があります。

Anacondaのインストールはこちらからできます。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Macを使っている方は上記の画面上部のmacOSというところをクリックするとmac版のAnacondaをダウンロードすることができます。

ここで、左側のPython 3.7 versionをダウンロードするのですが、「64-bit Graphical Installer」と「32-bit Graphical Installer」の2種類があると思います。これはWindows10の環境によってどちらをダウンロードすべきかが変わってきます。ただ、よっぽどのことがない限り64ビット版のWindows10を使っているはずなので、「64-bit Graphical Installer」をダウンロードして問題ないと思います。

もし自分のWindows10が64ビット版なのか32ビット版なのか確認したいという場合にはこちらの記事などを参考にするといいでしょう。

ダウンロードが完了したら、「ダウンロード」フォルダにAnaconda3-2019.10-Windows-x86_64.exeというファイルがあるはずなので、ダブルクリックしてインストールを開始します。また、ブラウザの下部のメニューをダブルクリックして直接実行しても構いません。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

環境によってはウィルス対策ソフトによって実行が阻まれるかもしれませんが、安全なプログラムなので手動で実行を許可してください。

Anacondaのインストール

Anaconda3-2019.10-Windows-x86_64.exeを実行すると、このようなウィンドウが出てきたはずです。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ここからAnacondaのインストールを始めていきます。「Next >」を押しましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Anacondaについてのライセンスが書かれています。「I Agree」を押しましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ここは理由のない限り「Just Me」を選択して、「Next >」を押しましょう。「All Users」を選択すると、PC内のユーザーがAnaconda及びPythonを使用できるようになります。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Anacondaのインストール先を指定します。デフォルトで入力された値のまま「Next >」を押しましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

オプションの選択画面です。両方にチェックをつけて「Install」を押しましょう。「Add Anaconda to my PATH environment variable」を選択すると、赤字で「Not recommended.(非推奨)」と表示されるのですが、問題ありません。パスの設定の仕方をすでにご存知の方はこちらは選択しないで、インストール後に手動でパスの設定をした方が無難です。パスの設定の仕方がわからない方や、そもそもパスとはなんぞやと思っている方はここにチェックを入れても問題ないはずなので、非推奨ですがチェックを入れてしまいましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

インストールが開始されます。インストールが完了するまで少し待ちましょう。内蔵ストレージがSSDであればものの数分で終わりますが、HDDだと数十分かかることもあります。PCのスペックでかなり変動するので気長に待ちましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

「Completed(完了)」と表示され、インストールが完了しました。「Next >」を押しましょう。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ほかのソフトの紹介なので無視して「Next >」を押します。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

デフォルトではチェックがついているのですが、外します。英語が得意で得意で仕方がなくてAnacondaの使い方を英語で学びたいという方はチェックをつけたままで結構です。「Finish」を押すとウィンドウが閉じて一通り作業は終わりです。

ここまで来たらAnaconda3-2019.10-Windows-x86_64.exeというファイルは削除してしまって大丈夫です。

最後にPythonがしっかり動くかどうか確認します。

Pythonの動作確認

コマンド プロンプト」を開きましょう。(macの方はターミナルを開いてください。)

「コマンド プロンプト」とは、よくプログラマが使っているような黒い画面のことです。開くとこのような画面が出てきます。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

「こんなソフト入ってないよ」と思われる方もいるかもしれませんが、このソフトは知らないだけで必ずWindowsに最初から入っています。

スタートメニューを開き、アプリ一覧から「Windows システム ツール」を探します。「Windows システム ツール」をクリックし展開すると、「コマンド プロンプト」が見つかるはずです。

今後プログラミングをするには頻繁に使うことになるので、デスクトップにショートカットを作るなどしておきましょう。ちなみにデスクトップにドラッグアンドドロップするだけでショートカットの作成が可能です。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

コマンド プロンプトを開きます。そして「python」と打ってEnterを押してください。

このような画面になれば成功です。Pythonの実行モードになりました。Pythonのプログラムが実行可能です。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

試しにPythonのプログラムを実行してみましょう。

「print("Hello World")」と打ち込んでEnterを押してください。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Hello Worldと出力されれば成功です。もしエラーなどが出たら変なスペースは入れてないか、スペルミスはしていないかなど確かめてみてください。

最後に「exit()」と打ち、Enterを押してください。Pythonから抜けることができます。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

お疲れ様でした。これでPythonがきちんと動いてることが確認できました。コマンドプロンプトは閉じてしまって構いません。

これでPythonが自分のPC上で動かせるようになったわけですが、この黒い画面でプログラムを書いていくのはあまりに不便すぎます。次回はもっと便利なPythonプログラムの実行方法、プログラミングを快適にするツールなどの説明をしていきたいと思います。