Python 2 shebang含非ASCII字符的编码问题及替代方案问询
Great question—this is a notoriously tricky edge case in Python 2.7's encoding handling, especially when working with virtual environments in non-ASCII directories. Let’s break down the problem and your available solutions:
核心问题解析
Python 2.7 parses the shebang line before it reads any PEP 263 magic comments. If the shebang path contains non-ASCII characters, the OS or Python interpreter will throw an encoding error before your # -*- coding: utf-8 -*- comment even gets processed. That’s why the magic comment fails here—it never gets a chance to take effect.
可行的替代方案
1. 修改virtualenv生成的脚本shebang(最直接的临时修复)
Instead of using the absolute path to your virtualenv’s Python binary (which includes non-ASCII characters), replace the shebang line in all virtualenv scripts (like bin/python, bin/pip, etc.) with:
#!/usr/bin/env python2
This tells the system to find the python2 binary in your current environment’s PATH—which, when your virtualenv is activated, will point to the correct binary without referencing the non-ASCII path.
To avoid doing this manually every time you create a virtualenv, you can try using the --relocatable flag when creating or updating the env:
virtualenv --relocatable /path/to/your/non-ascii-venv
Note: The --relocatable option has some limitations (it doesn’t work perfectly for all packages), but it’s worth testing for your use case.
2. 主目录内创建纯ASCII软链接(无需管理员权限)
Even if your home directory has non-ASCII characters, you can create a pure ASCII-named directory inside your home and link to the virtualenv from there. For example:
# Create a pure ASCII directory mkdir ~/ascii_venvs # Link your existing virtualenv to this directory ln -s ~/你的含中文目录/venv ~/ascii_venvs/my_venv
Now you can use the pure ASCII path ~/ascii_venvs/my_venv/bin/python to run your scripts—this bypasses the non-ASCII path in the shebang entirely, and you don’t need admin rights since this is all within your home directory.
3. 系统locale调整(有限作用)
Setting your system’s LANG or LC_ALL environment variable to a UTF-8 locale (e.g., en_US.UTF-8) can sometimes help the OS parse non-ASCII paths in shebangs correctly. You can set this temporarily in your shell:
export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8
However, this depends on your OS’s shebang parsing implementation—some older systems still struggle with non-ASCII paths here, so it’s not a guaranteed fix.
4. 升级到Python 3(终极解决方案)
Python 3.6+ completely revamped path handling with PEP 529, which sets the default filesystem encoding to UTF-8 across all platforms. This eliminates most non-ASCII path issues, and the shebang/magic comment system works reliably even with non-ASCII paths. Since Python 2 is no longer supported, upgrading is the most sustainable fix long-term.
关于环境变量的说明
Unfortunately, Python 2.7 does not have an environment variable that globally sets the source file encoding (unlike Python 3’s PYTHONUTF8 flag). The PYTHONIOENCODING variable only controls encoding for standard input/output, not source file parsing—so it won’t help with this shebang issue.
内容的提问来源于stack exchange,提问作者Glutexo

