0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Qiita の Q&A がきっかけで、Windows コマンドプロンプトで atcoder-tools を使う方法を調べたので、まとめておきます。

ネットを検索しても、atcoder-tools を Windows で使った例が見つかりませんでした。逆に、次のような 「使えない」といった記事が見つかります。

scr1.png

Windows で C++ などのコンパイル環境を準備するのは いろいろと面倒ですが、それに比べると、Python は Windows でも扱いやすい言語です。

atcoder-tools インストール方法

pip install atcoder-tools

Windows だからと言って インストール方法に違いは無く、
インストール後の注意点として、WSLでも同様な 次の2つの問題に直面するため、対応が必要です。

  • 1.pip install markupsafe==2.0.1

    markupsafe の最新版だと 次のエラーとなるため、2.0.1 にダウングレードする
    ImportError: cannot import name 'soft_unicode' from 'markupsafe'
      

  • 2.atcoder ログインエラー

    ↓ この記事を参考に、cookieファイルを直接更新する

環境

Python 言語で使用する場合は、
.atcodertools.tomlを 次のように設定します。

Windows11(24H2) コマンドプロンプト
Microsoft Windows [Version 10.0.26100.4484]
(c) Microsoft Corporation. All rights reserved.

C:\Users\nak435>python --version
Python 3.13.5

C:\Users\nak435>atcoder-tools version
2.14.0

C:\Users\nak435>type .\.atcodertools.toml
[tester]
compile_before_testing=false
[compiler]
compile_command='python'
[codestyle]
workspace_dir='~/atcoder-workspace/'
lang='python'
テストケースの配置
C:\Users\nak435>atcoder-tools gen abc400

2025-07-01 21:15:05,497 INFO: Going to load C:\Users\nak435\.atcodertools.toml as config
2025-07-01 21:15:05,515 INFO: Loaded session from C:\Users\nak435\.local\share\atcoder-tools\cookie.txt
2025-07-01 21:15:06,056 INFO: Successfully Logged in using the previous session cache.
2025-07-01 21:15:06,057 INFO: If you'd like to invalidate the cache, delete C:\Users\nak435/.local/share\atcoder-tools\cookie.txt.
2025-07-01 21:15:06,057 INFO: Login successful.
=================================================
2025-07-01 21:15:06,349 INFO: Problem A: C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\templates\default_template.py is used for template
2025-07-01 21:15:06,494 INFO: Problem A: Created examples.
2025-07-01 21:15:06,571 INFO: Problem A: Format prediction succeeded
2025-07-01 21:15:06,595 INFO: Problem A: Saved code to C:\Users\nak435/atcoder-workspace/abc400\A\main.py
2025-07-01 21:15:06,596 INFO: Problem A: Saved metadata to C:\Users\nak435/atcoder-workspace/abc400\A\metadata.json
=================================================
2025-07-01 21:15:06,596 INFO: Problem B: C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\templates\default_template.py is used for template
(以下、省略)

C:\Users\nak435>cd atcoder-workspace

C:\Users\nak435\atcoder-workspace>tree . /f

C:\USERS\NAK435\ATCODER-WORKSPACE
└─abc400
    ├─A
    │      in_1.txt
    │      in_2.txt
    │      in_3.txt
    │      main.py
    │      metadata.json
    │      out_1.txt
    │      out_2.txt
    │      out_3.txt
    │
    ├─B
(以下、省略)

C:\Users\nak435\atcoder-workspace>type .\abc400\A\main.py
#!/usr/bin/env python3
import sys

(以下、省略)
ABC400-A問題の解答コードに書き換えて、テスト

C:\Users\nak435\atcoder-workspace>cd abc400\a

