Pythonプログラミング入門(1)Pythonのインストール
2019/10/17
Pythonのインストール
執筆時点でのPythonの最新バージョンは3.8.0
ですので、このバージョンのPythonをインストールします。
https://www.python.org/downloads/にアクセスします。
「Python3.8.0」をクリックします。
Python 3.8.0のページに移動します。
「Windows x86-64 executable installer」をクリックします。
バーが表示されます。
「保存」をクリックします。
ダウンロードが開始します。
ダウンロードが完了したら、ダウンロードしたフォルダのpython-3.8.0-amd64.exe
を開きます。
「Python 3.8.0 (64-bit) Setup」というウィンドウが開きます。
「Add Python 3.8 to PATH」をチェックします。
「Install Now」をクリックします。
インストールが開始します。
インストールが完了したら、「Close」をクリックします。
Pythonの起動
Pythonのインストールが成功したことを確認するため、Pythonを起動してみましょう。
コマンドプロンプトを起動し、python --version
と入力し、Enterキーを押します。
下のようにバージョン番号が表示されたら正常にインストールされています。
C:\work>python --version
Python 3.8.0
python --version
コマンドでPythonを起動した場合にはバージョン番号を出力したらPythonはすぐ終了してしまいます。
対話的にPythonのプログラムを実行するには単にpython
とだけ入力し、Enterキーを押します。
C:\work>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
この状態で何らかのプログラムを入力し、Enterキーを押せばそのプログラムが実行されることになります。
また、ソースコードとしてファイルに保存されたPythonのプログラムを実行するにはpython
の後に空白とファイルのパスを入力し、Enterキーを押します。
たとえば、test.py
というファイルに保存されているPythonのプログラムを実行するにはpython test.py
と入力し、Enterキーを押します。
C:\work>python>python test.py
なお、コマンドライン引数を指定する場合にはファイルのパスの後に空白を入力し、コマンドライン引数を続けます。
たとえば、1つ目のコマンドライン引数をfoo
、2つ目のコマンドライン引数をbar
としてtest.py
というファイルに保存されているPythonのプログラムを実行するにはpython test.py foo bar
と入力し、Enterキーを押します。
C:\work>python>python test.py foo bar
pipの実行
PyhtonをインストールするとPythonのパッケージマネージャであるpipも同時にインストールされます。
C:\work>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user
configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times
(corresponding to WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5
times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe,
(b)ackup, (a)bort.
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any
HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private
key and the certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip
is available for download. Implied with --no-index.
--no-color Suppress colored output
たとえば、仮想環境を作成するツールであるvirtualenvwrapper-winをインストールするにはpip install
コマンドを使用して下のようにします。
C:\work>pip install virtualenvwrapper-win
Collecting virtualenvwrapper-win
Downloading https://files.pythonhosted.org/packages/f5/23/4cba98733b9122219ce67177d745e4984b524b867cf
3728eaa807ea21919/virtualenvwrapper-win-1.2.5.tar.gz
Collecting virtualenv (from virtualenvwrapper-win)
Downloading https://files.pythonhosted.org/packages/89/66/786e0d6f61bd0612f431e19b016d1ae46f1cb8d21a8
0352cc6774ec876e3/virtualenv-16.7.6-py2.py3-none-any.whl (3.4MB)
|████████████████████████████████| 3.4MB 66kB/s
Installing collected packages: virtualenv, virtualenvwrapper-win
Running setup.py install for virtualenvwrapper-win ... done
Successfully installed virtualenv-16.7.6 virtualenvwrapper-win-1.2.5
