简体   繁体   中英

vsc python debugger and pylance doesn't recognize installed package

I installed snowflake-connector-python in my virtual environment recently using pip install snowflake-connector-python[pandas]==2.7.6 , and it works ok when I run my script from the command line with the virtual environment activated. I set the same virtual environment as VSC interpreter, but trying to run python debugger raises an exeception:

ModuleNotFoundError: No module named 'snowflake.connector'

I also can see that pylance doesn't recognize the package either, but all other installed packages are recognized with no problem.

I have seen several questions and answers for similar cases, but none of them have worked for me.

EDIT

I added the parameter python to the launch.json :

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "python": "${workspaceFolder}/MyVenv/Scripts/python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "--env", "dev",
                "--argument"
            ],
            "envFile": "${workspaceFolder}/.env",
        }
    ]
}

Getting the same result.

EDIT

I found that the way the module is imported makes pylance to recognize it, but debugger is still raising the error.

This is the old and new way of importing the module into the code:

Old way

在此处输入图像描述

New way

在此处输入图像描述

With the new way, code is not highlighted by pylance , but it is not colored as recognized, though.

Usually, when your module can't be found, your environment hasn't been activated.

You can try adding the following code to your settings.json after choosing the python interpreter:

"python.terminal.activateEnvironment": true

I had a similar issue with VSCode as well. What I did is uninstalled the Python extension, reinstalled it and then installed Create Blank Python Module extension (all from Visual Studio Marketplace, of course)

Hope this helps

The problem was finally solved by doing this:

  1. Delete the python virtual environment.
  2. Create the python virtual environment again.
  3. Activate the new python virtual enviroment.
  4. Installing all libraries again with python -m pip install -r requirements.txt

I had this problem on mac, it was caused by the interpreter in the vs code Python settings being set to the string "python" instead of the string "python3" (on my system running "python" gives an unknown command error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM