Home > Blockchain >  Successfully import a module in terminal, but not in VS Code
Successfully import a module in terminal, but not in VS Code

Time:08-11

When I want to import a package import cvxpy, I met a problem as follows.

(py37) E:\amaterials\program\Codefield\CODE_Python> cmd /C "C:\Users\adkad\.conda\envs\py37\python.exe c:\Users\adkad\.vscode\extensions\ms-python.python-2022.12.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher 51686 -- e:\amaterials\program\Codefield\CODE_Python\mathmodel\zhengshuguihua.py "
Traceback (most recent call last):
  File "e:\amaterials\program\Codefield\CODE_Python\mathmodel\zhengshuguihua.py", line 10, in <module>
    import cvxpy as cp
ModuleNotFoundError: No module named 'cvxpy'
Press any key to continue . . . 
(py37) E:\amaterials\program\Codefield\CODE_Python>python
Python 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cvxpy
>>> print(cvxpy.__version__)
1.2.1
>>> ^Z

my launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "python": "D:\\program\\python\\python.exe",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

In VS Code terminal, I can successfully import cvxpy, but when I use Ctrl-F5 to run the code, it shows the cvxpy is not found.

How can I deal with it?

CodePudding user response:

Python 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

"python": "D:\program\python\python.exe",

Python in conda environment is used in the terminal, but is not the same as in launch.json.

You can delete this line "python": "D:\\program\\python\\python.exe", and select the conda environment as the interpreter in vscode.("CTRL SHIFT P" and type "Python: select interpreter").

  • Related