lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
536 stars 189 forks source link

Wrong Python Version & Actor not found in VS Studio Code #262

Closed jasperf closed 3 years ago

jasperf commented 3 years ago

Tried to install on Mac as there is no homebrew port.. that I know of. So I used pip. But was told pygame zero uses Python 2.7.17 and o cannot install as I run Python 3.4:

pip install git+https://github.com/lordmauve/pgzero@master
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting git+https://github.com/lordmauve/pgzero@master
  Cloning https://github.com/lordmauve/pgzero (to revision master) to /private/var/folders/_4/g8fn6chn46g9v058h8k4pzpw0000gn/T/pip-req-build-0vJxRX
  Running command git clone -q https://github.com/lordmauve/pgzero /private/var/folders/_4/g8fn6chn46g9v058h8k4pzpw0000gn/T/pip-req-build-0vJxRX
ERROR: Package 'pgzero' requires a different Python: 2.7.17 not in '>=3.4'

Is there perhaps a version that does work with Python 3?

jasperf commented 3 years ago

I installed pyenv to be able to switch Python versions on my Mac:

brew install pyenv
eval "$(pyenv init -)"

Then I installed requested version

pyenv install 2.7.17
Downloading openssl-1.0.2q.tar.gz...
-> https://www.openssl.org/source/old/1.0.2/openssl-1.0.2q.tar.gz
Installing openssl-1.0.2q...
Installed openssl-1.0.2q to /Users/jasper/.pyenv/versions/2.7.17

python-build: use readline from homebrew
Downloading Python-2.7.17.tar.xz...
-> https://www.python.org/ftp/python/2.7.17/Python-2.7.17.tar.xz
Installing Python-2.7.17...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
....

Then you can check version you have using:

pyenv versions

and you can switch between python versions with the command:

pyenv global version_of_choice

all based on this excellent article by Python Engineering.

