Getting Started: Setting Up Python and Libraries
This lesson is designed to guide complete beginners through the setup process to install Python and any necessary tools for the upcoming lessons.
Step-by-Step Guide to Installing Python
- Download Python:
Visit python.org/downloads and download the latest version of Python for your operating system.
- Run the Installer:
Open the downloaded file and follow the prompts to install Python. Make sure to check the box that says Add Python to PATH during installation.
- Verify the Installation:
Open a command prompt (Windows) or terminal (Mac/Linux)
- Windows: Press Win + R to open the Run dialog, type
cmd, and press Enter. - Mac: Press Command + Space to open Spotlight, type
Terminal, and press Enter. - Linux: Press Ctrl + Alt + T to open the terminal.
In the command prompt or terminal window type:
python --version
If Python is installed correctly, it will show the version number.
Tip: Press Enter to run the code in the command prompt or terminal window.
- Windows: Press Win + R to open the Run dialog, type
- Install Pip:
Pip is Python's package installer. Type the following command in your command prompt or terminal to ensure pip is installed:
python -m ensurepip --upgrade
Lesson 1: Introduction to Sound and Python
Objective: Understand the basics of sound processing in Python.
Before You Begin: Make sure you have completed the Getting Started: Setting Up Python and Libraries lesson.
Install Necessary Libraries
In your command prompt or terminal, type the following to install required libraries:
pip install pydub numpy
What’s Happening: PyDub is a library used for audio processing, and NumPy helps with mathematical computations.
Note: If you see a message indicating you need to upgrade pip, type the following command and press Enter:
python -m pip install --upgrade pip
This will upgrade pip to the latest version, allowing you to proceed with installing libraries.
Getting Started: Downloading and Installing Visual Studio Code
This guide will help you download, install, and open Visual Studio Code (VS Code), a popular code editor we’ll use for writing and running Python code.
Step-by-Step Guide to Downloading and Installing Visual Studio Code
- Download Visual Studio Code:
Visit https://code.visualstudio.com/download and select the correct version for your operating system (Windows, macOS, or Linux).
- Install Visual Studio Code:
Once downloaded, open the installer file and follow the prompts to install VS Code. During installation:
- Windows: Run the downloaded
.exefile and check the box to "Add to PATH" if prompted, then complete the installation. - macOS: Open the
.dmgfile, drag VS Code to your Applications folder, and complete the installation. - Linux: Follow the specific steps provided on the Visual Studio Code website for your Linux distribution.
- Windows: Run the downloaded
- Opening Visual Studio Code
- Windows: Search for "Visual Studio Code" in the Start menu and click to open it.
- macOS: Open your Applications folder, find Visual Studio Code, and double-click to open.
- Linux: Open your applications menu and search for "Visual Studio Code," or type
codein your terminal if set up to launch from the command line.
Tip: Once you open VS Code, you can start a new file by selecting File > New File, then Save As with a .py extension, like sound_test.py.
Creating a Simple Sound File
Open Visual Studio Code (VS Code), where you’ll write and run the code. Follow these steps:
- Open VS Code.
- Create a new file by selecting File > New File, then Save As with a
.pyextension, likesound_test.py.Tip: You can name the file anything you like, just remember what it is!
- Copy and paste (or type) the following code into the new file:
from pydub import AudioSegment
sound = AudioSegment.silent(duration=1000) # Creates a 1-second silent sound
What’s Happening: This code creates a 1-second silent audio file.
To run your code, open the terminal in VS Code by selecting View > Terminal and typing:
python sound_test.py
Tip: Don’t forget to save your file before running it!
Challenge: Modify the duration to create a longer or shorter silent audio file.
Did you have issues? See troubleshooting here.