atcoder-python

本記事は以前Qiitaに投稿した内容を本ブログに持ってきています。

はじめに

最近、AtCoderを始めました。
AtCoderは競技プログラミングサイトで、毎週リアルタイムのコンテストが開催されていて、AtCoderで評価されたレーティングを利用して、転職活動ができるAtCoderJobsというサービスが話題になっているそうです。
最初、シェルがfishでHomebrewなどが入っている状態で色々試していましたが、うまくいかなかったので、Mac環境をきれいにして環境構築をしました。

環境

macOS Catalina 10.15.2 Homebrew 2.1.15 bash 3.2.57

シェルをbashに戻す

ターミナルで以下コマンドを叩いて、ターミナルの再起動をするとシェルがbashに戻ります。

$ chsh -s /bin/bash

Homebrewをアンインストールする

以下コマンドを叩いてパスワードを入力すると、Homebrewをアンインストールできます。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

Homebrewの公式サイトに記載されています。

Homebrewインストールする

以下コマンドを叩く。

$ usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

インストールできたか確認するために、以下コマンドを叩きます。

$ brew doctor

すると、Warningが大量に出たので、brew doctorでWarningが出たので解決方法まとめました。(for Mac)を参考に解決しました。
Warning: Unbrewed dylibs were found in /usr/local/include. のログに関しては残すことを忘れてしまっていたので、上記記事を引用しています。

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/arith.h
  /usr/local/include/binary_log_types.h
  /usr/local/include/cdt.h
  /usr/local/include/cgraph.h
  /usr/local/include/color.h
  /usr/local/include/decode.h
  /usr/local/include/demux.h
  /usr/local/include/encode.h
  /usr/local/include/errmsg.h
  /usr/local/include/fcfreetype.h
  /usr/local/include/fcprivate.h
  /usr/local/include/fontconfig.h
  /usr/local/include/freetype/config/ftconfig.h
  /usr/local/include/freetype/config/ftheader.h
  /usr/local/include/freetype/config/ftmodule.h
  /usr/local/include/freetype/config/ftoption.h
  /usr/local/include/freetype/config/ftstdlib.h
  /usr/local/include/freetype/freetype.h
  /usr/local/include/freetype/ftadvanc.h
  /usr/local/include/freetype/ftbbox.h
  /usr/local/include/freetype/ftbdf.h
  /usr/local/include/freetype/ftbitmap.h
  /usr/local/include/freetype/ftbzip2.h
  /usr/local/include/freetype/ftcache.h
  /usr/local/include/freetype/ftchapters.h
  /usr/local/include/freetype/ftcid.h
  /usr/local/include/freetype/ftdriver.h
  /usr/local/include/freetype/fterrdef.h
  /usr/local/include/freetype/fterrors.h
  /usr/local/include/freetype/ftfntfmt.h
  /usr/local/include/freetype/ftgasp.h
  /usr/local/include/freetype/ftglyph.h
  /usr/local/include/freetype/ftgxval.h
  /usr/local/include/freetype/ftgzip.h
  /usr/local/include/freetype/ftimage.h
  /usr/local/include/freetype/ftincrem.h
  /usr/local/include/freetype/ftlcdfil.h
  /usr/local/include/freetype/ftlist.h
  /usr/local/include/freetype/ftlzw.h
  /usr/local/include/freetype/ftmac.h
  /usr/local/include/freetype/ftmm.h
  /usr/local/include/freetype/ftmodapi.h
  /usr/local/include/freetype/ftmoderr.h
  /usr/local/include/freetype/ftotval.h
  /usr/local/include/freetype/ftoutln.h
  /usr/local/include/freetype/ftparams.h
  /usr/local/include/freetype/ftpfr.h
  /usr/local/include/freetype/ftrender.h
  /usr/local/include/freetype/ftsizes.h
  /usr/local/include/freetype/ftsnames.h
  /usr/local/include/freetype/ftstroke.h
  /usr/local/include/freetype/ftsynth.h
  /usr/local/include/freetype/ftsystem.h
  /usr/local/include/freetype/fttrigon.h
  /usr/local/include/freetype/fttypes.h
  /usr/local/include/freetype/ftwinfnt.h
  /usr/local/include/freetype/t1tables.h
  /usr/local/include/freetype/ttnameid.h
  /usr/local/include/freetype/tttables.h
  /usr/local/include/freetype/tttags.h
  /usr/local/include/ft2build.h
  /usr/local/include/geom.h
  /usr/local/include/graphviz_version.h
  /usr/local/include/gvc.h
  /usr/local/include/gvcext.h
  /usr/local/include/gvcjob.h
  /usr/local/include/gvcommon.h
  /usr/local/include/gvconfig.h
  /usr/local/include/gvplugin.h
  /usr/local/include/gvplugin_device.h
  /usr/local/include/gvplugin_layout.h
  /usr/local/include/gvplugin_loadimage.h
  /usr/local/include/gvplugin_render.h
  /usr/local/include/gvplugin_textlayout.h
  /usr/local/include/gvpr.h
  /usr/local/include/lt_dlloader.h
  /usr/local/include/lt_error.h
  /usr/local/include/lt_system.h
  /usr/local/include/mux.h
  /usr/local/include/mux_types.h
  /usr/local/include/my_command.h
  /usr/local/include/my_list.h
  /usr/local/include/mysql.h
  /usr/local/include/mysql/client_plugin.h
  /usr/local/include/mysql/plugin_auth_common.h
  /usr/local/include/mysql/udf_registration_types.h
  /usr/local/include/mysql_com.h
  /usr/local/include/mysql_time.h
  /usr/local/include/mysql_version.h
  /usr/local/include/mysqld_error.h
  /usr/local/include/mysqlx_ername.h
  /usr/local/include/mysqlx_error.h
  /usr/local/include/mysqlx_version.h
  /usr/local/include/pack.h
  /usr/local/include/pathgeom.h
  /usr/local/include/pathplan.h
  /usr/local/include/png.h
  /usr/local/include/pngconf.h
  /usr/local/include/textspan.h
  /usr/local/include/types.h
  /usr/local/include/usershape.h
  /usr/local/include/xdot.h

