LykosAI/StabilityMatrix · error · InvalidDataException
pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII
Error message
pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path} What it means
PyVenvCfg.Load also rejects pyvenv.cfg content containing NUL bytes after UTF-8 decoding. NUL bytes mean the file was written in an incompatible encoding (typically UTF-16 without a BOM, or corruption), so an InvalidDataException naming the path is thrown to avoid silently parsing garbage.
Solutions
- Rewrite pyvenv.cfg as UTF-8/ASCII without NUL bytes
- Recreate the venv to regenerate a clean config file
- Inspect the file bytes to confirm the encoding and convert with an explicit Unicode->UTF-8 conversion
- Catch InvalidDataException and rebuild the environment or regenerate the config
Example fix
// before [IO.File]::WriteAllText($path, $content, [Text.Encoding]::Unicode) # UTF-16, maybe no BOM // after [IO.File]::WriteAllText($path, $content, new-object Text.UTF8Encoding($false))
Defensive patterns
Strategy: validation
Validate before calling
var text = new UTF8Encoding(false).GetString(File.ReadAllBytes(cfgPath));
if (text.Contains('\0'))
throw new InvalidDataException($"{cfgPath} is not valid UTF-8/ASCII; rewrite as UTF-8"); Try / catch
try
{
var cfg = PyVenvCfg.Load(cfgPath);
}
catch (InvalidDataException ex)
{
logger.LogWarning(ex, "pyvenv.cfg contains NUL bytes; recreating venv config");
RecreateVenvConfig(cfgPath);
} Prevention
- Write config files with explicit UTF-8 encoding, never UTF-16
- Validate file bytes after any programmatic write to pyvenv.cfg
- Recreate the venv if config corruption is suspected
When it happens
Trigger: Loading a pyvenv.cfg whose raw bytes decode (as UTF-8) with embedded '\0' characters — UTF-16-encoded files lacking a BOM, or corrupted/truncated binary writes.
Common situations: UTF-16 files written without BOM by custom scripts, disk corruption, tools that wrote the config with wrong encoding settings.
Related errors
- pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII
- Torch is not installed in the virtual environment.
- ComfyUI venv was not found at
- Windows ROCm bitsandbytes is only supported on Python 3.12.x
- Failed to start Forge Classic install process
AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12).
Data as JSON: /api/errors/06851ba69c368b9b.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Core/Python/PyVenvCfg.cs:69
/// </summary>
public static PyVenvCfg Load(string path)
{
var bytes = File.ReadAllBytes(path);
// pyvenv.cfg is UTF-8/ASCII; reject UTF-16 BOMs and NUL bytes, which
// indicate the file was read with the wrong encoding.
if (
bytes.Length >= 2
&& ((bytes[0] == 0xFF && bytes[1] == 0xFE) || (bytes[0] == 0xFE && bytes[1] == 0xFF))
)
{
throw new InvalidDataException($"pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII: {path}");
}
var content = new UTF8Encoding(false).GetString(bytes);
if (content.Contains('\0'))
{
throw new InvalidDataException($"pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path}");
}
return Parse(content);
}
/// <summary>
/// Gets the value of the last matching key (CPython is last-wins), or null.
/// Setting rewrites every matching key, appending a new key when absent.
/// </summary>
public string? this[string key]
{
get
{
for (var i = _entries.Count - 1; i >= 0; i--)
{
if (_entries[i].Key is { } k && k.Equals(key, StringComparison.OrdinalIgnoreCase))
{
return _entries[i].Value;View on GitHub (pinned to af93d6ef57)