class: center, middle ### 28: Python virtual environments, VSCode launch configurations, packages w/ pip and requirements.txt --- #### Outline - [Our bug from yesterday: a Vim browser extension](#3) - [Python virtual environments (in VSCode)](#4) - [Setting up a launch configuration in VSCode](#5) - [Python packages, the Python package index, and pip](#6) - [Package dependencies at the project level, requirements.txt](#7) - [Preview: Git (with Git bash), SourceTree, and GitHub (remote repository host)](#8) --- #### Our bug from yesterday: a Vim browser extension - If anyone else uses Chrome Remote Desktop with Vimium active, you have been warned! --- #### Python virtual environments (in VSCode) - Virtual environments at a high level -- sandboxing project packages so that they don;t interfere with each other - In VSCode specifically: https://jasonmurray.org/posts/2020/vscodepythonvenv/ - In the integrated terminal, run: - `python -m venv .venv` - And then `./.venv/Scripts/activate` to activate the virtual environment --- #### Setting up a launch configuration in VSCode - https://code.visualstudio.com/docs/python/debugging#_set-configuration-options Example: ```json { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "configurations": [ { "name": "Preprocess", "type": "python", "request": "launch", "program": "main.py", "console": "integratedTerminal" } ] } ``` --- #### Python packages, the Python package index, and pip - https://pypi.org/ - https://www.w3schools.com/python/python_pip.asp - `pip` is a package manager for Python, just like `choco` is a package manager for Windows. Example: - Let's say you want to build a URL from a file path. URLs don't have spaces (conventionally), and usually follow a specific format. To get strings in a format, we can download a package and use it: https://pypi.org/project/python-slugify/ --- #### Python packages, the Python package index, and pip Let's say we had a simple program like the following ```python import slugify def make_url(input_string): return slugify.slugify(input_string) ``` You can also just import the function directly (`from slugify import slugify` = in the format of `from module_name import function_name`), and then you don't need to worry about the module name when invoking the method. --- #### Package dependencies at the project level, requirements.txt - It's not as hard as you might think - https://note.nkmk.me/en/python-pip-install-requirements/ - First: `pip freeze > requirements.txt` - Then when installing your program somewhere else: `pip install -r requirements.txt` --- #### Preview: Git (with Git bash), SourceTree, and GitHub (remote repository host) - Git is a version control system. There are other options too, but Git is probably the most popular. - Version control systems track the changes you make to files over time, and are absolutely essential to the software development process, especially when working on a team of developers. - Today we will just be downloading and setting up a couple tools. We will get into specifics in our next lesson <!-- Footnote List --> <div class="footnotes"> <ol> </ol> </div> --- #### Outline - [Our bug from yesterday: a Vim browser extension](#3) - [Python virtual environments (in VSCode)](#4) - [Setting up a launch configuration in VSCode](#5) - [Python packages, the Python package index, and pip](#6) - [Package dependencies at the project level, requirements.txt](#7) - [Preview: Git (with Git bash), SourceTree, and GitHub (remote repository host)](#8)