はじめに
WindowsでPythonを使ってatcoder-toolsを使えるようにするまでの手順です。
同じようなことをしている方の参考になれば幸いです
scoopを使ってpythonをインストールする
PythonをそのままWindowsにインストールしても問題ありませんが、scoopを使うと、各種言語のInstallやGitなどの環境のインストールもできるのでお勧めです。
私は、Elixirもインストールしました。
関連するファイルは、scoopディレクトリにインストールされるので、管理しやすい(問題があったら、全部消して入れ直しもできる等)メリットもあります。
Elixirをインストールした時の記事参考
atcoder環境構築
ホームディレクトリにatcoder-workspace
作成
[codestyle]
indent_type='space' # 'tab' or 'space'
indent_width=4
template_file='~/atcoder-workspace/my_template.py'
workspace_dir='~/atcoder-workspace/'
lang='python' # Check README.md for the supported languages.
#code_generator_file="~/custom_code_generator.py"
code_generator_toml="~/atcoder-workspace/code_generator.toml"
[postprocess]
#exec_on_each_problem_dir='clang-format -i ./*.cpp'
#exec_on_contest_dir='touch CMakeLists.txt'
[compiler]
#compile_command='g++ main.cpp -o main -std=c++17'
#compile_only_when_diff_detected=true
[tester]
compile_before_testing=true
compile_only_when_diff_detected=true
timeout_adjustment=1.2
[etc]
download_without_login=false
parallel_download=false
save_no_session_cache=false
skip_existing_problems=false
in_example_format="in_{}.txt"
out_example_format="out_{}.txt"
atcoder用のディレクトリ作成
mkdir atcoder-workspace
テンプレートファイル作成
テンプレートファイルの内容はお好みのものにしてください。
import sys
import os
from math import ceil, floor, sqrt, pi, factorial, gcd,lcm,sin,cos,tan,asin,acos,atan2,exp,log,log10, isqrt
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import cmp_to_key, lru_cache, reduce, cache
from operator import add, iand, ior, itemgetter, mul, xor
from string import ascii_lowercase, ascii_uppercase, ascii_letters
from typing import *
from bisect import bisect, bisect_left, bisect_right
from heapq import heapify, heappop, heappush, heappushpop, nlargest, nsmallest
from sortedcontainers import SortedSet, SortedList, SortedDict
from itertools import product, accumulate,permutations,combinations, count
input = lambda: sys.stdin.readline().rstrip("\r\n")
I = input
II = lambda: int(I())
LI = lambda: list(input().split())
LII = lambda: list(map(int, input().split()))
sys.setrecursionlimit(10000000)
inf = 100100100100100100100
debug = False
# debug = True
if debug:
def dprint(*arg): print(*arg, file=sys.stderr)
else:
def dprint(*arg): pass
venv環境作成
venvは使わななくても環境構築はできます。venvを使うと、pipでインストールするパッケージが、AtCoder専用の環境にできるので、ほかのPythonの開発環境と分ける事ができるので、venvで構築してみます。
【重要】
atcoder-toolsが、python3という名前で、pythonが動作することを前提にしているので、シンボリックリンクを作成しておく必要がありました。
このファイルがないとatcoder-tools test
が想定通りに動作しません。
cd atcoder-workspace
python -m venv .venv
cd .venv/Scripts
bash -c "ln -s python.exe python3.exe"
bashは、scoopでgitをインストールしていれば使えると思います。
PowerShellのコマンドだと、
New-Item -ItemType SymbolicLink -Path python3.exe -Target python.exe
でも同じだとおもいます。管理者権限のPowerShellで実行してください。
python3.exeができている事が大切です。
PS C:\Users\masat\atcoder-workspace\.venv\Scripts> dir
ディレクトリ: C:\Users\masat\atcoder-workspace\.venv\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2025/02/16 16:36 2196 activate
-a---- 2025/02/16 16:36 1016 activate.bat
-a---- 2025/02/16 16:36 2275 activate.fish
-a---- 2025/02/04 16:38 27969 Activate.ps1
-a---- 2025/02/04 16:37 393 deactivate.bat
-a---- 2025/02/16 16:36 108414 pip.exe
-a---- 2025/02/16 16:36 108414 pip3.13.exe
-a---- 2025/02/16 16:36 108414 pip3.exe
-a---- 2025/02/04 16:38 254832 python.exe
-a---- 2025/02/04 16:38 254832 python3.exe
-a---- 2025/02/04 16:38 251248 pythonw.exe
venvをActivateして、pipで必要なモジュールをインストールする
実行するコマンド
.\atcoder-workspace\.venv\Scripts\Activate.ps1
pip install pip install atcoder-tools
pip install "markupsafe==2.0.1"
pip install sortedcontainers
pip install git+https://github.com/not522/ac-library-python
PS C:\Users\masat> .\atcoder-workspace\.venv\Scripts\Activate.ps1
(.venv) PS C:\Users\masat> pip install atcoder-tools
Collecting atcoder-tools
Using cached atcoder_tools-2.14.0-py3-none-any.whl.metadata (30 kB)
Collecting Jinja2<3.0.0,>=2.11.3 (from atcoder-tools)
~~~省略~~~
VSCodeの設定
VSCodeのtask設定
この設定をしておくと、サンプルのテストケースの実行を簡単に行うことができます。
実行方法
回答のmain.pyをエディタで選択
Ctrl+Shift+Pを押して、Task: Run Task
を選択 Test AtCoder Problem (for PowerShell)
を選択
設定ファイル
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Test AtCoder Problem (for PowerShell)",
"type": "shell",
"command": "atcoder-tools test",
"group": {
"kind": "test",
"isDefault": true
},
"options": {
"cwd": "${fileDirname}"
},
"presentation": {
"reveal": "always",
"focus": true,
"panel": "shared",
}
},
{
"label": "atcoder-tools submit (for PowerShell)",
"type": "shell",
"command": "atcoder-tools submit",
"group": "atcoder-tools",
"presentation": {
"reveal": "always",
"focus": true,
"panel": "shared",
},
"options": {
"cwd": "${fileDirname}"
},
},
]
}
その他
VSCodeにPython language supportのプラグインを追加する
Ctrl+Shift+Pを押して、Python: Select Interpreter
を選び、.venvのpythonを選ぶ
この設定は、次の設定をsetting.jsonに記述しておけば選択されるのかもしれません。
{
"python.defaultInterpreterPath": ".venv\\Scripts\\python.exe"
}
追加
atcoder-tools submitで提出した時の言語をPyPy3に変更する方法
.venv\Lib\site-packages\atcodertools\common\language.py
のsubmission_lang_patternの記述を変更する
インストール前のatcoder-toolsを修正してインストールするのが筋だとおもいますが、簡単な方法で対処。atcoder-toolsをバージョンアップ等した時には、もとに戻るので都度対応。