LykosAI/StabilityMatrix · error · InvalidDataException
pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII
Error message
pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII: {path} What it means
PyVenvCfg.Load reads a venv's pyvenv.cfg as bytes and rejects files with a UTF-16 BOM (FF FE / FE FF), since venv config must be UTF-8/ASCII. Reading a UTF-16 file as UTF-8 would produce garbage key/value pairs, so an InvalidDataException naming the path is thrown instead.
Solutions
- Rewrite pyvenv.cfg as UTF-8 (no BOM) using a text editor or 'Set-Content -Encoding utf8NoBOM'
- Recreate the venv so the config is generated with the correct encoding
- Convert the file: read as Unicode, write back as UTF-8 before loading
- Catch InvalidDataException and regenerate the config programmatically
Example fix
// before powershell: "home = ..." | Set-Content pyvenv.cfg # UTF-16 by default // after powershell: "home = ..." | Set-Content -Encoding utf8NoBOM pyvenv.cfg
Defensive patterns
Strategy: validation
Validate before calling
var bytes = File.ReadAllBytes(cfgPath);
bool isUtf16 = bytes.Length >= 2 && ((bytes[0] == 0xFF && bytes[1] == 0xFE) || (bytes[0] == 0xFE && bytes[1] == 0xFF));
if (isUtf16) { /* convert to UTF-8 before loading */ } Type guard
static bool IsUtf8Clean(string path)
{
var b = File.ReadAllBytes(path);
if (b.Length >= 2 && ((b[0] == 0xFF && b[1] == 0xFE) || (b[0] == 0xFE && b[1] == 0xFF))) return false;
return !new UTF8Encoding(false).GetString(b).Contains('\0');
} Try / catch
try
{
var cfg = PyVenvCfg.Load(cfgPath);
}
catch (InvalidDataException ex)
{
logger.LogWarning(ex, "pyvenv.cfg bad encoding; regenerating");
RewriteAsUtf8(cfgPath);
var cfg = PyVenvCfg.Load(cfgPath);
} Prevention
- Never write pyvenv.cfg with PowerShell 5.x default redirection (UTF-16); use utf8NoBOM
- Always save text config files as UTF-8 without BOM
- Verify encoding when scripts patch venv configs
When it happens
Trigger: Calling PyVenvCfg.Load on a pyvenv.cfg that was saved as UTF-16 LE/BE — e.g. created by PowerShell 5.x ('>' redirection) or Notepad with 'Unicode' encoding.
Common situations: Windows tooling or scripts rewriting pyvenv.cfg with UTF-16 default encoding; a venv created/patched by a PowerShell script that out-recreated the config.
Related errors
- pyvenv.cfg contains NUL bytes; 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/0345c9231b4a1efc.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Core/Python/PyVenvCfg.cs:63
return new PyVenvCfg(entries);
}
/// <summary>
/// Loads a pyvenv.cfg file. Fails loudly on non-UTF-8 encodings instead of
/// silently mangling the file.
/// </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]
{
getView on GitHub (pinned to af93d6ef57)