Pythonをインストールして、いよいよプログラムを書いて、実行してみよう!と思っても、どうやって実行するのかわからない!
ということになっていませんか?
この記事では、
- Python(またはAnaconda、またはPyCharm)をインストールしたんだけど実行方法がわからない!
- コマンドプロンプトを起動して、って書いてあるけどどうやるの!?
- コマンドプロンプトを起動して、「python helloworld.py」と入力してもエラーメッセージが出るんだけど!
といったところでつまずいてしまったあなたに贈る、Pythonのプログラムの実行方法を説明する記事です。
いろいろなページでサラッと書いてあることは、そんな意味だったのか!と目からうろこが落ちる記事になっていますので、ゆっくりお読みください。
本記事を読む前に、Pythonがどんなプログラミング言語なのかをおさらいしておきたい人は次の記事を参考にしてください。
→ Pythonとは?特徴やできること、活用例をわかりやすく簡単に解説
なお、その他のPythonの記事についてはこちらにまとめています。
Pythonでせっかく書いたプログラムが実行できない!?
以下のようなhelloworld.pyを、「D:\Python\helloworld」フォルダに作成しましょう。
print("Hello World!")
次に、「python helloworld.py」と入力して実行しましょう。
Hello World!
と表示…されたらこの記事は読んでいませんよね。
この『「python helloworld.py」と入力して実行しましょう。』のたった1行で説明されている操作を、インストールした環境ごとに説明していきます。
Python環境別プログラムの実行方法
Python 3.6をインストールした場合
まずは、以下の記事を参考にPython 3.6をインストールした場合の、実行方法から説明しましょう。
ただし、インストールの途中で表示される「Add Python 3.6 to PATH」にチェックをつけましょう。これは、隠れたつまずきポイントです。
![](https://www.sejuku.net/blog/wp-content/uploads/2017/09/python_install-150x85.jpg)
ここでは、Windows 10にインストールした場合の操作を説明します。
まずは、以下の内容のhelloworld.pyを、DドライブのPythonフォルダの中のhelloworldフォルダに作成しましょう。
print("Hello World!")
「helloworld.py」をエクスプローラーで確認すると以下のようになります。
![python-execute01](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute01.png)
では、このhelloworld.pyをPython 3.6で実行しましょう。
(1)スタートボタンを右クリックして、「コマンドプロンプト」をクリックします。
![python-execute02](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute02.png)
(2)「D:」と入力し、Enterキーを押します。
![python-execute03](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute03.png)
(3)「cd D:\Python\helloworld」と入力し、Enterキーを押します。
![python-execute04](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute04.png)
(4)「python helloworld.py」と入力し、Enterキーを押します。
![python-execute05](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute05.png)
「Hello World!」と表示されましたか?
![python-execute06](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute06.png)
表示されていれば、これで作成したファイルが、Python 3.6で実行できたことになりますね!
もし、以下のようなメッセージが表示されたときは、次の点を確認します。
D:\Python\helloworld>python helloworld.py python: can't open file 'helloworld.py': [Errno 2] No such file or directory
- D:\Python\helloworldフォルダにhelloworld.pyが保存されていますか。
- 「helloworld.py」を間違えて「heloworld.py」(lが1つしかない)などになっていませんか。
- 「python helloworld.py」と入力するときに、カーソルより左側に「D:\Python\helloworld>」と表示されていますか。
Anacondaをインストールした場合
次は、以下の記事を参考にAnacondaをインストールした場合の実行方法です。
![](https://www.sejuku.net/blog/wp-content/uploads/2018/02/shutterstock_629004191_eye-150x86.jpg)
Anacondaの場合も、以下の内容のhelloworld.pyを、DドライブのPythonフォルダの中のhelloworldフォルダに作成しましょう。
print("Hello World!")
「helloworld.py」をエクスプローラーで確認すると以下のようになります。
![python-execute01](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute01.png)
では、このhelloworld.pyをAnacondaのbase (root)環境で実行しましょう。
(1)スタートボタンをクリックして、「Anaconda3 (64-bit)」→「Anaconda Navigator」の順にクリックします。
![python-execute07](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute07.png)
(2)「Environments」をクリック後、「base (root)」をクリックし、「」をクリックして、「Open Terminal」をクリックします。
![python-execute08](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute08.png)
(3)「D:」と入力し、Enterキーを押します。
![python-execute10](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute10.png)
(4)「cd D:\Python\helloworld」と入力し、Enterキーを押します。
![python-execute11](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute11.png)
(5)「python helloworld.py」と入力し、Enterキーを押します。
![python-execute12](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute12.png)
「Hello World!」と表示されましたか?
![python-execute13](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute13.png)
表示されていれば、これで作成したファイルが、Anacondaで実行できたことになりますね!
もし、以下のようなメッセージが表示されたときは、次の点を確認します。
(base) D:\Python\helloworld>python helloworld.py python: can't open file 'helloworld.py': [Errno 2] No such file or directory
- D:\Python\helloworldフォルダにhelloworld.pyが保存されていますか。
- 「helloworld.py」を間違えて「heloworld.py」(lが1つしかない)などになっていませんか。
- 「python helloworld.py」と入力するときに、カーソルより左側に「(base) D:\Python\helloworld>」と表示されていますか。
PyCharmをインストールした場合
最後に、以下の記事でPyCharmをインストールした場合の実行方法を説明して終わりにしましょう。
![](https://www.sejuku.net/blog/wp-content/uploads/2021/01/a8daf75edf6ce148af5756fe3bd84406.jpg)
PyCharmの場合は、先にプロジェクトを作成する必要があるので、ちょっと手間が増えますが辛抱強く行きましょう。
(1)スタートボタンをクリックし、「JetBrains」→「JetBrains PyCharm Community Edition 2018.1.3」の順にクリックします。
![python-execute14](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute14.png)
PyCharmが起動します。
(2)「ファイル」→「新規プロジェクト」の順にクリックします。
![python-execute15](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute15.png)
(3)「ロケーション」に「D:\Python\helloworld」と入力し、「作成」をクリックします。
![python-execute16](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute16.png)
※ここで以下の画面が表示された場合は、「いいえ」をクリックして、別のフォルダを指定してください。既存のフォルダを指定すると、Pythonの実行環境を作成できません。
![python-execute17](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute17.png)
(5)「helloworld D:\Python\helloworld」をクリックします。
![python-execute19](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute19.png)
(6)「ファイル」→「新規」の順にクリックします。
![python-execute20](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute20.png)
(7)「Pythonファイル」をクリックします。
![python-execute21](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute21.png)
(8)「名前」に「helloworld.py」と入力し、「OK」をクリックします。
![python-execute22](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute22.png)
(9)「print(“Hello World!”)」と入力します。
![python-execute23](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute23.png)
(10)「実行」→「デバッグ」の順にクリックします。
![python-execute25](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute25.png)
(11)「helloworld」をクリックします。
![python-execute26](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute26.png)
helloworld.pyが実行され、PyCharmの左下に「Hello World!」と表示されます。
![python-execute27](https://www.sejuku.net/blog/wp-content/uploads/2018/05/python-execute27.png)
これで、PyCharmで実行できたことになりますね!
まとめ
今回は、Pythonのインストールが終わった方に向けて、自分で書いたプログラム(helloworld.py)を実行する操作を説明しました。
『「python helloworld.py」と入力して実行しましょう。』のたった1行で説明されている操作が、実はこんなに大変だったのか!と目からうろこが落ちたのではないでしょうか。
ここまでくれば、あとは自分が好きなようにプログラムを書き、実行するだけです! その「実行するだけ」が大変ではありますが、何とかやっていけそう!という気がしてきたのではないでしょうか。
では!