Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Setting Up a Python Development Environment

Recommended resources

Welcome to your first lesson! This course is designed to build your skills from the ground up, starting with the essential tools of the trade and culminating in the creation of your own AI-powered chatbot. To begin that journey, this lesson focuses on setting up a professional development environment.

Just as a craftsperson organizes their workshop before starting a project, a software engineer sets up their digital workspace. Today, you will learn how to configure the three key components of this workspace: a code editor, the Python interpreter, and an isolated project environment. Mastering this setup is the first and most critical step toward building reliable, complex applications like the chatbot you'll develop for your portfolio.

Your Digital Workshop: The Core Components

Before we start installing software, let's clarify what a development environment is and what pieces it includes. For our purposes, we need three things to work together seamlessly:

  1. A Code Editor: This is the application where you will write, read, and manage your code. We will use Visual Studio Code (VS Code), a powerful and popular editor in the software industry.
  2. A Python Interpreter: This is the program that actually understands and executes the Python code you write. The editor is where you write the instructions; the interpreter is what carries them out.
  3. The Python Extension for VS Code: This is a plugin for VS Code that acts as a bridge, connecting the editor to the interpreter. It adds Python-specific features like code completion, error checking, and debugging tools directly into your editor.

The resource below from the official VS Code documentation explains this relationship well.

Getting Started with Python in VS Code

Please read the introductory section of this guide. It provides a clear overview of the three components we've just discussed.

Focus on the section Understand the Python setup. This will solidify your understanding of how the editor, extension, and interpreter interact.


Installation and Initial Setup

Now it's time to install these components. The process involves installing Python itself, then installing VS Code, and finally installing the Python extension from within VS Code.

The following resources provide detailed, step-by-step instructions for your specific operating system (Windows, macOS, or Linux).

Getting Started with Python in VS Code

This part of the VS Code documentation guides you through installing a Python interpreter.

Follow the instructions under the heading Install a Python interpreter. Be sure to follow the specific steps for your operating system (Windows, macOS, or Linux). At the end, you should be able to run a command in your terminal to verify that Python was installed correctly.

For a visual walkthrough of installing both Python and VS Code, the following video is very helpful. It also shows you how to install the necessary Python extension inside VS Code.

How to set up Python on Visual Studio Code

This video from Kenny Yip Coding provides a clear, step-by-step guide for setting up Python and VS Code.

Watch the following segments: Installing Python: The first part shows how to check for an existing Python version and install it from python.org. Installing VS Code: The next segment covers downloading and installing the editor. Installing the VS Code Extension: Later in the video, watch this part to see how to find and install the official Python extension from Microsoft. You can ignore the part about "Code Runner" for now.

After completing these steps, you will have the fundamental tools ready. The next crucial concept is ensuring your projects remain organized and independent.


Project Isolation: The Why of Virtual Environments

As you build more projects, you'll rely on third-party packages—collections of pre-written code that solve common problems. For example, one project might need version 2 of a data analysis package, while a second, newer project requires version 3. If you install these packages into a single, global location, you create a conflict; you can only have one version installed at a time. This makes it impossible to work on both projects on the same machine.

To solve this, professional developers use virtual environments. A virtual environment is an isolated, self-contained directory that holds all the specific packages and a specific Python interpreter version for a single project.

Think of it like having a separate, dedicated toolbox for every project you work on. This prevents tools from one project from getting mixed up with another, ensuring that each project has exactly the versions of the tools it needs.

The following article explains the problems that virtual environments solve in more detail.

Python Virtual Environments: A Primer – Real Python

This article from Real Python provides an excellent explanation of why virtual environments are a best practice.

Read the section titled Why Do You Need Virtual Environments?. Pay close attention to the concepts of "system pollution," "dependency conflicts," and "reproducibility issues." The example with two different Django versions clearly illustrates the core problem.


Creating Your First Virtual Environment

Now that you understand why virtual environments are essential, let's walk through how to create and use one for a project. We will do this from within VS Code using its integrated terminal.

The standard tool for creating virtual environments is a built-in Python module called venv. The process is as follows:

  1. Create a folder for your project.
  2. Open this folder in VS Code.
  3. Open a terminal inside VS Code.
  4. Run a command to create the virtual environment inside your project folder.
  5. Activate the environment to start using it.

The following video demonstrates this exact workflow.

How To Setup A Virtual Environment For Python In Visual Studio Code In 2023

This short video from Indently shows the complete process of creating and activating a virtual environment within VS Code.

Watch the video from the beginning. It will guide you through: Creating a project folder and opening it in VS Code (project setup). Understanding the problem with installing packages without an environment (the 'pip' issue). Creating the virtual environment using the venv command (creating the environment). Note the slight difference for Mac/Linux (python3) vs. Windows (python). Activating the environment and installing a package (activation and installation). Notice how the terminal prompt changes after activation to show the name of the active environment.

To reinforce this, the "Python Virtual Environments: A Primer" article provides the specific commands in text form. This is useful to have as a reference.

Python Virtual Environments: A Primer – Real Python

This resource provides the command-line instructions for creating and managing virtual environments, and shows how to connect them to your editor.

First, review the core commands in the section How Can You Work With a Python Virtual Environment?. Focus on the "Create It" and "Activate It" subsections. Don't worry about installing packages just yet; we'll cover that in a future lesson. Next, read the section How Do You Enable a Venv in Your IDE?. This explains how VS Code automatically detects the virtual environment you created and how you can manually select it using the "Python: Select Interpreter" command if needed. This step ensures that when you run code from VS Code, it uses the isolated environment you just set up.

By completing these steps, you now have a fully configured, professional development environment for your first Python project.


Conclusion

In this foundational lesson, you have successfully configured your digital workshop. You learned about the essential roles of the code editor (VS Code) and the Python interpreter, and you installed them. Most importantly, you understood the critical need for project isolation and learned how to create and activate a virtual environment using venv. This setup prevents dependency conflicts and is a standard practice for all professional Python developers.

You are now fully prepared to start writing and running code. In the next lesson, we will use the environment you just created to execute your first Python statements and scripts, taking your first active step into the world of programming.