Install & Compatibility
Where this runs
tested against v0.9.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
wrap
✓ from textwrap3 import wrap
Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.
fill
✓ from textwrap3 import fill
Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.
shorten
✓ from textwrap3 import shorten
This function, backported from Python 3.4+, is available directly from 'textwrap3'.
TextWrapper
✓ from textwrap3 import TextWrapper
The TextWrapper class for advanced configuration is imported directly.
Demonstrates basic text wrapping, filling, and shortening using the core functions from the textwrap3 library.
from textwrap3 import wrap, fill, shorten
long_text = """This is a very long string that needs to be wrapped
into several lines to be more readable. It demonstrates the basic functionality of the textwrap3 library, which is a backport of Python 3.6's textwrap module."""
# Using wrap to get a list of lines
wrapped_lines = wrap(long_text, width=40)
print("Wrapped lines (list):")
for line in wrapped_lines:
print(line)
print("\n" + "-" * 30 + "\n")
# Using fill to get a single string with newlines
filled_text = fill(long_text, width=40)
print("Filled text (single string):")
print(filled_text)
print("\n" + "-" * 30 + "\n")
# Using shorten to truncate text
shortened_text = shorten(long_text, width=50, placeholder=' [...]')
print("Shortened text:")
print(shortened_text)
Debug
Known issues
gotchaDue to its design principle, `textwrap3` adheres to Python 3's sensibilities, especially concerning Unicode. It uses `re.UNICODE` and treats Unicode em-dashes like ASCII '--'. This can lead to subtle differences in wrapping behavior compared to the native `textwrap` module in older Python 2.x environments where Unicode handling might differ.fixBe aware of potential Unicode handling discrepancies. Test text wrapping behavior thoroughly with diverse character sets, especially when migrating from native Python 2.x `textwrap`.
affects: All versions of textwrap3, specifically when used in Python 2.x environments.
gotchaThe `wrap()` and `fill()` functions, by default, replace all internal whitespace characters (including newlines and tabs) with single spaces before wrapping. Existing newlines in multi-paragraph input will be collapsed. If you need to preserve original newlines as paragraph breaks, process paragraphs separately or use `replace_whitespace=False` (though this might lead to other formatting challenges).fixPass `replace_whitespace=False` to `wrap()` or `TextWrapper` constructor if you want to prevent the collapsing of whitespace. For multi-paragraph text, it's often better to split the text into paragraphs, wrap each, and then join them back.
affects: All versions of textwrap3.
gotchaFor modern Python 3.6+ environments, `textwrap3` is generally unnecessary. All the features it backports are already present in the standard library's `textwrap` module. Installing it in these environments adds an unnecessary dependency.fixFor Python 3.6 and later, use the built-in `textwrap` module directly (e.g., `import textwrap`) instead of `textwrap3`. The functionality is identical.
affects: Users on Python 3.6 and newer.
gotchaThe underlying `textwrap.wrap()` logic (inherited by `textwrap3`) can exhibit poor performance (quadratic time complexity) when wrapping text containing extremely long, unbroken words (e.g., a very long string without spaces or hyphens) if `break_long_words` is `True` (the default).fixIf you encounter performance issues with very long words, consider setting `break_on_hyphens=False` or `break_long_words=False` in the `TextWrapper` constructor or `wrap`/`fill` function calls if breaking long words is not critical, or pre-processing the text to insert break points.
affects: All versions of textwrap3.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'textwrap3'
The 'textwrap3' package is not installed in the current Python environment.
AttributeError: module 'textwrap' has no attribute 'shorten'
This error occurs on Python versions older than 3.4 when trying to use `textwrap.shorten` directly. Even if `textwrap3` is installed, you must explicitly import `textwrap3` as it does not automatically patch the built-in `textwrap` module.
fixInstead of `import textwrap`, use `import textwrap3 as textwrap` or `from textwrap3 import shorten` to access the backported functionality:
```python
import textwrap3 as textwrap
# or from textwrap3 import shorten
my_string = "This is a very long string that needs to be shortened."
shortened_string = textwrap.shorten(my_string, width=20, placeholder="...")
print(shortened_string)
```
TypeError: shorten() got an unexpected keyword argument 'fix_sentence_endings'
The `fix_sentence_endings` argument is valid for `textwrap.wrap` and `textwrap.fill` but not for `textwrap.shorten` (or `textwrap3.shorten`).
fixRemove the `fix_sentence_endings` argument, as it is not applicable to the `shorten` function:
```python
import textwrap3
text = "This is a sample sentence. It might be long."
# Incorrect:
# textwrap3.shorten(text, width=20, fix_sentence_endings=True)
# Correct:
shortened_text = textwrap3.shorten(text, width=20, placeholder="...")
print(shortened_text)
```
TypeError: shorten() missing 1 required positional argument: 'text'
The `textwrap3.shorten` function requires at least one positional argument: the string (`text`) to be shortened.
fixProvide the `text` string as the first argument to the `shorten` function:
```python
import textwrap3
# Incorrect:
# textwrap3.shorten(width=10)
# Correct:
shortened_text = textwrap3.shorten("Hello, world! This is some text.", width=10)
print(shortened_text)
``` Upgrade
Version history
0.9.2latest on PyPI · released Jan 23, 2019
Audit
Dependencies
No dependency data recorded yet.