|
1 | | -# Setup: Run Python on Your Computer |
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Setup |
| 4 | +nav_order: 4 |
| 5 | +has_children: true |
| 6 | +permalink: /setup/ |
| 7 | +course_lesson: true |
| 8 | +course_index: "00" |
| 9 | +next_page: /lessons/fundamentals/ |
| 10 | +next_title: Python Fundamentals |
| 11 | +--- |
| 12 | + |
| 13 | +# Setup: Run Your First Python Program |
| 14 | + |
| 15 | +This page helps you install Python, create one file, and see your first successful output. Complete the quick start before learning about virtual environments. |
| 16 | + |
| 17 | +## Before installing anything |
| 18 | + |
| 19 | +If you use a school or office computer, ask a parent, teacher, or administrator before installing software. Do not remove a Python installation that already belongs to the operating system. |
| 20 | + |
| 21 | +You need: |
| 22 | + |
| 23 | +- a computer running Windows, macOS, or Linux; |
| 24 | +- permission to install software; |
| 25 | +- a text editor; |
| 26 | +- a terminal, which is a window where you type commands. |
| 27 | + |
| 28 | +Choose only the section for your operating system. |
| 29 | + |
| 30 | +## Windows quick start |
| 31 | + |
| 32 | +### Step 1 — install Python |
| 33 | + |
| 34 | +Use the official [Python Install Manager instructions](https://docs.python.org/3/using/windows.html). It can be installed from Python.org or the Microsoft Store. |
| 35 | + |
| 36 | +After installation, open **PowerShell** from the Start menu and type: |
| 37 | + |
| 38 | +```powershell |
| 39 | +python --version |
| 40 | +``` |
2 | 41 |
|
3 | | -## Goal |
| 42 | +If your installation uses the `py` command, this also works: |
4 | 43 |
|
5 | | -By the end of this section you can install Python, open a terminal, run Python code, create a project folder, and use an isolated virtual environment. |
| 44 | +```powershell |
| 45 | +py --version |
| 46 | +``` |
6 | 47 |
|
7 | | -## Which Python should I install? |
| 48 | +You should see a Python 3 version. The exact numbers may be newer than examples in this course. |
8 | 49 |
|
9 | | -Use a current stable Python 3 release from [python.org/downloads](https://www.python.org/downloads/). Do not install a pre-release for this course. |
| 50 | +### Step 2 — create a learning folder |
10 | 51 |
|
11 | | -## macOS |
| 52 | +```powershell |
| 53 | +mkdir python-learning |
| 54 | +cd python-learning |
| 55 | +``` |
| 56 | + |
| 57 | +- `mkdir` creates a folder. |
| 58 | +- `cd` moves the terminal into that folder. |
| 59 | + |
| 60 | +### Step 3 — create and run `hello.py` |
| 61 | + |
| 62 | +Open the `python-learning` folder in your editor. Create a file named `hello.py` and type: |
12 | 63 |
|
13 | | -1. Download the macOS installer from [Python Releases for macOS](https://www.python.org/downloads/macos/). |
14 | | -2. Run the installer and complete the prompts. |
15 | | -3. Open Terminal and verify: |
| 64 | +```python |
| 65 | +print("Hello, Python!") |
| 66 | +``` |
| 67 | + |
| 68 | +Save the file. Return to PowerShell and run: |
| 69 | + |
| 70 | +```powershell |
| 71 | +python hello.py |
| 72 | +``` |
| 73 | + |
| 74 | +If `python` does not work but `py` does, run: |
| 75 | + |
| 76 | +```powershell |
| 77 | +py hello.py |
| 78 | +``` |
| 79 | + |
| 80 | +Expected output: |
| 81 | + |
| 82 | +```text |
| 83 | +Hello, Python! |
| 84 | +``` |
| 85 | + |
| 86 | +## macOS quick start |
| 87 | + |
| 88 | +### Step 1 — install Python |
| 89 | + |
| 90 | +Download the current supported Python 3 installer from [Python Releases for macOS](https://www.python.org/downloads/macos/). Use the normal `.pkg` installer and complete its instructions. |
| 91 | + |
| 92 | +Open **Terminal** and type: |
16 | 93 |
|
17 | 94 | ```bash |
18 | 95 | python3 --version |
19 | 96 | ``` |
20 | 97 |
|
21 | | -4. Create a workspace: |
| 98 | +You should see a Python 3 version. Do not delete or modify Python supplied by macOS or Apple development tools. |
| 99 | + |
| 100 | +### Step 2 — create a learning folder |
22 | 101 |
|
23 | 102 | ```bash |
24 | 103 | mkdir python-learning |
25 | 104 | cd python-learning |
26 | | -python3 -m venv .venv |
27 | | -source .venv/bin/activate |
28 | | -python --version |
29 | 105 | ``` |
30 | 106 |
|
31 | | -## Linux |
| 107 | +### Step 3 — create and run `hello.py` |
| 108 | + |
| 109 | +Open the `python-learning` folder in your editor. Create `hello.py`: |
| 110 | + |
| 111 | +```python |
| 112 | +print("Hello, Python!") |
| 113 | +``` |
32 | 114 |
|
33 | | -Python is already available on many distributions. Use your distribution's package manager and prefer Python 3. On Debian or Ubuntu: |
| 115 | +Save it and run: |
| 116 | + |
| 117 | +```bash |
| 118 | +python3 hello.py |
| 119 | +``` |
| 120 | + |
| 121 | +Expected output: |
| 122 | + |
| 123 | +```text |
| 124 | +Hello, Python! |
| 125 | +``` |
| 126 | + |
| 127 | +## Linux quick start |
| 128 | + |
| 129 | +Python 3 is already available on many Linux distributions. First try: |
| 130 | + |
| 131 | +```bash |
| 132 | +python3 --version |
| 133 | +``` |
| 134 | + |
| 135 | +If Python is unavailable on Debian or Ubuntu, use: |
34 | 136 |
|
35 | 137 | ```bash |
36 | 138 | sudo apt update |
37 | 139 | sudo apt install python3 python3-venv python3-pip |
38 | | -python3 --version |
| 140 | +``` |
| 141 | + |
| 142 | +For Fedora, Arch, or another distribution, use its package manager and the [official Python Unix guide](https://docs.python.org/3/using/unix.html). |
| 143 | + |
| 144 | +Create the learning folder: |
| 145 | + |
| 146 | +```bash |
39 | 147 | mkdir python-learning |
40 | 148 | cd python-learning |
| 149 | +``` |
| 150 | + |
| 151 | +Create `hello.py` in your editor: |
| 152 | + |
| 153 | +```python |
| 154 | +print("Hello, Python!") |
| 155 | +``` |
| 156 | + |
| 157 | +Save it and run: |
| 158 | + |
| 159 | +```bash |
| 160 | +python3 hello.py |
| 161 | +``` |
| 162 | + |
| 163 | +## What just happened? |
| 164 | + |
| 165 | +```text |
| 166 | +hello.py -> Python reads the file -> print() displays text -> terminal shows output |
| 167 | +``` |
| 168 | + |
| 169 | +- `hello.py` is a Python source file. |
| 170 | +- `.py` is the usual filename ending for Python code. |
| 171 | +- `python`, `py`, or `python3` starts the Python interpreter. |
| 172 | +- An **interpreter** is the program that reads and runs Python instructions. |
| 173 | + |
| 174 | +If you saw `Hello, Python!`, pause and enjoy that small win. Your computer has successfully run your code. |
| 175 | + |
| 176 | +## Recommended next step: a virtual environment |
| 177 | + |
| 178 | +A virtual environment is a private project box for Python tools and installed packages. One project's packages will not interfere with another project's packages. |
| 179 | + |
| 180 | +```text |
| 181 | +computer Python |
| 182 | +├── project A .venv -> project A packages |
| 183 | +└── project B .venv -> project B packages |
| 184 | +``` |
| 185 | + |
| 186 | +The Basic examples use the standard library and can run without extra packages. Creating `.venv` now is still a useful habit. |
| 187 | + |
| 188 | +### macOS and Linux |
| 189 | + |
| 190 | +From inside `python-learning`: |
| 191 | + |
| 192 | +```bash |
41 | 193 | python3 -m venv .venv |
42 | 194 | source .venv/bin/activate |
43 | 195 | python --version |
44 | 196 | ``` |
45 | 197 |
|
46 | | -For Fedora, Arch, and other distributions, follow the distribution-specific instructions in the [official Unix guide](https://docs.python.org/3/using/unix.html). |
| 198 | +### Windows PowerShell |
47 | 199 |
|
48 | | -## Windows |
49 | | - |
50 | | -1. Install Python using the official [Windows installation guide](https://docs.python.org/3/using/windows.html) or the Python Install Manager from [python.org/downloads](https://www.python.org/downloads/). |
51 | | -2. Open PowerShell and verify: |
| 200 | +From inside `python-learning`: |
52 | 201 |
|
53 | 202 | ```powershell |
54 | | -py --version |
| 203 | +python -m venv .venv |
| 204 | +.venv\Scripts\Activate.ps1 |
| 205 | +python --version |
55 | 206 | ``` |
56 | 207 |
|
57 | | -3. Create a workspace and virtual environment: |
| 208 | +If you normally use `py`, create it with: |
58 | 209 |
|
59 | 210 | ```powershell |
60 | | -mkdir python-learning |
61 | | -cd python-learning |
62 | 211 | py -m venv .venv |
63 | | -.venv\Scripts\Activate.ps1 |
64 | | -python --version |
65 | 212 | ``` |
66 | 213 |
|
67 | | -If PowerShell blocks activation, use the Python interpreter directly with `.venv\Scripts\python.exe`, or ask an administrator to review the execution-policy setting. Do not randomly change system security settings. |
68 | | - |
69 | | -## First program |
| 214 | +When activation succeeds, the terminal prompt normally begins with `(.venv)`. Activation makes the plain `python` command select the environment's interpreter. |
70 | 215 |
|
71 | | -Create a file named `hello.py`: |
| 216 | +To leave the environment later: |
72 | 217 |
|
73 | | -```python |
74 | | -print("Hello, Python!") |
| 218 | +```text |
| 219 | +deactivate |
75 | 220 | ``` |
76 | 221 |
|
77 | | -Run it from the activated environment: |
| 222 | +If PowerShell blocks activation, do not randomly weaken computer security. You may run the environment directly: |
78 | 223 |
|
79 | | -```bash |
80 | | -python hello.py |
| 224 | +```powershell |
| 225 | +.venv\Scripts\python.exe hello.py |
81 | 226 | ``` |
82 | 227 |
|
| 228 | +## Choose an editor |
| 229 | + |
| 230 | +Read [Editor Setup](editor-setup.md). Any editor that saves plain `.py` files is acceptable. IDLE, which is included with many Python installations, is enough for this course. |
| 231 | + |
| 232 | +## Learn six terminal commands |
| 233 | + |
| 234 | +Read [Terminal Basics](terminal-basics.md). You only need a few commands to move between folders and run files. |
| 235 | + |
83 | 236 | ## Setup checkpoint |
84 | 237 |
|
85 | | -- [ ] Python 3 is installed. |
86 | | -- [ ] The version command works. |
87 | | -- [ ] A virtual environment was created. |
88 | | -- [ ] The environment was activated. |
89 | | -- [ ] `hello.py` runs successfully. |
90 | | -- [ ] You know how to deactivate it with `deactivate`. |
| 238 | +- [ ] A Python 3 version command works. |
| 239 | +- [ ] The `python-learning` folder exists. |
| 240 | +- [ ] `hello.py` contains the one-line program. |
| 241 | +- [ ] Running the file displays `Hello, Python!`. |
| 242 | +- [ ] I know which command works on my computer: `python`, `py`, or `python3`. |
| 243 | +- [ ] I either created `.venv` or understand that I can return to that optional step. |
| 244 | + |
| 245 | +If a checkpoint fails, open [Setup Troubleshooting](troubleshooting.md). |
91 | 246 |
|
92 | 247 | --- |
93 | 248 |
|
|
0 commit comments