Visual Studio Code Python Configuration Error

Viewed 32

Recently I started working on a project with other people. Part of the project is made on Python, and when I try executing it I get the following error:

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

After talking with some of my partners, it looks like I am the only one getting this error, the code works for everyone else except for me. Having this in count I guess is a problem with my Visual Studio Code, since we all work with editor and on Windows 10.

I made a small, simple project where I get this same error with two small scripts, the project follows this structure:

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

And the scripts are the following:

short.py

import sys

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

from driver import *

print_on_console('Hello world!')

driver.py

def print_on_console(message):
    print(message)

So basically I get this error when trying to import the function "print_on_console" into short.py.

I don't know if it is necessary but this is my settings.json form Visual Studio Code:

{
  "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"
}

As extensions, the related to Python ones that I am currently using are:

  • Jupyter
  • Jupyter Keymap
  • Jupyter Notebook Renderers
  • Pylance
  • Python

Thanks in advance to anyone who tries to help me with this issue!

1 Answers

Are you running in a venv or do you have the packages installed globally? If you are running in a venv you probably need to add the venv path in VS Code

enter image description here

Select this in the bottom right corner of VS Code then

enter image description here

Select Enter Interpreter path and select the location of the python.exe file from your venv Then if you run via VS Code it will launch into the venv automatically

Related