How To Configure Python In Visual Studio Code

1. How To Configure Python Environment In Visual Studio Code.

  1. Open visual studio code and click the extensions option on the left bottom of the main interface. You can also click the View —> Extensions menu item at the top of the visual studio code to open the extensions option panel.
    visual studio code extensions option
  2. Enter the search keyword “python” in the search box and click “Install” to install.
  3. Restart the visual studio code or click the “reload” button in the python extension bottom right corner, then you can use visual studio code to edit and run python script files.

2. How To Create & Run Python Script File In Visual Studio Code.

  1. Open the visual studio code, click File —> New File menu item, input the below python source code into the new file, and save it to a local file ( with .py extension.
    msg = 'I love python'
  2. Right-click the above file content in visual studio code, click the Run Current File in Interactive Window menu item in the popup menu list to run the python script.
  3. For the first time, it will prompt you to install ipykernel, click the Install button to install it, you will see the below output log text in the visual studio code TERMINAL window.
    Executing command in shell >> /Library/Frameworks/Python.framework/Versions/3.7/bin/python /Users/songzhao/.vscode/extensions/ms-python.python-2021.2.636928669/pythonFiles/ pip install -U ipykernel --user
    Collecting ipykernel
      Downloading ipykernel-5.5.0-py3-none-any.whl (120 kB)
         |████████████████████████████████| 120 kB 699 kB/s 
    Collecting jupyter-client
      Downloading jupyter_client-6.1.11-py3-none-any.whl (108 kB)
         |████████████████████████████████| 108 kB 757 kB/s 
    Collecting appnope
      Downloading appnope-0.1.2-py2.py3-none-any.whl (4.3 kB)
    Collecting ipython>=5.0.0
      Downloading ipython-7.21.0-py3-none-any.whl (784 kB)
         |████████████████████████████████| 784 kB 2.1 MB/s 
    Collecting traitlets>=4.1.0
      Downloading traitlets-5.0.5-py3-none-any.whl (100 kB)
         |████████████████████████████████| 100 kB 2.3 MB/s 
    Collecting tornado>=4.2
      Downloading tornado-6.1-cp37-cp37m-macosx_10_9_x86_64.whl (416 kB)
         |████████████████████████████████| 416 kB 540 kB/s 
    Collecting decorator
      Downloading decorator-4.4.2-py2.py3-none-any.whl (9.2 kB)
    Collecting pickleshare
      Downloading pickleshare-0.7.5-py2.py3-none-any.whl (6.9 kB)
    Requirement already satisfied: setuptools>=18.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel) (40.8.0)
    Collecting jedi>=0.16
      Downloading jedi-0.18.0-py2.py3-none-any.whl (1.4 MB)
         |████████████████████████████████| 1.4 MB 1.0 MB/s 
    Collecting pygments
      Downloading Pygments-2.8.1-py3-none-any.whl (983 kB)
         |████████████████████████████████| 983 kB 391 kB/s 
    Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0
      Downloading prompt_toolkit-3.0.16-py3-none-any.whl (366 kB)
         |████████████████████████████████| 366 kB 791 kB/s 
    Collecting pexpect>4.3
      Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
         |████████████████████████████████| 59 kB 897 kB/s 
    Collecting backcall
      Downloading backcall-0.2.0-py2.py3-none-any.whl (11 kB)
    Collecting parso<0.9.0,>=0.8.0
      Downloading parso-0.8.1-py2.py3-none-any.whl (93 kB)
         |████████████████████████████████| 93 kB 126 kB/s 
    Collecting ptyprocess>=0.5
      Downloading ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
    Collecting wcwidth
      Downloading wcwidth-0.2.5-py2.py3-none-any.whl (30 kB)
    Collecting ipython-genutils
      Downloading ipython_genutils-0.2.0-py2.py3-none-any.whl (26 kB)
    Collecting pyzmq>=13
      Downloading pyzmq-22.0.3-cp37-cp37m-macosx_10_9_x86_64.whl (1.0 MB)
         |████████████████████████████████| 1.0 MB 700 kB/s 
    Collecting jupyter-core>=4.6.0
      Downloading jupyter_core-4.7.1-py3-none-any.whl (82 kB)
         |████████████████████████████████| 82 kB 504 kB/s 
    Requirement already satisfied: python-dateutil>=2.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from jupyter-client->ipykernel) (2.8.1)
    Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from python-dateutil>=2.1->jupyter-client->ipykernel) (1.15.0)
    Installing collected packages: ipython-genutils, wcwidth, traitlets, ptyprocess, parso, tornado, pyzmq, pygments, prompt-toolkit, pickleshare, pexpect, jupyter-core, jedi, decorator, backcall, appnope, jupyter-client, ipython, ipykernel
      WARNING: The script pygmentize is installed in '/Users/songzhao/Library/Python/3.7/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
      WARNING: The scripts jupyter, jupyter-migrate and jupyter-troubleshoot are installed in '/Users/songzhao/Library/Python/3.7/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
      WARNING: The scripts jupyter-kernel, jupyter-kernelspec and jupyter-run are installed in '/Users/songzhao/Library/Python/3.7/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
      WARNING: The scripts iptest, iptest3, ipython and ipython3 are installed in '/Users/songzhao/Library/Python/3.7/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed appnope-0.1.2 backcall-0.2.0 decorator-4.4.2 ipykernel-5.5.0 ipython-7.21.0 ipython-genutils-0.2.0 jedi-0.18.0 jupyter-client-6.1.11 jupyter-core-4.7.1 parso-0.8.1 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.16 ptyprocess-0.7.0 pygments-2.8.1 pyzmq-22.0.3 tornado-6.1 traitlets-5.0.5 wcwidth-0.2.5
  4. Visual studio code will run your python script in a Jupyter kernel, so it needs to install the above ipykernel.
  5. After installing the ipykernel, you can see the python script output in a newly created interactive window in visual studio code.
  6. When you right-click the python source file content, there are a lot of menu items in the popup menu list, you can run them one by one to test its functions. Some menu items provide the debug function such as Run to Line In Interactive Window etc.
  7. If you want to run the python script file in a terminal, just right-click the python file, and select the Run Python File in Terminal menu item in the popup menu list. Then you can see the python output in the visual studio code TERMINAL window at the bottom.

3. How To Debug Python Script File In Visual Studio Code.

  1. Single-click the beginning of each python source code line to set a breakpoint.
  2. Click the Run —> Start Debugging menu item at the top menu bar.
  3. Select the Python File item in the popup drop-down list.
  4. It will open the RUN AND DEBUG panel on the left side of the visual studio code and will stop at the breakpoint which you set before.
  5. Click the Step Over, Step Into, Step Out button icon to debug your python script source code.
  6. Hover your mouse on the variable name to display the variable value.
  7. You can also select the variable name and right-click it, then click the Add to Watch menu item in the popup menu list to add the variable to the WATCH panel to see its value.
  8. If you want to stop debugging you can click the Run —> Stop Debugging menu item.
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x