使用BeautifulSoup、Platypus与Python3时的Unicode差异问题
Hey Colin, I’ve dealt with similar headaches when packaging Python 3 scripts for Mac with Platypus and working with BeautifulSoup, so let’s walk through the most likely fixes:
1. Ditch the 2to3 Conversion for BeautifulSoup 4.6
BeautifulSoup 4.6 actually already supports Python 3 natively—you don’t need to run it through 2to3 at all. That conversion might have broken internal code structures. Here’s how to reset it:
- Uninstall the modified version of BeautifulSoup:
pip3.6 uninstall beautifulsoup4 - Reinstall the original, Python 3-compatible version directly using your target Python 3.6 pip:
This ensures you’re using a clean, officially supported build for Python 3.6.pip3.6 install beautifulsoup4==4.6.0
2. Verify Platypus is Using the Correct Python 3.6 Interpreter
Platypus often defaults to the system’s default Python, which might not be your 3.6.0 installation. To fix this:
- Open your Platypus project settings.
- Under the Script section, click the dropdown next to "Interpreter" and select "Custom".
- Paste the full path to your Python 3.6 executable. On most Macs, this might look like:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 - Double-check that your script’s shebang line matches this path:
#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
3. Fix File Encoding Issues for Local XML/HTML
Python 3 handles file encoding differently than Python 2, which is a common source of parsing errors with local files:
- When opening XML/HTML files in your script, explicitly specify the encoding (most likely UTF-8, but adjust if your files use another format):
with open("your_file.html", "r", encoding="utf-8") as f: soup = BeautifulSoup(f, "html.parser") - If you’re dealing with files that have mixed encodings, add an error handler to avoid crashes:
with open("your_file.xml", "r", encoding="utf-8", errors="replace") as f: soup = BeautifulSoup(f, "lxml")
4. Test the Script Directly Before Packaging
Before wrapping it with Platypus, run the script directly in your terminal using Python 3.6 to isolate issues:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 your_script.py
This will show you the exact error traceback (like missing modules, encoding errors, or parsing bugs) without Platypus adding extra layers of complexity.
5. Check for Missing Dependencies in the Packaged App
Platypus might not include all required dependencies by default. If your script uses other libraries besides BeautifulSoup (like lxml or html5lib), make sure they’re installed for Python 3.6, and use Platypus’s "Include Python Modules" option to bundle them with the app.
内容的提问来源于stack exchange,提问作者Colin Fredericks