Warning: Your Xcode (10.3) is outdated.
Please update to Xcode 11.3 (or delete it).
Xcode can be updated from the App Store.

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/lib/node_modules/expo-cli/node_modules/.bin/detect-libc
  /usr/local/lib/node_modules/expo-cli/node_modules/.bin/prebuild-install

まずは、Warning: Unbrewed header files were found in /usr/local/include.の解決をします。

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/arith.h
  /usr/local/include/binary_log_types.h
  /usr/local/include/cdt.h
  /usr/local/include/cgraph.h
  ... ← 長いので省略

ターミナルで以下を叩きます。

$ vim ~/.del_files.sh

作成したファイルに2行目以降にUnexpected header files:に記載されているファイルをコピペします。

#!/bin/bash
  /usr/local/include/arith.h
  /usr/local/include/binary_log_types.h
  /usr/local/include/cdt.h
  /usr/local/include/cgraph.h
  ...

以下の部分は参考にした記事からまるっと引用しています。

カーソルを一番上のファイル(2行目)の頭におき、ctrl + vで矩形選択のビジュアルモード(VISUAL BLOCK)に移行。↓カーソルで一番下のファイルの頭まで選択し、:を押します。

すると、下のラインに既に :'<,'>が入力された状態でコマンドラインが表示されるので、続けて s/^/rm -f /と入力します。(rm は、ファイルやディレクトリを削除してあげるコマンドで、 -f は、「削除しますか?」的なメッセージを都度出さないようにするオプションです。)

Enterを押して、全ての行(一番上以外)の先頭にs/^/rm -f /が追加されているのを確認したら、もう一度:を押します。一番下の行に入力欄が出てくるので、wq!と入力し、Enterを押します。

$ bash ~/.del_files.sh

これで対象ファイルの削除ができました。

次に、Warning: Your Xcode (10.3) is outdated.を解決します。

Warning: Your Xcode (10.3) is outdated.
Please update to Xcode 11.3 (or delete it).
Xcode can be updated from the App Store.

App StoreでXcodeをアップデートします。 アップデートできたら、再度

$ brew doctor

を叩きます。
そうすると、

Error: You have not agreed to the Xcode license. Please resolve this by running:
  sudo xcodebuild -license accept

が表示されたので、

$ sudo xcodebuild -license accept

を叩くと、Xcodeは解決できました。

次に、Warning: Broken symlinks were found. Remove them with "brew cleanup"を解決をします。

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/lib/node_modules/expo-cli/node_modules/.bin/detect-libc
  /usr/local/lib/node_modules/expo-cli/node_modules/.bin/prebuild-install

以前、会社のReact Nativeの勉強会に参加していた時の残骸が残っていたので、これも削除します。

$ sudo rm -f /usr/local/lib/node_modules/expo-cli/node_modules/.bin/detect-libc
$ sudo rm -f /usr/local/lib/node_modules/expo-cli/node_modules/.bin/prebuild-install

すると、

$ brew doctor
Your system is ready to brew.

が表示されて、Homebrewのインストールは完了しました。

pyenvをインストールする

複数のPython環境を扱えるように、pyenvをインストールします。
MacOSとHomebrewとpyenvで快適python環境を。を参考にしました。

