gpt4 book ai didi

python-2.7 - VS Code 调试总是在 Python 的内部模块上给出 ImportError

转载 作者:行者123 更新时间:2023-12-03 19:37:39 24 4
gpt4 key购买 nike

我回答了我自己的问题,请参阅下面的解决方案。将这一切留给后代,因为我做了很多其他人说会工作并且为他们工作的事情,但没有一个工作。

问题

下面是一个非常简单的python文件。当试图在这个文件上使用 Visual Studio Code 的调试器时,我总是得到一个 ImportError在任何内部模块上,例如 app.modelsapp.tests.scripts.stubs .如果这些模块在下面列出 faker , 或任何其他外部模块,调试器将通过外部模块而不抛出错误,但会在 app.models 上抛出错误或首先列出的任何内部模块。这在我尝试过的 8-10 个文件中是正确的。

我试过的

我已经在 launch.json 和 User Settings 中指定了确切的 pythonPath,如下所示。我已添加 "exceptionHandling"忽略 ImportErrors。我已经卸载并重新安装了 Don Jayamanne 出色的 Python 扩展。我已经运行了 Python: Select Workspace Interpreter并将其指向./.venv/bin/python2.7 , ./.venv/bin/python2 ,和``./.venv/bin/python`。

文件

launch.json Python 配置

    "name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "/Users/REDACTED/REDACTED/.venv/bin/python2.7",
"program": "${file}",
"cwd": "${workspaceRoot}",
"env": null,
# we have 10+ environmental shell scripts, so I can't really use "envFile" in
# a meaningful way, though I have tried pointing it at one of the shell scripts
# but was still unable to debug.
"envFile": "${workspaceRoot}/.venv",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
],
"exceptionHandling": {
"ignore": [
"ImportError"
]
}
},

python 文件
from app.models import Kelly
import app.tests.scripts.stubs
from faker import Faker
import factory
from datetime import datetime
from bson.objectid import ObjectId

fake = Faker()

kelly_names = [REDACTED]


class KellyFactory(factory.Factory):
class Meta:
model = Kelly
id = ObjectId()
is_archived = False
email = factory.LazyAttribute(
lambda kel: '%s@email.com' % kel.name.split()[0])
name = fake.word(ext_word_list=kelly_names)
phone = fake.phone_number()
date_last_modified = datetime.now()


kelly = KellyFactory()

最佳答案

解决方案

在浏览我所做的一切时,为了发布这个问题,我尝试更改路径 program来自 "${file}"到它的绝对路径。这使得调试器可以在没有错误的情况下超越导入。然后我遇到了一个问题,因为我不在任何环境中,所以我输入了 dev我的机器上的环境,尽管我不必在 VSCode 中更改任何其他内容。但是,我现在有一个 unverified breakpoint问题,并正在追查该问题。一旦我弄清楚了就会更新。

我尝试解决的未验证断点问题

我改了"program"返回 "${file}" ,并且调试工作完全符合预期,断点工作,像这样的 super 简单文件

import sys
print(sys.version)
print(sys.executable)

然而,这会导致 ImportError再次使用更复杂的文件,例如原始问题中上面列出的 Python 文件,尽管 "exceptionHandling"launch.json 中指定.

关于python-2.7 - VS Code 调试总是在 Python 的内部模块上给出 ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45941526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com