Previous | Tutorial index | Next

Tutorial 9: Installing Python and Python IDEs

Learning Objective

To be able to install Python and required Python IDEs on the computer. Get Python and Python IDEs running for the learning activities included in this course.

9.1 Introduction: Setting Up Your Digital Workspace

Before you can write your first line of Python code, you need to set up your programming environment. This is the first practical hurdle every developer faces. Unlike many tutorials that gloss over installation issues, this guide provides detailed, step-by-step instructions for Windows, macOS, and Linux. More importantly, you will learn why certain steps (like adding Python to the PATH) are critical, and you will set up an Integrated Development Environment (IDE) that will make your coding journey smoother. By the end of this tutorial, you will be ready to write and run Python scripts on your own machine.

9.2 Installing Python: Platform-Specific Guides

Python is cross-platform, but the installation process differs across operating systems. Follow the guide for your specific OS.

9.2.1 Windows Installation

  1. Download the Installer:

  2. Run the Installer:

  3. UAC Prompt:

  4. Verification:

9.2.2 macOS Installation

macOS usually comes with an older version of Python 2.7 pre-installed for system use. Do not uninstall that version, as it is required by macOS itself. We will install a modern Python 3 separately.

Option A: Official Installer (Easiest)

Option B: Homebrew (For developers)

Verification:

9.2.3 Linux Installation

Most modern Linux distributions (Ubuntu, Debian, Fedora) come with Python 3 pre-installed. However, you may need to install the full toolchain including pip and venv.

Ubuntu/Debian (APT):

sudo apt update sudo apt install python3 python3-pip python3-venv

Fedora/RHEL (DNF):

sudo dnf install python3 python3-pip

Arch (Pacman):

sudo pacman -S python python-pip

Verification:

9.3 Understanding the PATH Environment Variable

When you type python in the command prompt/terminal, the operating system needs to know where the python.exe (or python3) file is located. It searches through all the directories listed in the PATH environment variable.

9.4 Integrated Development Environments (IDEs)

A text editor lets you write code; an IDE (Integrated Development Environment) helps you write, debug, run, and manage it. IDEs provide features like syntax highlighting, auto-completion, debugging tools, and integrated terminals. We will set up three options, but VS Code is highly recommended for this course.

  1. Download: Go to code.visualstudio.com and download the installer for your OS.
  2. Install: Run the installer.
  3. Install the Python Extension:
  4. Select the Python Interpreter:
  5. Write and Run Code:

9.4.2 PyCharm Community Edition

9.4.3 Thonny – For Absolute Beginners

Thonny is incredibly simple and comes bundled with Python, making it the easiest to start with.

9.5 The Integrated Terminal

Regardless of the IDE you choose, you will need to use the terminal/command prompt occasionally.

9.6 Package Management with pip

9.6.1 What is pip?

pip stands for "Pip Installs Packages." It is the package installer for Python. You use it to install third-party libraries from PyPI.

9.6.2 The Problem: Global Packages

If you install all packages globally (pip install numpy), you will eventually run into issues: Project A needs numpy version 1.2, but Project B needs version 1.3. They conflict.

9.6.3 The Solution: Virtual Environments (venv)

A virtual environment is an isolated Python environment for a specific project. It has its own Python interpreter and its own pip directory.

How to create a virtual environment:

  1. Open the terminal in your project folder.

  2. Run:

    python -m venv myenv

    (Replace myenv with your desired environment name, e.g., venv).

  3. Activate the environment:

  4. Once activated, your terminal prompt will show (myenv). Now, if you run pip install numpy, it installs only into this project folder.

  5. Deactivate: deactivate

9.6.4 Managing Dependencies with requirements.txt

To share your project, or to move it to another computer, you need to list all dependencies.

9.7 Troubleshooting Common Installation Issues

Issue Likely Cause Solution
'python' is not recognized (Windows) Did not add Python to PATH. Reinstall and check the PATH box, or manually add the install folder to PATH.
python3: command not found (macOS/Linux) Python 3 is not installed. Install it via the official installer or Homebrew/APT.
pip: command not found Pip did not install with Python. Use python -m pip install <package> instead of pip install, or install python3-pip (Linux).
pip install gives Permission denied (macOS/Linux) Trying to install globally without sudo. Use --user flag: pip install --user <package>, or better, create a virtual environment.
VS Code cannot find the interpreter. The Python extension is not installed, or the interpreter path is not detected. Ctrl+Shift+P > Python: Select Interpreter > Choose your Python 3.
py command works but python doesn't (Windows) The Python launcher (py) is installed. You can use py to run Python, but still add python to PATH for convenience.

9.8 Quizzes

Quiz 1: Installation and PATH

1. What is the most critical step when installing Python on Windows that beginners often forget?

Answer(B) Checking "Add Python to PATH"

2. On macOS and Linux, which command should you typically use to check if Python 3 is installed?

Answer(B) `python3 --version`

3. The PATH environment variable tells the operating system:

Answer(B) The list of directories to search for executable programs

Quiz 2: IDEs and Editors

4. Which of the following is NOT an IDE but rather a code editor that requires extensions to support Python?