However, when I ran a check and tried installing again (even reopened iTerm I still had the same Python version complaint:

pyenv versions
* system (set by /Users/jasper/.pyenv/version)
  2.7.17
 jasper@MacMini  ~  pip install git+https://github.com/lordmauve/pgzero@master

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting git+https://github.com/lordmauve/pgzero@master
  Cloning https://github.com/lordmauve/pgzero (to revision master) to /private/var/folders/_4/g8fn6chn46g9v058h8k4pzpw0000gn/T/pip-req-build-Fs5aBm
  Running command git clone -q https://github.com/lordmauve/pgzero /private/var/folders/_4/g8fn6chn46g9v058h8k4pzpw0000gn/T/pip-req-build-Fs5aBm
ERROR: Package 'pgzero' requires a different Python: 2.7.17 not in '>=3.4'
jasperf commented 3 years ago

Well, when I used pip with 2.7.17 set as Python version it did work

pip install pygame
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pygame
  Downloading https://files.pythonhosted.org/packages/87/03/f64bcc5e6dd926fab25c3b8661b46c787518dcbea7770048cd784e8e758e/pygame-2.0.1-cp27-cp27m-macosx_10_9_intel.whl (6.9MB)
     |████████████████████████████████| 6.9MB 2.8MB/s
Installing collected packages: pygame
Successfully installed pygame-2.0.1

and it also installed Idle to use with pygame from the command line. But will try to work with VS Studio Code instead.

jasperf commented 3 years ago

Also realized I could install pygame using Python 3 using pip3. So I added Python 3.4.0 using pyenv and installed it:

pyenv versions
* system (set by /Users/jasper/.pyenv/version)
  2.7.17
  3.4.0
 jasper@MacMini  ~  python --version
Python 3.9.6
 jasper@MacMini  ~  pip3 install pygame
Collecting pygame
  Downloading pygame-2.0.1-cp39-cp39-macosx_10_9_intel.whl (6.9 MB)
     |████████████████████████████████| 6.9 MB 2.5 MB/s
Installing collected packages: pygame
Successfully installed pygame-2.0.1
WARNING: You are using pip version 20.3.3; however, version 21.2.4 is available.
You should consider upgrading via the '/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command.

short test from Idle 3:

Python 3.9.2 (default, Feb 24 2021, 13:30:36) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pygame
pygame 2.0.1 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
lordmauve commented 3 years ago

Pygame Zero has never supported Python 2. You read this error message in exactly the opposite sense:

Package 'pgzero' requires a different Python: 2.7.17 not in '>=3.4'

pgzero requires 3.4+, you were running with Python 2.7.17.

jasperf commented 3 years ago

I see now that I misread it all. Feeling kind of silly now.. I think I perhaps still had Python 2 running for an older Ansible setup. And after tinkering for quite some time I got back to Python 3 using pyenv. And yes, using pip3 I could then install all. Do still wonder how I managed to install a version on 2.. but perhaps it was possible but would just never work properly. Thank you work letting me know @lordmauve .

Checking Python on my MacMini now I see

jasper@MacMini  ~  brew list | grep ^python
python-tk@3.9
python@2.7.17
python@3.8
python@3.9
 jasper@MacMini  ~  which -a python python2 python2.7 python3 python3.6
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python2
/usr/bin/python2
/usr/local/bin/python2.7
/usr/bin/python2.7
/usr/local/bin/python3
/usr/bin/python3
 jasper@MacMini  ~  python --version
Python 3.9.7

and idle or idle3 both work, but Python 3 is running now so should be good to go. Just need to figure out with VS Code errors like

Exception has occurred: NameError       (note: full exception trace is shown but execution is paused at: <module>)
name 'Actor' is not defined
  File "/Users/jasper/code/galaxian/galaxian.py", line 8, in <module> (Current frame)
    bullet = Actor('bullet', center=(0, -10))

adding import pygame at the top did not help. On Raspberry Pi Galaxian worked out of the box.

jasperf commented 3 years ago

Wanted to check if I had pygame installed and used pip3

jasper@MacMini  ~  pip3 list
Package           Version
----------------- -------
ansible           2.10.4
ansible-base      2.10.4
astroid           2.4.2
cffi              1.14.4
cryptography      3.3.1
isort             5.6.4
Jinja2            2.11.2
lazy-object-proxy 1.4.3
MarkupSafe        1.1.1
mccabe            0.6.1
packaging         20.8
pip               20.3.3
protobuf          3.14.0
pycparser         2.20
pygame            2.0.1
pylint            2.6.0
pyparsing         2.4.7
PyYAML            5.3.1
setuptools        57.4.0
six               1.15.0
toml              0.10.2
wheel             0.37.0
wrapt             1.12.1
WARNING: You are using pip version 20.3.3; however, version 21.2.4 is available.
You should consider upgrading via the '/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command.

As I was warned to upgrade pip I decided to do that and now pip can be used for Python 3 it seems and I can skip using pip3:

 jasper@MacMini  ~  /usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip
Requirement already satisfied: pip in ./Library/Python/3.9/lib/python/site-packages (20.3.3)
Collecting pip
  Downloading pip-21.2.4-py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 2.8 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.3.3
    Uninstalling pip-20.3.3:
      Successfully uninstalled pip-20.3.3
Successfully installed pip-21.2.4

I did a new list check and had all:

 jasper@MacMini  ~  pip list
Package           Version
----------------- -------
ansible           2.10.4
ansible-base      2.10.4
astroid           2.4.2
cffi              1.14.4
cryptography      3.3.1
isort             5.6.4
Jinja2            2.11.2
lazy-object-proxy 1.4.3
MarkupSafe        1.1.1
mccabe            0.6.1
packaging         20.8
pip               21.2.4
protobuf          3.14.0
pycparser         2.20
pygame            2.0.1
pylint            2.6.0
pyparsing         2.4.7
PyYAML            5.3.1
setuptools        57.4.0
six               1.15.0
toml              0.10.2
wheel             0.37.0
wrapt             1.12.1

And double checking I am using Python 3 based on Homebrew installation, not the pyenv installation:

 jasper@MacMini  ~  pip --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
jasperf commented 3 years ago

On the actor error, I found your threads https://github.com/lordmauve/pgzero/issues/61 as well so will look into this some more to make Galaxian work on my Mac and bypass

Exception has occurred: NameError       (note: full exception trace is shown but execution is paused at: <module>)
name 'Actor' is not defined
  File "/Users/jasper/code/galaxian/galaxian.py", line 7, in <module> (Current frame)
    bullet = Actor('bullet', center=(0, -10))
jasperf commented 3 years ago

Hmm I read

_Update Aug 18, 2019: The screen object cannot be imported since it is created as global variable during runtime (object = instance of the Screen class) and IDE-supported code completion is not possible then. See the source code: https://github.com/lordmauve/pgzero/blob/master/pgzero/game.py (esp. the def reinitscreen part)

at https://stackoverflow.com/questions/55438239/name-actor-is-not-defined so perhaps I will have to use Idle3 instead then.. On Raspberry Pi using a PI UI I was able to run all, but perhaps there something like

PYFLAKES_BUILTINS=Actor,Rect,ZRect,animate,clock,exit,images,keyboard,keymods,keys,mouse,music,screen,sounds,storage,tone
export PYFLAKES_BUILTINS

was implemented.. will have to see. And .. one would think we can do this on MacOs as well somehow.

jasperf commented 3 years ago

Added

# PyGame
PGZERO_BUILTINS=Actor,Rect,ZRect,animate,clock,exit,images,keyboard,keymods,keys,mouse,music,screen,sounds,storage,tone
export PGZERO_BUILTINS

to /Users/jasper/.zshrc and restarted VS Studio Code and iTerm. Even sourced the rc file. But still seeing

jasper@MacMini  ~/code/galaxian   /usr/bin/env /usr/local/bin/python /Users/jasper/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/lib/python/debugpy/launcher 51541 -- /Users/jasper/code/galaxian/galaxian.py 
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/jasper/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/Users/jasper/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/Users/jasper/.vscode/extensions/ms-python.python-2021.10.1317843341/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/jasper/code/galaxian/galaxian.py", line 7, in <module>
    bullet = Actor('bullet', center=(0, -10))
NameError: name 'Actor' is not defined