Skip to Content
Course content

6: Setting Up VS Code for Python Development

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I once worked with a junior dev who spent an entire Friday afternoon fighting a ghost. He had installed the requests library via his terminal, and he could see it sitting there in his site-packages. But every time he hit "Run" in VS Code, the editor screamed ModuleNotFoundError: No module named 'requests'. He was literally staring at a successful installation message in one window and a crash report in another. The culprit? He had three different versions of Python installed on his machine, and VS Code was pointing to a version of Python that didn't have the library installed. It's a rite of passage, but it's a massive waste of time.

Picking the Right Python Interpreter

The most important thing to understand is that VS Code isn't a Python compiler; it's a text editor that needs to be told which Python "engine" to use to run your code. This is called the Interpreter. If you're using virtual environments—which you should be—this is where things usually go sideways.

To fix this, you don't dig through deep menu settings. Instead, use the Command Palette by hitting Ctrl+Shift+P (or Cmd+Shift+P on Mac) and typing "Python: Select Interpreter". This opens a list of every Python version your computer can find. I always recommend looking for the one labeled ('venv') or ('env') if you've created a local environment for your project. Once you select it, you'll see the version pinned in the bottom-right corner of your status bar. If that corner says the wrong version, your code will behave unpredictably, no matter how correct your logic is.

Leveraging Pylance for Real-Time Feedback

Writing code without a language server is like driving in the dark without headlights. You won't know you've hit a wall until you actually crash (or, in our case, run the script and get a SyntaxError). This is why the Microsoft Python extension—and specifically the Pylance engine it includes—is non-negotiable.

Once installed, Pylance provides "IntelliSense." If you have a variable named user_profile_data and you start typing user_pro..., it will autocomplete the name for you. More importantly, it provides static type checking. If you try to call a method that doesn't exist on a string, Pylance will put a yellow or red squiggly line under the code before you ever hit the run button. Trust those squiggly lines; they save me hours of debugging every single week.

Breaking the Print-Statement Habit with the Debugger

For a long time, my only way of debugging was peppering my code with print("Got here 1") and print(f"Variable x is {x}"). It's a clumsy way to work. VS Code has a built-in debugger that lets you literally freeze time.

By clicking to the left of the line number, you can set a "breakpoint" (a red dot). When you run your code in Debug mode (F5), the program will pause exactly at that dot. You can then hover your mouse over any variable to see its current value or use the "Debug Console" to run commands against the live state of your program. It's essentially like being able to perform surgery on your code while it's still breathing.




📋 Practical Task

Exercise: Configuring a Dedicated Environment for a Weather Script

Instead of using your global Python installation, you are going to set up a project-specific environment and verify that VS Code is communicating with it correctly. Follow these requirements:

  • Create a new folder named weather_app and open it in VS Code.
  • Open the integrated terminal (Ctrl+`) and create a virtual environment using python -m venv .venv.
  • Use the Command Palette (Ctrl+Shift+P) to set the Python Interpreter to the one located inside your .venv folder.
  • Install the requests library specifically within that environment using the terminal.
  • Create a file named main.py and write a script that imports requests and prints the status code of a request to https://www.google.com.
  • Set a breakpoint on the line where the request is made and run the debugger (F5). Verify that you can see the response object in the "Variables" pane before the program finishes.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.