Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Code run with -c or a "code" launch gets __name__ == "builtins" instead of "__main__"

オープン
#2,079 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@Om-singhaI がすでに取り組んでいます。

2026年10月6日 から。

  • #2080 @Om-singhaI による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

Start with run_code() in src/debugpy/server/cli.py and the CLI tests in tests/debugpy/server/test_cli.py; compare its execution setup with run_file() using runpy.run_path(). Run the focused CLI tests and reproduce with python3 -m debugpy --listen 127.0.0.1:0 -c "$CODE". Done when code runs with __name__ == "__main__", its main block runs, and the reported pickling failure is covered by a test.

索引モデルが issue の本文から書いたものです。

説明

Before creating a new issue, please check the FAQ to see if your question is answered there.

Environment data

  • debugpy version: 1.8.22+3.g751502d (running from source at 751502d, current main)
  • OS and version: macOS 26.6.2 (25G83)
  • Python version (& distribution if applicable, e.g. Anaconda): CPython 3.10.6 from python.org, and 3.14.3 from Homebrew
  • Using VS Code or Visual Studio: neither. I used the CLI, and also a "code" launch sent straight to the adapter.

Actual behavior

Code passed with -c, or with "code" in a launch configuration, doesn't run as __main__. __name__ is "builtins", so an if __name__ == "__main__": block is skipped, and a class the code defines can't be pickled:

__name__ = builtins
pickle failed: Can't pickle <class 'Point'>: attribute lookup Point on builtins failed

That is from 3.10.6. 3.14.3 gives the same result, with the pickle error worded as it's not found as builtins.Point.

run_code() in src/debugpy/server/cli.py ends with eval(code, {}). That globals dict has no __name__, so the name is looked up in builtins and comes back as "builtins". A class the code defines gets that as its __module__, which is where pickle then looks for it. A file target doesn't have this problem, because run_file() uses runpy.run_path(target, run_name="__main__"), which puts a temporary __main__ module in place.

A "code" launch goes through the same function, since the adapter turns it into a -c argument for the debugpy CLI in the debuggee. With "code": ["print('__name__ is', __name__)", "if __name__ == '__main__':", " print('main block ran')"] and "console": "internalConsole", the debuggee prints __name__ is builtins and nothing else.

Expected behavior

The same as python -c:

__name__ = __main__
main block ran
pickle ok

Steps to reproduce:

CODE='import pickle
class Point: pass
print("__name__ =", __name__)
if __name__ == "__main__":
    print("main block ran")
try:
    pickle.dumps(Point()); print("pickle ok")
except Exception as e:
    print("pickle failed:", e)'

python3 -c "$CODE"
python3 -m debugpy --listen 127.0.0.1:0 -c "$CODE"

The first command prints the expected output above and the second prints the actual output. Nothing needs to attach; the code runs right away because --wait-for-client isn't given.

I have a fix and can open a PR. It runs the code in a temporary __main__ module, the same way run_path() does for a file, and adds a test to tests/debugpy/server/test_cli.py.

主要言語
Python
スター
2.5k
フォーク
205
平均マージ
1日 9時間
マージ済み PR(30日)
3

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/debugpy のほかの issue

microsoft/debugpy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。