Answer(C) Visual Studio Code (It is a code editor that becomes a full IDE when extensions are added).

5. In VS Code, how do you select the Python interpreter for your current project?

Answer(B) By using the Command Palette (`Ctrl+Shift+P`) and selecting "Python: Select Interpreter"

6. What makes Thonny a good choice for absolute beginners?

Answer(B) It has a very simple interface and shows variable values during step-by-step debugging

Quiz 3: Pip and Virtual Environments

7. The standard package manager for Python is called:

Answer(B) PIP

8. What is the primary purpose of a virtual environment (venv)?

Answer(B) To isolate project dependencies, preventing version conflicts

9. Which command activates a virtual environment named my_venv on Windows?

Answer(B) `my_venv\Scripts\activate`

10. To generate a list of all installed packages in a project to share with others, you use:

Answer(B) `pip freeze > requirements.txt`

11. If pip is not found on your system, you can still install a package by using:

Answer(B) `python -m pip install package`

9.9 Exercises

Exercise 1: Installation Verification Checklist

Instructions: Perform the following actions on your computer and tick them off. Write down the output for steps that ask for specific information.

Exercise 2: Hello, World! in the IDE

Instructions:

  1. Open your chosen IDE (we recommend VS Code).

  2. Create a new folder named unit1-projects on your desktop.

  3. Open that folder in VS Code (File > Open Folder).

  4. Create a new file named hello.py.

  5. Write the following code:

    print("Hello, World!")
  6. Run the script using the green play button or terminal command.

  7. Modify the script to print your name. Example: print("My name is [Your Name]"). Run it again.

  8. Submit: Note down any error messages you encountered (if any) and how you resolved them.

Exercise 3: Creating a Virtual Environment and Installing a Package

Instructions:

  1. Open a terminal in your unit1-projects folder.

  2. Create a virtual environment named venv:

    python -m venv venv

    (Note: On Windows, if python doesn't work, use py).

  3. Activate the virtual environment:

  4. Verify the prompt changed to (venv).

  5. Install a popular package called requests:

    pip install requests
  6. Run pip freeze. You should see requests and its dependencies listed.

  7. Export the list to requirements.txt:

    pip freeze > requirements.txt
  8. Open requirements.txt and confirm the entries are there.

  9. Submit: Copy the output of pip freeze (after installing requests) and paste it into your exercise submission.

Exercise 4: Troubleshooting Practice

Instructions: A friend is trying to run hello.py and gets an error. Read the scenario below and write a one-sentence solution for them.

9.10 Homework Questions

Answer the following questions in complete sentences. Each response should be 3–5 sentences unless otherwise specified.

Short Answer Questions

1. Explain the purpose of the "Add Python to PATH" option during Windows installation. What happens if you do not select it?

Sample AnswerThe "Add Python to PATH" option adds the Python executable directory to the system's PATH environment variable so that the command prompt can find the `python` command from any directory. If you do not select it, typing `python` in the command prompt will result in an error, and you will have to navigate to the Python installation directory each time you want to run a script or use the full file path to the executable.

2. Why is it recommended to use a virtual environment (venv) for each Python project rather than installing all packages globally?

Sample AnswerVirtual environments are recommended because they isolate dependencies for each project, preventing conflicts where different projects require different versions of the same package. If you install everything globally, you will eventually encounter "dependency hell" where upgrading a library for one project breaks another. Virtual environments also make it easier to share a `requirements.txt` file so that other developers can recreate your exact development environment.

3. What is the difference between a command-line terminal and a Python IDE like PyCharm? Why might you need both?

Sample AnswerA command-line terminal provides a text-based interface to run commands, navigate the file system, and execute scripts manually, while a Python IDE provides a graphical workspace with code editing, debugging, and project management tools. You need the terminal to install packages using `pip`, manage virtual environments, and run scripts without the overhead of an IDE, while the IDE is essential for writing, formatting, and debugging larger codebases efficiently.

Essay Questions

Answer the following questions in 300–400 words each.

4. Imagine you are helping a friend who is completely new to programming install Python on their Windows computer. Write a step-by-step guide that explains not only what to click but why each step is important. Ensure you include how to write their first "Hello, World!" program.

Suggested outline:

5. Describe your experience setting up the Python environment for this course. Which IDE did you choose? What specific challenges did you encounter during installation (e.g., PATH issues, permission errors, choosing the right interpreter), and how did you resolve them? If you had no challenges, explain what you did to ensure a smooth setup and what you would tell a classmate who is struggling.

Suggested outline:

Research Questions

These questions require additional research beyond the tutorial content.

6. Research the concept of "Linting" (e.g., using pylint or flake8 in VS Code). Why is it beneficial to have a linter installed in your IDE? How does it relate to PEP 8 (Python's style guide)?

7. Research the difference between the py launcher (Windows) and the python command. What is the purpose of the py launcher, and how does it simplify managing multiple Python versions on a single machine?

8. Research Jupyter Notebooks (.ipynb). How does a Jupyter Notebook differ from a traditional .py script and IDE? Why might a data scientist prefer a Jupyter Notebook for exploratory analysis? (Hint: Look at interactive cells, markdown support, and visualization integration).

Previous | Tutorial index | Next