Depth First Search in Python (DFS with Examples)

Depth first search in Python: Command Prompt output of a DFS topological sort of course prerequisites

Depth first search in Python follows one branch of a graph as far as it can go, then backs up and tries the next branch. A short recursive function and a set of visited nodes is all it takes: The graphs here are things you’d actually model, like course prerequisites and service dependencies, and each … Read more >>

How to Change the Working Directory in Python

Change working directory in Python: Command Prompt output of os.chdir and os.getcwd

You change the working directory in Python with os.chdir(path), and check where you are with os.getcwd(). After the change, every relative file path is resolved from the new folder: The folder names below are real ones I created under C:\pyguides, and every script ran on Python 3.12.5 in Command Prompt. You’ll see the basics, the … Read more >>

How to Use Python in Excel (=PY Function Guide)

How to use Python in Excel: Microsoft's introduction to Python in Excel support page

Here’s how to use Python in Excel: select a cell, go to Formulas > Insert Python (or type =PY(), write your Python code, and press Ctrl+Enter. Use xl() to pull worksheet data into your code: I don’t have Excel’s cloud Python in my test setup, so the Excel steps follow Microsoft’s support docs as of … Read more >>

How to Move a File in Python (shutil and pathlib)

Move a file in Python: Command Prompt output of shutil.move moving an invoice into an archive folder

The easiest way to move a file in Python is shutil.move(source, destination). Give it a folder and the file goes inside it; give it a full path and the file is moved and renamed in one step: These examples created, moved and checked real files with Python 3.12.5 on Windows; the screenshots show each script’s … Read more >>

Python Import from Another Directory (5 Ways That Work)

Python import from another directory: ModuleNotFoundError fixed by running the app with python -m

For a Python import from another directory, the cleanest fix is to make the folder a package and run your script as a module from the project root with python -m. Python then finds sibling folders automatically: I built that small project for real and ran every command with Python 3.12.5 in Command Prompt, errors … Read more >>

os.path.join() in Python: Join Paths the Right Way

os path join in Python: Command Prompt output of os.path.join with absolute paths and drive letters

os.path.join() in Python combines folder and file names into one path, using the right separator for your operating system: a backslash on Windows and a forward slash on macOS and Linux. My machine runs Python 3.12.5 on Windows, so the screenshots show backslashes; I also use posixpath to show what macOS and Linux would print. … Read more >>

Order of Operations in Python (Operator Precedence)

Order of operations in Python: Command Prompt output showing PEMDAS rules and parentheses

Python’s order of operations follows the PEMDAS rule from math class: parentheses first, then exponents (**), then multiplication and division, then addition and subtraction, working left to right within each level. I checked every expression on Python 3.12.5, so the results below are exactly what Python prints, including a few that surprise people. Below you’ll … Read more >>

Breadth First Search in Python (BFS with Examples)

Breadth first search in Python: Command Prompt output of BFS solving a grid maze with the shortest path

Breadth first search in Python explores a graph level by level: first the start node, then all its neighbors, then their neighbors, and so on. You build it with a queue from collections.deque and a set of visited nodes: Every BFS here uses only the standard library, and I ran each one with Python 3.12.5 … Read more >>

pip install -r requirements.txt: Install, Create and Fix Errors

pip install from requirements txt: Command Prompt installing packages into a virtual environment

Running pip install from a requirements txt file takes one command: activate your virtual environment in the project folder, then run pip install -r requirements.txt. pip reads every line and installs those packages and versions: Every command here really ran on Python 3.12.5 in Windows Command Prompt, installing two small packages from PyPI into a … Read more >>

How to Make a Directory in Python (os and pathlib)

Make a directory in Python: Command Prompt output of pathlib mkdir creating nested folders

Need to make a directory in Python? Use os.mkdir(“name”) for a single folder, or Path(“a/b/c”).mkdir(parents=True, exist_ok=True) to create nested folders without errors if they already exist: Each folder in these examples was created for real by Python 3.12.5 on Windows, and the screenshots show the Command Prompt output. I’ll cover os.mkdir(), os.makedirs(), pathlib, dated and … Read more >>