C:\Users\nak435\atcoder-workspace\abc400\A>type main.py
#!/usr/bin/env python3
a = int(input())
print(400 // a if 400 % a == 0 else -1)

C:\Users\nak435\atcoder-workspace\abc400\A>atcoder-tools test
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\Scripts\atcoder-tools.exe\__main__.py", line 7, in <module>
    sys.exit(main())
             ~~~~^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\atcoder_tools.py", line 66, in main
    exit_program(tester_main(prog, args))
                 ~~~~~~~~~~~^^^^^^^^^^^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\tester.py", line 424, in main
    exec_file = infer_exec_file(
        glob.glob(os.path.join(args.dir, '*')), excluded_exec_files)
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\tester.py", line 63, in infer_exec_file
    raise NoExecutableFileError
atcodertools.tools.tester.NoExecutableFileError

Winodws 環境では、main.pyの1行目に記述した #! で始まる行(shebangと言うそうだ)は、単なるコメントで無効です。よって、上記のようなエラーになります。

Windows でテストする方法

そこで、次のバッチファイルをmain.pyと同じディレクトリに配置します。

main.bat
@echo off
python main.py
ABC400-A問題のテスト
C:\Users\nak435\atcoder-workspace\abc400\A>tree . /f

C:\USERS\NAK435\ATCODER-WORKSPACE\ABC400\A
    in_1.txt
    in_2.txt
    in_3.txt
    main.bat
    main.py
    metadata.json
    out_1.txt
    out_2.txt
    out_3.txt


C:\Users\nak435\atcoder-workspace\abc400\A>type main.bat
@echo off
python main.py

C:\Users\nak435\atcoder-workspace\abc400\A>type main.py
#!/usr/bin/env python3
a = int(input())
print(400 // a if 400 % a == 0 else -1)

C:\Users\nak435\atcoder-workspace\abc400\A>atcoder-tools test
2025-07-01 21:39:00,722 INFO: Inferred exec file: .\main.bat
# in_1.txt ... PASSED 119 ms
# in_2.txt ... PASSED 92 ms
# in_3.txt ... PASSED 99 ms
Passed all test cases!!!

C:\Users\nak435\atcoder-workspace\abc400\A>atcoder-tools submit
2025-07-01 21:39:21,298 INFO: config is loaded from USER_CONFIG_PATH(C:\Users\nak435\.atcodertools.toml)
2025-07-01 21:39:21,308 INFO: Loaded session from C:\Users\nak435\.local\share\atcoder-tools\cookie.txt
2025-07-01 21:39:21,468 INFO: Successfully Logged in using the previous session cache.
2025-07-01 21:39:21,468 INFO: If you'd like to invalidate the cache, delete C:\Users\nak435/.local/share\atcoder-tools\cookie.txt.
2025-07-01 21:39:21,473 INFO: Inferred exec file: .\main.bat
# in_1.txt ... PASSED 103 ms
# in_2.txt ... PASSED 91 ms
# in_3.txt ... PASSED 91 ms
Passed all test cases!!!
2025-07-01 21:39:21,949 ERROR: Cancel submitting because you already sent some code to the problem. Please specify -u to send the code. https://atcoder.jp/contests/abc400/submissions/64499004

いつからか、コンテスト中以外の時間帯/期間は、submit がエラーになるようになりました。
その場合は、CLIPコマンドを使い、コードをクリップボードにコピーして、Atcoderの提出サイトに直接ペーストして『提出(submit)』してください。

ABC400-A問題のコピー
C:\Users\nak435\atcoder-workspace\abc400\A>clip <main.py

C:\Users\nak435\atcoder-workspace\abc400\A>

scr2.png

Python 以外のインタプリタ系言語の場合も、main.batに起動コマンドを書けば、テスト は可能だと思います。
もちろん、コンパイル言語の場合は、.atcodertools.tomlにコンパイルコマンドを定義すれば普通に使えます。

(コンパイル言語) Swift で試してみた

↑ こちらの手順に沿って、Windows11 に Swift環境を構築。

.atcodertools.tomlを swift 向けに書き換え

.atcodertools.toml
[compiler]
compile_command='swiftc main.swift -o main.exe'
compile_only_when_diff_detected=true
[tester]
compile_before_testing=true
compile_only_when_diff_detected=true
timeout_adjustment=1.2
[codestyle]
workspace_dir='~/atcoder-workspace/'
lang='swift'
C:\Users\nak435>cd atcoder-workspace

C:\Users\nak435\atcoder-workspace>atcoder-tools gen abc400
(省略)

C:\Users\nak435\atcoder-workspace\abc400\A>type main.swift
import Foundation
let a = Int(readLine()!)!
print(400.isMultiple(of: a) ? 400 / a : -1 )

C:\Users\nak435\atcoder-workspace\abc400\A>atcoder-tools test
[Main Program]
compile command:  swiftc main.swift -o main.exe
Compiling... (command: `swiftc main.swift -o main.exe`)
   ライブラリ main.lib とオブジェクト main.exp を作成中

# in_1.txt ... PASSED 843 ms
# in_2.txt ... PASSED 19 ms
# in_3.txt ... PASSED 27 ms
Passed all test cases!!!

C:\Users\nak435\atcoder-workspace\abc400\A>

swift でも使えました。

atcoder-tools test/submit で 'cp932' エラーになる場合
C:\Users\nak435\atcoder-workspace\abc400\A>atcoder-tools test
[Main Program]
compile command:  swiftc main.swift -o main.exe
Compiling... (command: `swiftc main.swift -o main.exe`)
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\Scripts\atcoder-tools.exe\__main__.py", line 7, in <module>
    sys.exit(main())
             ~~~~^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\atcoder_tools.py", line 66, in main
    exit_program(tester_main(prog, args))
                 ~~~~~~~~~~~^^^^^^^^^^^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\tester.py", line 409, in main
    compile_main_and_judge_programs(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        metadata.lang,
        ^^^^^^^^^^^^^^
    ...<2 lines>...
        compile_command=compile_command
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\compiler.py", line 45, in compile_main_and_judge_programs
    _compile(code_filename, exec_filename,
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             compile_command, cwd, force_compile)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\tools\compiler.py", line 30, in _compile
    code, stdout = run_command_with_returncode(compile_command, cwd)
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nak435\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\atcodertools\executils\run_command.py", line 20, in run_command_with_returncode
    return proc.returncode, proc.stdout.decode(locale.getpreferredencoding())
                            ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'cp932' codec can't decode byte 0x88 in position 48: illegal multibyte sequence
decoding with 'cp932' codec failed

UnicodeDecodeError: 'cp932'になる場合は、
↓ こちらの手順にて 「Windowsでのロケール」を変更します。

追伸;

Swift をインストールしたら、Python のデフォルトバージョンがダウングレードしてしまった。
py.iniでデフォルトバージョンを変更することができます。

C:\Users\nak435>python --version
Python 3.9.13

C:\Users\nak435>python3 --version
Python 3.13.5

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?