$ brew install pyenv
==> Installing dependencies for pyenv: autoconf, openssl@1.1, pkg-config and readline
==> Installing pyenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.catalina.bott
==> Downloading from https://akamai.bintray.com/ca/ca510b350e941fb9395522a03f9d2
######################################################################## 100.0%
==> Pouring autoconf-2.69.catalina.bottle.4.tar.gz
🍺  /usr/local/Cellar/autoconf/2.69: 67 files, 3.0MB
==> Installing pyenv dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.1d.catalina
==> Downloading from https://akamai.bintray.com/d7/d7f992ebfd78f80828051f6dc6a1a
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.1d.catalina.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1d: 7,983 files, 17.9MB
==> Installing pyenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.catalina.
==> Downloading from https://akamai.bintray.com/c0/c0a6927c8e404f6db8b14d6644a21
######################################################################## 100.0%
==> Pouring pkg-config-0.29.2.catalina.bottle.1.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.2: 11 files, 623KB
==> Installing pyenv dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.1.catalina.bot
==> Downloading from https://akamai.bintray.com/ab/ab3c966f4cae7d0f3ecc5688bb989
######################################################################## 100.0%
==> Pouring readline-8.0.1.catalina.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/readline/8.0.1: 48 files, 1.5MB
==> Installing pyenv
==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.2.16.catalina.bottl
==> Downloading from https://akamai.bintray.com/1d/1da51fb67d5aac04bda57820d4bf9
######################################################################## 100.0%
==> Pouring pyenv-1.2.16.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/pyenv/1.2.16: 671 files, 2.5MB
==> Caveats
==> openssl@1.1
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

インストールできました。

pyenv用にprofileを調整

この部分はMacOSとHomebrewとpyenvで快適python環境を。から引用しています。

pyenvのインストールの最後の所に細かいヒント(Caveats)が書いてあったので、従っておきます。具体的には2点

  • pyenvはデフォルトでは~/.pyenvにファイルを置きたがるので、/usr/loca/var/pyenvを使う様にお願いする
  • pyenvには自動補完機能があるので、それを提供してもらう。
$ vim ~/.bash_profile

を叩いて、.bash_profileに以下内容を記載します。

# pyenvさんに~/.pyenvではなく、/usr/loca/var/pyenvを使うようにお願いする
export PYENV_ROOT=/usr/local/var/pyenv

# pyenvさんに自動補完機能を提供してもらう
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

インストールしたpyenvの確認

以下のように表示されたら、問題なしです。

$ pyenv --version
pyenv 1.2.16

Pythonをインストールする

pyenvでインストールできるPythonを確認します。

$ pyenv install --list

Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  ...

インストールできるバージョンが多数表示されます。

次に、Python 3.4.3をインストールします。

$ pyenv install 3.4.3

ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (OS X 10.15.2 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/kd/l6xnjrd50bjcb6p8vc9hq6n40000gn/T/python-build.20200121234254.85780
Results logged to /var/folders/kd/l6xnjrd50bjcb6p8vc9hq6n40000gn/T/python-build.20200121234254.85780.log

Last 10 log lines:
(cd /Users/ユーザー名/.pyenv/versions/3.4.3/share/man/man1; ln -s python3.4.1 python3.1)
if test "xupgrade" != "xno"  ; then \
		case upgrade in \
			upgrade) ensurepip="--upgrade" ;; \
			install|*) ensurepip="" ;; \
		esac; \
		 ./python.exe -E -m ensurepip \
			$ensurepip --root=/ ; \
	fi
Ignoring ensurepip failure: pip 6.0.8 requires SSL/TLS

のようなエラーが発生します。 これがよく分からなく、色々と試しましたが、pyenvでinstallできないが原因だったようです。。
Python 3.4.3入れられないのか。。

上記記事を参考に

$ pyenv install 3.5.3

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.3.tar.xz...
-> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
Installing Python-3.5.3...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.5.3 to /Users/ユーザー名/.pyenv/versions/3.5.3

を叩くと、うまくインストールされました!!

AtCoderで使用するPython設定

AtCoder用フォルダで実行するPythonを設定します。 まず、AtCoder用フォルダを作成して、移動します。

$ cd AtCoder/ ←今回はこのようなフォルダを作成しました。
$ pyenv local 3.5.3

そして、確認をすると

$ python --version
Python 2.7.16

あれ、、デフォルトのPythonのままかわっていない。。
ググってみると、pyenvでPythonのバージョンを切り替えられない場合の対処法+を発見しました。

記事の内容通りに、

$ pyenv init
# Load pyenv automatically by appending
# the following to ~/.bash_profile:

eval "$(pyenv init -)" ←この部分を ~/.bash_profileに記載します。

そして、

source ~/.bash_profile

を叩くと、

python --version
Python 3.5.3

このフォルダでPython 3.5.3が使えるようになりました!!

まとめ

公式ドキュメントはしっかり読まないとやっぱりダメですね。。
今回はPython 3.5.3をインストールしましたが、AtCoderはPython 3.4.3が対象で、 もしコンテスト中に使い勝手が悪いとなったら、Python 3.4.3のインストールを検討します。
にしても、今回は遠回りしすぎたなぁ。