I tried to build an environment when studying Python, but this was unexpectedly difficult.
Python is installed by default on Mac, but old Python2 is installed instead of the latest Python3, so it can not be left as it is for new study & development.
Moreover, there are many installation methods, and it was difficult for Python beginners to know which method to install. Furthermore, if I didn't know the right or left, I was confused with the Python installation method and the library installation method, and there were many occasions when I was asked, "What? What were you doing now?"
I had the same problem when I asked my fellow Python students, so I thought I'd write it as a memorandum.
This time, I installed the latest version of Python 3 and the game library pygame on Mac OS Catalina using Homebrew and pyenv.
Homebrew --Not to mention a package management tool for Mac and Linux.
pyenv --Python version control tool. It seems that you can install Python without using this, but since it is easy to install and switch between multiple versions, it seems that it will be easier to use later if you install using pyenv, so I will use this this time.
pip --A library management tool for Python. Use this for installation work of each library. When you install Python, it will be installed at the same time, so no installation work is required.
pygame --A collection of game creation modules for Python. If you use this, you can make a game relatively easily with just Python. In fact, even a Python beginner could make a game in two days. I made a game that uses this to learn Python, so this time I will summarize the procedure to install the library using this.
Execute the following command in the terminal according to the official documentation. Of course, if it is already installed, you can skip it.
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
If the installation is successful, you can check the version of Homebrew with brew -v
.
$ brew -v
Homebrew 2.4.9
Homebrew/homebrew-core (git revision 3b87b; last commit 2020-08-04)
Homebrew/homebrew-cask (git revision 2ee9f; last commit 2020-08-04)
Update Homebrew to the latest version with brew update
and install pyenv with brew install
.
$ brew update
$ brew install pyenv
It is OK if you can check the version of pyenv with pyenv -v
.
$ pyenv -v
pyenv 1.2.20
pyenv itself works with this, but Python installed via pyenv will not be recognized unless you add the pyenv settings to the terminal, so add the settings to the terminal with the following command and make the terminal recognize the added settings.
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
$ source .zshrc
If the terminal is bash, use the following command.
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
$ source .bash_profile
Of course, there is no problem even if you write directly to the profile etc. with vim.
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
You can check the version of Python that can be installed by pyenv with the pyenv install -l
command.
$ pyenv install -l
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
...
3.8.4
3.8.5
3.9.0b5
3.9-dev
3.10-dev
...
Unless you have a specific reason, it's a good idea to have the latest stable version (the one without the'dev'after the number) installed.
By the way, platforms such as anaconda can also be installed with pyenv.
This time install 3.8.5 with the pyenv install
command.
$ pyenv install 3.8.5
At this stage, the installation is just complete, and the version of Python used has not yet been switched.
python -v
Even if you check the version with the command, python2 installed by default on mac is recognized.
$ python -V
Python 2.7.16
Switch the installed Python to work by default with the pyenv global
command.
$ pyenv global 3.8.5
If you can confirm that the version has been switched with python -V
, the installation is successful.
If it doesn't recognize it well, try reloading the pyenv settings into the terminal with `` $ source .zshrc```. (* For bash, use
$ source .bash_profile`.)
$ python -V
Python 3.8.5
By the way, you can see the list of Python versions installed with ``` pyenv versions` ``. The one with * at the beginning works as the default version.
$ pyenv versions
system
* 3.8.5 (set by /Users/linus/.pyenv/version)
Install the library with the pip install
command. This time I will install pygame.
$ pip install pygame
This time, we need the latest version under development, not the latest stable version, so specify the version and install it. You can install by specifying the version by adding the version after the library name.
$ pip install pygame==2.0.0.dev10
You can see a list of libraries installed using pip with the pip list
command.
$ pip list
Package Version
---------- -----------
pip 20.1.1
pygame 2.0.0.dev10
setuptools 47.1.0
You can install it with the specified version. Other libraries, such as Django, can be installed in the same way.
When uninstalling the package etc. installed this time, you can execute it with the following command. Please note that files may remain when uninstalling unless they are deleted in the reverse order of installation.
$ pip uninstall pygame
$ pyenv uninstall 3.8.5
$ brew uninstall pyenv
I had a lot of trouble when I was working on building the environment for the first time, but it was quite difficult to reorganize it. I often hear that Python is a language that is easy for beginners to program, but it is hard to say that environment construction is for beginners.
Recommended Posts