💻

Setting Up Python with Visual Studio Code

Jul 8, 2024

Setting Up Python with Visual Studio Code

Introduction

  • Many use PyCharm or Anaconda with Spider for Python.
  • Preferred choice: Visual Studio Code (VS Code).
  • VS Code is more convenient for working with multiple languages.

Installing Python

  • Open terminal/command prompt.
  • Check Python version: python --version or py --version.
  • If Python is not recognized: download from python.org.
    • Go to Downloads > Download for your OS (Windows, Mac, or Linux).
  • Install Python:
    • Click Install Now.

Installing Visual Studio Code

  • Go to code.visualstudio.com.
  • Make sure to download the blue version (VS Code) not purple (Visual Studio).
  • Download for your OS.
  • Install VS Code:
    • Accept agreement.
    • Create a desktop icon.
    • Click Install.

Setting up a Python Project in VS Code

  • Open VS Code.
  • Create a new file (Ctrl+S).
  • Save file with .py extension (e.g., test.py).
  • Example content for test.py: print("This is file 1") print(50)
  • Create another file test2.py: print("This is file 2") print(100)

Running Python Code in Terminal

  • Open terminal in VS Code.
  • Navigate to file directory:
    • cd Desktop
    • python test.py or py test.py
  • Example outputs:
    • For test.py: This is file 1 50
    • For test2.py: This is file 2 100

Using VS Code Terminal and Extensions

  • Use the built-in terminal or run button to execute code.
  • To enable run button:
    1. Go to Extensions panel.
    2. Install Python extension by Microsoft.
    3. Install Code Runner extension.
  • Setting Code Runner to use terminal:
    • Gear icon > Settings.
    • Scroll to Actions on Save > Edit in settings.json.
    • Add: "code-runner.runInTerminal": true

Organizing Your Project

  • Create a folder (e.g., python_files).
  • Move Python files into this folder.
  • Drag folder into VS Code to open it.
  • Alternatively, use terminal to open folder in VS Code:
    • cd Desktop
    • cd python_files
    • code .

Conclusion

  • Follow these steps to set up Python with VS Code.
  • Like and subscribe for more tutorials.