|
| 1 | +# Introduction |
| 2 | + |
| 3 | +This is an introduction to getting started with Python. |
| 4 | + |
| 5 | +Contact: <majp@ramboll.com> |
| 6 | + |
| 7 | +Please don't hestitate to contact me if something doesn't work. I wrote this off the top of my head, so I probably missed something... |
| 8 | + |
| 9 | +## Installing a package manager for Python |
| 10 | + |
| 11 | +We will be using the [`micromamba`](https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html) package manager. It is a standalone package manager based on [`conda`](https://docs.conda.io/en/latest/). |
| 12 | + |
| 13 | +### Windows |
| 14 | + |
| 15 | +Open a `PowerShell` terminal and run |
| 16 | + |
| 17 | +```powershell |
| 18 | +Invoke-Expression ((Invoke-WebRequest -Uri https://micro.mamba.pm/install.ps1).Content) |
| 19 | +``` |
| 20 | + |
| 21 | +This will place `micromamba.exe` at `C:\Users\<you>\AppData\Local\micromamba\micromamba.exe`. |
| 22 | + |
| 23 | +Next, initialize your shell with |
| 24 | + |
| 25 | +```powershell |
| 26 | +C:\Users\<you>\AppData\Local\micromamba\micromamba.exe shell init -s powershell -p ~\micromamba |
| 27 | +``` |
| 28 | + |
| 29 | +Confirm that `micromamba` was installed successfully with |
| 30 | + |
| 31 | +```powershell |
| 32 | +micromamba info |
| 33 | +``` |
| 34 | + |
| 35 | +This should list some information about your micromamba installation. Done! |
| 36 | + |
| 37 | +### Linux |
| 38 | + |
| 39 | +Open your favourite terminal and run |
| 40 | + |
| 41 | +```bash |
| 42 | +"${SHELL}" <(curl -L https://micro.mamba.pm/install.sh) |
| 43 | +``` |
| 44 | + |
| 45 | +Next, initialize your shell with |
| 46 | + |
| 47 | +```bash |
| 48 | +./bin/micromamba shell init -s bash -p ~/micromamba |
| 49 | +``` |
| 50 | + |
| 51 | +Confirm that `micromamba` was installed successfully with |
| 52 | + |
| 53 | +```powershell |
| 54 | +micromamba info |
| 55 | +``` |
| 56 | + |
| 57 | +This should list some information about your micromamba installation. Done! |
| 58 | + |
| 59 | +## Create your first environment |
| 60 | + |
| 61 | +The Python programming language uses so-called *packages* that contain reusable code (functions, classes, modules). For example, the `numpy` package provides functionality for fast mathematical operations, and the `polars` package enables easy and fast handling of tabular data. |
| 62 | + |
| 63 | +An *environment* refers to the set of packages that the Python interpreter has access to. The Python interpreter is the process that executes your Python code. |
| 64 | + |
| 65 | +Let's create an environment called `aau`. Open your terminal (`PowerShell` on Windows, `bash` on Linux) and run |
| 66 | + |
| 67 | +```shell |
| 68 | +micromamba create --name aau python numpy polars |
| 69 | +``` |
| 70 | + |
| 71 | +The `--name` argument, which has the value `aau`, is the name for your environment. The values after that are the packages that will be installed in that environment. |
| 72 | + |
| 73 | +After you've created an environment, you need to *activate* it. Python only has access to the packages in the currently active environment. You do this with |
| 74 | + |
| 75 | +```shell |
| 76 | +micromamba activate aau |
| 77 | +``` |
| 78 | + |
| 79 | +## Installing a code editor |
| 80 | + |
| 81 | +We're now ready to install a code editor. The code editor is the programme in which you write your code. It can provide you with many tools that help you write better code. |
| 82 | + |
| 83 | +We will be use Microsoft Visual Studio Code (VS Code for short). This editor has a rich extension ecosystem, and you can find extensions for pretty much anything, including writing and running Python code! |
| 84 | + |
| 85 | +### Windows |
| 86 | + |
| 87 | +> Note: You might need to activate `winget`. To do so, install the ["App Installer"](https://apps.microsoft.com/detail/app-installer/9NBLGGH4NNS1?hl=en-US&gl=US) package from Microsoft Store. |
| 88 | +
|
| 89 | +To install VS Code on Windows, execute the following in `PowerShell`: |
| 90 | + |
| 91 | +```powershell |
| 92 | +winget install -e --id Microsoft.VisualStudioCode --override '/mergetasks="addcontextmenufiles,addcontextmenufolders,addtopath"' |
| 93 | +``` |
| 94 | + |
| 95 | +Now you should be able to open VS Code by running |
| 96 | + |
| 97 | +```powershell |
| 98 | +code |
| 99 | +``` |
| 100 | + |
| 101 | +### Linux |
| 102 | + |
| 103 | +TODO |
| 104 | + |
| 105 | +### Configure VS Code |
| 106 | + |
| 107 | +We'll need to configure a few things and install some extensions. |
| 108 | + |
| 109 | +### User configuration |
| 110 | + |
| 111 | +Your user (system-wide) configuration for VS Code is stored as a JSON file in `$Env:APPDATA\Code\User\settings.json`. |
| 112 | + |
| 113 | +You can open that file with |
| 114 | + |
| 115 | +```powershell |
| 116 | +code $Env:APPDATA\Code\User\settings.json |
| 117 | +``` |
| 118 | + |
| 119 | +and then paste the following JSON into the file: |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + // LANGUAGE-SPECIFIC ----------------------------------------------------- |
| 124 | + "[python]": { |
| 125 | + "editor.codeActionsOnSave": { |
| 126 | + "source.organizeImports": true |
| 127 | + }, |
| 128 | + "editor.defaultFormatter": "ms-python.black-formatter", |
| 129 | + "editor.formatOnSave": true, |
| 130 | + "editor.formatOnSaveMode": "modifications", |
| 131 | + "editor.formatOnType": false |
| 132 | + }, |
| 133 | + // PYTHON --------------------------------------------------------------- |
| 134 | + "black-formatter.args": [ |
| 135 | + "--line-length=120" |
| 136 | + ], |
| 137 | + "isort.args": [ |
| 138 | + "--profile=black", |
| 139 | + "--line-length=120" |
| 140 | + ], |
| 141 | + "python.analysis.autoImportCompletions": true, |
| 142 | + "python.languageServer": "Pylance", |
| 143 | + "python.terminal.activateEnvInCurrentTerminal": false, |
| 144 | + "python.testing.promptToConfigure": false, |
| 145 | + "python.testing.pytestArgs": [ |
| 146 | + "-vv" |
| 147 | + ], |
| 148 | + "python.testing.pytestEnabled": true, |
| 149 | + "pylint.importStrategy": "fromEnvironment", |
| 150 | + "python.analysis.importFormat": "relative", |
| 151 | + "python.terminal.activateEnvironment": false, |
| 152 | + "python.condaPath": "$MAMBA_EXE", |
| 153 | + "python.experiments.enabled": false, |
| 154 | + // GENERAL --------------------------------------------------------------- |
| 155 | + "debug.console.wordWrap": false, |
| 156 | + "editor.fontFamily": "'CaskaydiaCove NF Mono', Consolas, 'Courier New', monospace", |
| 157 | + "editor.fontLigatures": true, |
| 158 | + "editor.formatOnSave": true, |
| 159 | + "editor.minimap.enabled": true, |
| 160 | + "editor.renderWhitespace": "all", |
| 161 | + "editor.rulers": [ |
| 162 | + 120 |
| 163 | + ], |
| 164 | + "editor.unicodeHighlight.allowedCharacters": { |
| 165 | + "️": true |
| 166 | + }, |
| 167 | + "editor.wordWrapColumn": 120, |
| 168 | + "explorer.confirmDelete": false, |
| 169 | + "explorer.confirmDragAndDrop": false, |
| 170 | + "files.autoSave": "afterDelay", |
| 171 | + "vsicons.dontShowNewVersionMessage": true, |
| 172 | + "window.commandCenter": true, |
| 173 | + "workbench.iconTheme": "vscode-icons", |
| 174 | + "workbench.startupEditor": "none", |
| 175 | + "workbench.tree.indent": 12, |
| 176 | + "workbench.tree.renderIndentGuides": "always", |
| 177 | + "redhat.telemetry.enabled": false, |
| 178 | + "security.workspace.trust.untrustedFiles": "open", |
| 179 | + // FILE FORMATS ---------------------------------------------------------- |
| 180 | + "files.exclude": { |
| 181 | + "**/.DS_Store": true, |
| 182 | + "**/.git": false, |
| 183 | + "**/.hg": true, |
| 184 | + "**/.svn": true, |
| 185 | + "**/*__pycache__**": false, |
| 186 | + "**/*.aux": true, |
| 187 | + "**/*.bbl": true, |
| 188 | + "**/*.blg": true, |
| 189 | + "**/*.fdb_latexmk": true, |
| 190 | + "**/*.fls": true, |
| 191 | + "**/*.log": true, |
| 192 | + "**/*.out": true, |
| 193 | + "**/*.synctex.gz": true, |
| 194 | + "**/*.toc": true, |
| 195 | + "**/CVS": true |
| 196 | + }, |
| 197 | + "json.maxItemsComputed": 2000, |
| 198 | + // GIT ------------------------------------------------------------------- |
| 199 | + "diffEditor.ignoreTrimWhitespace": false, |
| 200 | + "git-graph.date.type": "Commit Date", |
| 201 | + "git-graph.dialog.createBranch.checkOut": true, |
| 202 | + "git-graph.graph.style": "angular", |
| 203 | + "git-graph.repository.commits.initialLoad": 75, |
| 204 | + "git-graph.repository.fetchAndPrune": true, |
| 205 | + "git-graph.repository.fetchAndPruneTags": true, |
| 206 | + "git.autofetch": true, |
| 207 | + "git.confirmSync": false, |
| 208 | + "git.openRepositoryInParentFolders": "never", |
| 209 | + "git.suggestSmartCommit": false, |
| 210 | + "gitlens.defaultDateShortFormat": "DD-MM-YYYY", |
| 211 | + "gitlens.defaultTimeFormat": "hh:mm", |
| 212 | + "gitlens.hovers.currentLine.over": "line", |
| 213 | + // TERMINAL -------------------------------------------------------------- |
| 214 | + "terminal.integrated.cursorBlinking": true, |
| 215 | + "terminal.integrated.defaultProfile.linux": "bash", |
| 216 | + "terminal.integrated.defaultProfile.windows": "PowerShell", |
| 217 | + "terminal.integrated.enableMultiLinePasteWarning": false, |
| 218 | + "terminal.integrated.fontFamily": "'CaskaydiaCove NF Mono'", |
| 219 | + "terminal.integrated.profiles.windows": { |
| 220 | + "PowerShell": { |
| 221 | + "args": [ |
| 222 | + "-ExecutionPolicy", |
| 223 | + "ByPass", |
| 224 | + "-NoExit", |
| 225 | + "-nologo" |
| 226 | + ], |
| 227 | + "source": "PowerShell" |
| 228 | + } |
| 229 | + }, |
| 230 | + "terminal.integrated.rightClickBehavior": "default", |
| 231 | + "terminal.integrated.automationProfile.windows": {}, |
| 232 | + "debug.allowBreakpointsEverywhere": true |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +#### Extensions |
| 237 | + |
| 238 | +There are many extensions available for VS Code. We'll install a few so we can more easily work with Python and enhance the experience. |
| 239 | + |
| 240 | +You can find the extension manager in the sidebar on the left-hand side of the window. The icon is four little squares. Here, you can search for extensions. You can run the code below in your terminal to install some recommended extensions: |
| 241 | + |
| 242 | +```shell |
| 243 | +code --install-extension eamodio.gitlens |
| 244 | +code --install-extension ms-python.python |
| 245 | +code --install-extension ms-python.vscode-pylance |
| 246 | +code --install-extension ms-python.black-formatter |
| 247 | +code --install-extension ms-python.flake8 |
| 248 | +code --install-extension ms-python.isort |
| 249 | +code --install-extension ms-python.pylint |
| 250 | +code --install-extension mhutchie.git-graph |
| 251 | +code --install-extension vscode-icons-team.vscode-icons |
| 252 | +``` |
| 253 | + |
| 254 | +## Installing a version control system |
| 255 | + |
| 256 | +It is extremely beneficial to put your code under version control. For this, we use [`git`](https://git-scm.com/). With version control, you can record the change history of your code and |
| 257 | + |
| 258 | +* See what changes were made |
| 259 | +* When those changes were made |
| 260 | +* Who made the changes |
| 261 | +* Go back in history in case you made mistakes or want to run an older version of your code |
| 262 | + |
| 263 | +### Windows |
| 264 | + |
| 265 | +To install `git`, execute the following in `PowerShell`: |
| 266 | + |
| 267 | +```powershell |
| 268 | +winget install --id=Git.Git -e |
| 269 | +``` |
| 270 | + |
| 271 | +### Linux |
| 272 | + |
| 273 | +Already installed by default. |
| 274 | + |
| 275 | +### Configure git |
| 276 | + |
| 277 | +Let's configure our `git` installation. |
| 278 | + |
| 279 | +Set your email and username with |
| 280 | + |
| 281 | +```shell |
| 282 | +git config --global user.name "your name here" |
| 283 | +git config --global user.email my.email@domain.com |
| 284 | +``` |
| 285 | + |
| 286 | +Set the default editor for `git` to VS Code with |
| 287 | + |
| 288 | +```shell |
| 289 | +git config --global core.editor code |
| 290 | +``` |
0 commit comments