如何在 VS Code 中为自定义模块设置导入? [英] How do I set up imports for custom modules in VS Code?

查看:159
本文介绍了如何在 VS Code 中为自定义模块设置导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的(工作/可执行)项目从 PyCharm 迁移到 VS Code.我的文件夹结构如下(简化):

I am trying to bring my (working/executable) project from PyCharm to VS Code. My folder structure looks like this (simplified):

root
|- .venv
|- src
 |- helper
 |- windows
  |- main
   |- __init__.py
   |- main_window.py
 |- __init__.py

我正在尝试执行 src/__init__.py 文件,其中包含以下代码:

I'm trying to execute the src/__init__.py file, which has the following code:

from PyQt5.QtWidgets import QApplication
from src.windows.main import MainWindow
import sys


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

这在 PyCharm 中完美运行,但 VS Code 给了我以下错误:

This works perfectly in PyCharm, but VS Code gives me the following error:

ModuleNotFoundError: No module named 'src'

我从互联网上尝试了许多解决方案(Stack Overflow 和官方 VS Code 文档),包括修改 settings.json 和 launch.json.目前我的 JSON 如下所示:

I've tried numerous solutions from the internet (Stack Overflow and official VS Code documentation), including modifying settings.json and launch.json. Currently my JSONs look like this:

launch.json:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "${config:python.pythonPath}",
            "env": {
                "PYTHONPATH": "${workspaceFolder}\\src"
            }
        }
    ]
}

settings.json(用户):

settings.json (User):

{
    "workbench.iconTheme": "Monokai Classic Icons",
    "editor.fontSize": 16,
    "editor.letterSpacing": 1,
    "editor.lineHeight": 27,
    "editor.fontFamily": "'Roboto Mono', monospace",
    "editor.fontWeight": "300",
    "workbench.colorTheme": "Monokai Classic",
    "window.zoomLevel": 0,
    "terminal.integrated.fontFamily": "'Inconsolata', monospace",
    "terminal.integrated.fontSize": 18,
    "python.autoComplete.addBrackets": true,
    "bracket-pair-colorizer-2.colors": [
        "White"
    ]
}

settings.json(工作区):

settings.json (Workspace):

{
    "python.pythonPath": "c:\\Users\\username\\PycharmProjects\\Snake_It_Off\\.venv\\Scripts\\python.exe",
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.pycodestyleEnabled": true
}

但是,我仍然收到错误消息.这是 VS Code 的问题吗?在 PyCharm 中,我可以将几个文件夹标记为源(并且它会自动检测任何包含 __init__.py 作为模块的文件夹).如何正确设置项目以使其能够运行?

However, I'm still getting the error. Is this a VS Code issue? In PyCharm, I could just mark several folders as sources (and it automatically detected any folder containing __init__.py as a module). How do I correctly set up the project to be able to run it?

推荐答案

我终于设法解决了我的问题.我不得不将我的主要可执行文件 src/__init__.py 移到 src 文件夹之外,因为使用包 可能导致问题.另一个重要步骤是通过运行以下代码来仔细检查我的 PYTHONPATH 设置是否正确:

I finally managed to solve my problem. I had to move my main executable file, src/__init__.py, outside of the src folder, since executing a script from withing a package can cause problems. Another important step was double-checking that my PYTHONPATH is set correctly by running the following code:

import sys

print(sys.path)

这篇关于如何在 VS Code 中为自定义模块设置导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