簡體   English   中英

Visual Studio 代碼 Python 配置錯誤

[英]Visual Studio Code Python Configuration Error

最近我開始和其他人一起做一個項目。 該項目的一部分是在 Python 上制作的,當我嘗試執行它時,我收到以下錯誤:

File "D:\projects\company\spiders\country\spain\cities\runnables\short.py", line 22, in <module>
    soup = get_soup_from_url(url, "html.parser")
NameError: name 'get_soup_from_url' is not defined

在與我的一些合作伙伴交談后,看起來我是唯一遇到此錯誤的人,該代碼適用於除我之外的其他所有人。 我想這是我的 Visual Studio Code 的一個問題,因為我們都使用編輯器和 Windows 10。

我做了一個小而簡單的項目,我在兩個小腳本中遇到了同樣的錯誤,項目遵循以下結構:

- spiders
   - country
      - spain
         - cities
            - runnable
               - short.py
   - utils
      - driver.py

腳本如下:

簡短的.py

import sys

sys.path.append('../../../../utils')

from driver import *

print_on_console('Hello world!')

驅動程序.py

def print_on_console(message):
    print(message)

所以基本上我在嘗試將 function "print_on_console" 導入 short.py 時收到此錯誤。

我不知道是否有必要,但這是我的設置。json 來自 Visual Studio 代碼:

{
  "terminal.integrated.tabs.enabled": true,
  "security.workspace.trust.untrustedFiles": "open",
  "editor.fontSize": 18,
  "workbench.colorTheme": "Oceanic Next (dimmed bg)",
  "tabnine.experimentalAutoImports": true,
  "editor.guides.bracketPairs": true,
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git Bash": {
      "source": "Git Bash"
    },
    "Ubuntu-20.04 (WSL)": {
      "path": "C:\\Windows\\System32\\wsl.exe",
      "args": ["-d", "Ubuntu-20.04"]
    },
    "Ubuntu (WSL)": {
      "path": "C:\\Windows\\System32\\wsl.exe",
      "args": ["-d", "Ubuntu"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "PowerShell"
}

作為擴展,我目前使用的與 Python 相關的是:

  • 木星
  • Jupyter 鍵盤映射
  • Jupyter 筆記本渲染器
  • 皮蘭斯
  • Python

提前感謝任何試圖幫助我解決這個問題的人!

您是在 venv 中運行還是在全球范圍內安裝了軟件包? 如果您在 venv 中運行,您可能需要在 VS Code 中添加 venv 路徑

在此處輸入圖像描述

Select 這個在 VS Code 的右下角 然后

在此處輸入圖像描述

Select Enter Interpreter path和 select venv 中python.exe文件的位置 然后,如果您通過 VS Code 運行,它將自動啟動到 venv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM