Setting Up Your Python Development Environment with GitHub Codespaces

Setting up a robust development environment is the first step toward a productive and efficient coding experience. This guide will walk you through the process of setting up your Python development environment using GitHub Codespaces and Visual Studio Code (VS Code).

Step 1: Accessing the Course Repository

To get started, you’ll need access to the course examples hosted in a GitHub repository. The repository is organized into two main folders:

  1. Finished Folder: Contains the final versions of all code examples.
  2. Start Folder: Contains the initial versions of the code examples, which you’ll work on throughout the course.

You can find the repository at the link provided in your course materials.

Step 2: Working with the Repository Locally

If you prefer to work locally on your computer, follow these steps:

  1. Clone the Repository:

    • Navigate to the GitHub repository.
    • Click on the “Code” button.
    • Choose to clone the repository using your preferred method (e.g., HTTPS, SSH, or GitHub CLI).
  2. Download the Repository:

    • Alternatively, you can download a zip file of the repository.
    • Extract the contents to your desired location on your computer.
  3. Set Up Your Local Environment:

    • Ensure that you have Python installed (at least version 3.10).
    • Open your favorite code editor (e.g., VS Code) and start working on the examples.

Step 3: Working with GitHub Codespaces

For an even more streamlined setup, you can use GitHub Codespaces, which provides a cloud-based development environment with VS Code:

  1. Fork the Repository:

    • Fork the course repository into your own GitHub account.
  2. Create a Codespace:

    • Navigate to the Codespaces menu in your GitHub repository.
    • Click the plus sign (+) to create a new codespace.
  3. Wait for the Codespace to Spin Up:

    • The creation process might take a minute. Once ready, you will have a browser-based version of VS Code.

Step 4: Setting Up the Codespace

  • Access the Files:

    • In the left-hand navigation panel, you’ll find the Finished and Start folders.
  • Open the Terminal:

    • Navigate to the terminal via View > Terminal or by pressing Ctrl+Backtick.
  • Check Python Installation:

    • Verify Python is installed by typing python --version in the terminal. You should see a version like 3.10.8.
  • Install Python Extension:

    • Ensure you have the Python extension installed. Click on the Extensions icon and search for “Python”. Install it if it’s not already installed.
  • Check Preferences:

    • Open the preferences by typing Ctrl+, or navigating to File > Preferences > Settings.
    • Search for “execute” in the settings.
    • Ensure that “Execute in File Dir” is checked.

Example: Running a Python Script in GitHub Codespaces

Let’s run a simple Python script to ensure everything is set up correctly.

  1. Create a New Python File:

    • In the Start folder, create a new file named example.py.
  2. Write Your Python Code:

				
					def greet(name):
    return f"Hello, {name}!"

if __name__ == "__main__":
    name = input("Enter your name: ")
    print(greet(name))

				
			

Run the Script:

  • Open the terminal.
  • Ensure you’re in the directory containing example.py.
  • Run the script by typing python example.py and pressing Enter.
  • Enter your name when prompted and see the greeting output.

Conclusion

Setting up your development environment using GitHub Codespaces and Visual Studio Code is a straightforward process that ensures you can start coding without any hassle. Whether you prefer working locally or in the cloud, these steps will help you get your environment ready, allowing you to focus on learning and coding. Happy coding!