Pythonでゲーム製作:pygameをインストールしよう

2021年3月11日Python,プログラミング

せっかくなら楽しんで学ぼう! あなたのPythonにpygameをインストール

pygameがインストールされているか調べよう

こんばんわ、楓プログラミングスタジオのたっきーです。
今日は、あなたのPython環境にpygameをインストールする方法をご紹介します。

  • pygameはゲームを製作するために設計されたpythonのモジュール集です。
  • 先ず、あなたのPython環境にpygameがインストール済みかを確認します。以下は、Anaconda promptからimportした結果です。importでエラーが出たため、pygameはインストールされていないことが分かりました。
(base) C:\>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pygame'
>>>

pygameをインストールする

  • 次にpipコマンドを使ってpygameのインストールをします。pipはAnacondaによってインストールされたパッケージ管理のためのツールになります。
  • しかし、pip install pygameコマンドでpygameのインストールを試みるもののpipのversionが古いため、エラーとなりました。
(base) C:\>pip install pygame
Collecting pygame
Exception:
Traceback (most recent call last):
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
    wb.build(autobuilding=True)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_set.py", line 554, in _prepare_file
    require_hashes
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 811, in get_page
    inst = cls(resp.content, resp.url, resp.headers)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 731, in __init__
    namespaceHTMLElements=False,
TypeError: parse() got an unexpected keyword argument 'transport_encoding'
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

(base) C:\>

 

  • エラーメッセージに従い、python -m pip install –upgrade pipコマンドを試みます。
  • しかし、またもやpipのversionが古いため、エラーとなりました。pipをバージョンアップするために実行したのに、これでは何も始まりません。
  • ちなみに、pipのversionは、pip -Vコマンドで確認できます。
(base) C:\>python -m pip install --upgrade pip
Exception:
Traceback (most recent call last):
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
    wb.build(autobuilding=True)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_set.py", line 487, in _prepare_file
    req_to_install, finder)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\req\req_set.py", line 428, in _check_skip_installed
    req_to_install, upgrade_allowed)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 811, in get_page
    inst = cls(resp.content, resp.url, resp.headers)
  File "C:\Users\takaa\Anaconda3\lib\site-packages\pip\index.py", line 731, in __init__
    namespaceHTMLElements=False,
TypeError: parse() got an unexpected keyword argument 'transport_encoding'
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

(base) C:\>
(base) C:\>pip -V
pip 9.0.1 from C:\Users\takaa\Anaconda3\lib\site-packages (python 3.6)

 

  • これがベストプラクティスか否か分かりませんが、下記ウェブよりget-pip.pyを入手して、pythonで実行します。

https://bootstrap.pypa.io/

 

(base) C:\Users\takaa\Documents\python>python get-pip.py
Collecting pip
  Cache entry deserialization failed, entry ignored
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. (read timeout=15)",)': /simple/pip/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. (read timeout=15)",)': /simple/pip/
  Downloading pip-9.0.3-py2.py3-none-any.whl (1.4MB)
    100% |????????????????????????????????| 1.4MB 127kB/s
Installing collected packages: pip
Successfully installed pip-9.0.3

(base) C:\Users\takaa\Documents\python>pip -V
pip 9.0.3 from c:\users\takaa\anaconda3\lib\site-packages (python 3.6)

 

  • 無事に、pipがversion 9.0.1からversion 9.0.3にアップグレードできました。
  • 次こそ、目的のpygameのインストールです。再度、pip install pygameコマンドを実行します。

 

(base) C:\>pip install pygame
Collecting pygame
  Cache entry deserialization failed, entry ignored
  Downloading pygame-1.9.3-cp36-cp36m-win_amd64.whl (4.2MB)
    100% |????????????????????????????????| 4.2MB 159kB/s
Installing collected packages: pygame
Successfully installed pygame-1.9.3

(base) C:\>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>>

 

  • 今度は、ちゃんとimportが成功していますね。
  • さあー、あなたもゲーム製作の環境を整えよう!