Python
Python environments, data handling, imports and runtime errors.
50 troubleshooting guides · Page 2 / 3
How can I process many async jobs without opening thousands of connections?
Creating one task per item can overwhelm connection pools and memory even when the tasks mostly await. Concurrency is a resource budget, not the same as input size.
When should TaskGroup replace a group of unrelated background tasks?
Use a task group when child operations belong to one lifetime and failures should be coordinated. Python 3.11 introduced `asyncio.TaskGroup` for this structured ownership.
Why does multiprocessing repeatedly start my script on Windows?
Spawn-based process creation imports the main module in each child. Unguarded top-level process creation therefore runs again during import.
Why is a shared counter sometimes too small after several threads update it?
A read-modify-write sequence is a logical transaction, not automatically an atomic application operation. Interleaving can overwrite another worker’s update.
Why does every Python log line appear twice?
A logger may have its own handler while also propagating the same record to a parent handler. Repeated configuration can add duplicate handlers too.
How do I preserve the original error when adding context to an exception?
Replacing an exception without its cause can hide the operation that actually failed. Python supports explicit chaining.
How can a Python file be closed even when processing raises an exception?
Resource release must follow the operation’s lifetime, including exceptional exits. Relying on garbage collection makes that timing implicit.
How do I avoid leaving a partially written JSON configuration file?
Writing directly over the destination exposes a truncated file if the process crashes halfway through. Write a complete replacement before switching names.
Is it safe to load a pickle file uploaded by a user?
No. Pickle deserialization can execute code, so it is not a safe interchange format for untrusted uploads. A filename extension or virus scan does not change that design.
Why can a short-looking regex take seconds on an invalid long string?
Nested or ambiguous repetition can make a backtracking regex explore many possible matches before rejecting input. Pattern length alone does not predict runtime.
How can I extract an uploaded ZIP without allowing paths outside its destination?
Archive member names are input data, not trusted filesystem paths. Absolute paths, parent traversal and platform-specific separators require validation before writing.
Why does bool of an environment variable set to false evaluate to True?
Environment values are strings. In Python every nonempty string, including `"false"` and `"0"`, is truthy.
Do Python type hints reject wrong values at runtime?
Annotations describe intended types for tools and readers; ordinary Python does not automatically enforce them when a function is called.
Why does caching a method appear to keep old objects alive?
An `lru_cache` retains references to cached arguments and results. For a method, `self` is part of the cache key, which can extend instance lifetime.
Why does a decorator hide my function name and signature metadata?
The decorated name usually refers to the wrapper function. Without copied metadata, introspection and tooling see the wrapper’s identity.
Why does sqlite3 reject a connection created in another thread?
Python’s sqlite3 connection checks thread ownership by default. Sharing one connection casually can mix transaction state between callers.
Why does sqlite3 treat a string parameter as many bindings?
The parameter argument must be a sequence of values. A string is itself a sequence, and `(value)` is only parenthesized text, not a one-element tuple.
How can I read a large text file without loading the whole file into memory?
`read()` without a size materializes the entire file. For line-oriented processing, iterate over the open file instead.
Why is Python hash unsuitable for a persistent cache key?
Built-in hashes of some types are randomized between processes and are designed for hash tables, not stable content identifiers. Collisions are also possible.
Should password reset tokens be generated with random.random?
No. The general-purpose random module is designed for simulation, not security tokens that must resist prediction.
How do I find the slow part of a Python program before rewriting it?
Guessing from source length often targets code that contributes little to actual runtime. Measure a representative workload first.
Why should async request metadata use ContextVar instead of a global variable?
Concurrent requests share module globals, so one request can overwrite another’s correlation ID or locale while both are active.
Why does Queue.join never return after a worker fails?
Queue completion counts depend on a matching `task_done()` for each successful `get()`. A worker that raises before acknowledging leaves unfinished work outstanding.
Why does comparing two equal strings with is sometimes fail?
`is` compares object identity; `==` compares values according to the type’s equality rules. Interpreter interning can make identity tests appear to work accidentally.
Ask your own question · Sign in to post questions and answers.