You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何解决Pyarmor混淆Python代码时的UnicodeEncodeError错误?

UnicodeEncodeError When Obfuscating Python Files with Pyarmor on Windows 10

Environment Context

  • OS: Windows 10
  • Python: 3.8.5
  • Pyarmor: 6.4.2

Project Directory Structure

│ .gitignore
│ main.py
│ README.md
│ requirements.txt
│
├───cogs
│   │ somefile.py
│   │ somefile.py
│   │ ... (10 total .py files)
│   │
│   └───__pycache__
│           pyc files
│
└───__pycache__
        main.cpython-38.pyc

The Problem

I've already set VS Code's encoding to UTF-8 and added # -*- coding: utf-8 -*- at the top of every Python file. But when running the obfuscation command from the main.py directory:

pyarmor -d obfuscate --recursive main.py

I get this traceback:

Traceback (most recent call last):
File "c:\users\User\appdata\local\programs\python\python38-32\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\User\appdata\local\programs\python\python38-32\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\Scripts\pyarmor.exe_main
.py", line 7, in
File "c:\users\User\appdata\local\programs\python\python38-32\lib\site-packages\pyarmor\pyarmor.py", line 1447, in main_entry
main(sys.argv[1:])
File "c:\users\User\appdata\local\programs\python\python38-32\lib\site-packages\pyarmor\pyarmor.py", line 1439, in main
args.func(args)
File "c:\users\User\appdata\local\programs\python\python38-32\lib\site-packages\pyarmor\pyarmor.py", line 694, in _obfuscate
encrypt_script(prokey, a, b, wrap_mode=args.wrap_mode,
File "c:\users\User\appdata\local\programs\python\python38-32\lib\site-packages\pyarmor\utils.py", line 933, in encrypt_script
f.write(''.join(lines))
File "c:\users\User\appdata\local\programs\python\python38-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 177-178: character maps to

I've tried the fixes in the Pyarmor docs, but the error persists.


Solution Options

1. Force UTF-8 Encoding via Environment Variable

Windows defaults to the cp1252 encoding for console operations, which conflicts with your UTF-8 files. Set the PYTHONIOENCODING variable before running Pyarmor to force Python to use UTF-8 for I/O:

For Command Prompt (CMD):

set PYTHONIOENCODING=utf-8
pyarmor -d obfuscate --recursive main.py

For PowerShell:

$env:PYTHONIOENCODING = "utf-8"
pyarmor -d obfuscate --recursive main.py

This ensures Pyarmor uses UTF-8 when writing obfuscated files, avoiding the charmap encoding conflict.

2. Patch Pyarmor's utils.py File

The error happens because Pyarmor opens output files without explicitly specifying UTF-8 encoding. You can modify the relevant code:

  1. Navigate to c:\users\User\appdata\local\programs\python\python38-32\lib\site-packages\pyarmor\utils.py
  2. Locate the encrypt_script function (near line 933 where the error occurs)
  3. Find the file opening line (looks like with open(new_path, 'w') as f:)
  4. Modify it to explicitly use UTF-8:
    with open(new_path, 'w', encoding='utf-8') as f:
    
  5. Save the file and re-run your Pyarmor command.

Note: This patch will be overwritten if you update Pyarmor, so it's a temporary fix until you can upgrade.

3. Upgrade Pyarmor to a Newer Version

Your Pyarmor version (6.4.2) is outdated. Later versions likely include fixes for Windows encoding issues. Upgrade to the latest compatible version for Python 3.8:

pip install --upgrade pyarmor

After upgrading, re-run your obfuscation command—this might resolve the problem entirely without extra tweaks.


内容的提问来源于stack exchange,提问作者Buster

火山引擎 最新活动