Attempted Relative Import With No Known Parent Package (Fix)

ImportError attempted relative import with no known parent package shown in Command Prompt

ImportError: attempted relative import with no known parent package means you ran a file that uses a relative import, like from ..utils import money, directly as a script. Run it as a module from the project root instead: I reproduced the error and every fix in a tiny warehouse project on Windows with Python 3.12.5. … Read more >>

How to Loop Through Files in a Directory in Python

Loop through files in directory Python: Command Prompt output of os.listdir and os.scandir listing files

You can loop through files in a directory in Python with Path(folder).iterdir() or os.listdir(), and keep only the files. For one file type, glob(“*.csv”) filters as it loops: These examples run against a small sales_2026 folder of state sales files that the script rebuilds every time, on Python 3.12.5. I’ll go through os.listdir and os.scandir, … Read more >>

Python compile() Function: Syntax and 6 Examples

Python compile() function turning a code string into a code object and running it with exec()

If you have Python code in a string and want to run it later, the Python compile function is the quickest way to get it. It takes the source, a filename and a mode and gives you a code object. In this tutorial, I’ll show you compile() in all three modes, plus a reusable rule, … Read more >>

Python complex() Function: Syntax and 5 Examples

Python complex() function creating complex numbers from numbers and strings

If you want to work with complex numbers in Python, the Python complex function is what you need. It takes a real and an imaginary part, or a number or string, and gives you back a complex number like (3+4j). In this tutorial, I’ll take complex() from bare calls to string input, magnitude and phase, … Read more >>

Python delattr() Function: Syntax and 5 Examples

Python delattr() function deleting an attribute from an object

If you want to delete an object’s attribute in Python, the Python delattr function is what you’re looking for. It takes the object and the attribute name as a string and removes it, like del obj.name. In this tutorial, I’ll take delattr() from a basic delete to string names, class attributes and a custom guard, … Read more >>

Python dict() Function: Syntax and 5 Examples

Python dict() function turning two lists into a dictionary with zip()

If you want a dictionary of key-value pairs in Python, the Python dict function is what you’re looking for. It takes keyword arguments, pairs or another mapping and gives you back a dict. In this tutorial, I’ll take dict() apart in five examples: keywords, tuples, zip(), copies and generators. You’ll see real output from Python … Read more >>

Python dir() Function: Syntax and 5 Examples

Python dir() function listing the public methods of str and list

If you want to see an object’s names in Python, the Python dir function does exactly that. It takes an optional object and gives you back a sorted list of its names. In this tutorial, I’ll walk through its syntax and five examples that go from your local scope to modules and your own classes. … Read more >>

pip install from pyproject.toml (With Examples)

pip install from pyproject.toml: Command Prompt output of pip building and installing a local project

For a pip install from pyproject.toml, open a terminal in the folder that holds the file and run pip install .. The dot means “this project”: pip reads pyproject.toml, builds the package and installs it along with its dependencies: I tested every command on a tiny invoice project inside a fresh virtual environment, using Python … Read more >>

__new__ vs __init__ in Python

__new__ vs __init__ in Python: output showing __new__ running before __init__ for an Order object

If you’re choosing between __new__ and __init__ for a class, pick __init__ nearly every time. That’s the heart of __new__ vs __init__ in Python: __new__ creates and returns the object, and __init__ fills in its attributes. Override __new__ only when creation itself must change, like subclassing float or str, returning a cached object, or building … Read more >>