在NAO机器人Choregraphe中导入requests模块遇ImportError: cannot import name utils
ImportError: cannot import name utils with Requests in Choregraphe Hey there, let's break down why you're hitting this ImportError: cannot import name utils issue when trying to use the requests module in your NAO robot's Choregraphe project. Here are the most likely causes and fixes to get you back on track:
Common Causes
- Misplaced Dependencies: It looks like you moved dependency packages inside the
requestsfolder, which messes up the module structure. Requests expects its core dependencies (likeurllib3,certifi,chardet) to exist at the same level as therequestsfolder, not nested inside it. This confuses Python's module resolver when it tries to findrequests.utils. - Incomplete Requests Package: If the
requestsfolder you copied doesn't include its ownutilssubfolder (with all necessary.pyfiles including__init__.py), the import will fail. This can happen if you downloaded a partial package or accidentally deleted files during extraction. - Python Version Mismatch: Choregraphe uses a specific Python version tied to your NAOqi version (older NAO robots often use Python 2.7, newer ones may use Python 3.x). If you downloaded a
requestsversion that doesn't support your Choregraphe's Python version (e.g., Requests 3.x for Python 3+ on a Python 2.7 setup), the module structure will be incompatible.
Step-by-Step Fixes
Fix Your Lib Folder Structure
Restructure yourlibdirectory sorequestsand its dependencies are at the same level, not nested. It should look like this:lib/ ├── requests/ │ ├── utils/ │ ├── __init__.py │ └── ... (other requests submodules/files) ├── urllib3/ ├── certifi/ └── chardet/ # Or charset_normalizer/, depending on your requests versionRemove any dependency folders you added inside the
requestsdirectory.Verify Requests Package Completeness
Open yourrequestsfolder and confirm there's autilssubfolder with files like__init__.py,cookies.py,encoding.py, etc. If it's missing, re-download the full source package for your target Python version, extract it, and copy the completerequestsfolder to yourlibdirectory.Match Python Versions
- Check your Choregraphe's Python version: Go to Help > About Choregraphe to find the NAOqi version, then look up which Python version it uses (e.g., NAOqi 2.5 uses Python 2.7, NAOqi 2.9+ uses Python 3.7).
- Download the corresponding
requestsversion: For Python 2.7, use Requests 2.27.x (the last version supporting Python 2). For Python 3.x, use the latest compatible version. - Ensure your dependencies (urllib3, certifi, etc.) also support the same Python version.
Test Imports Gradually
In your Choregraphe Python script, test imports one by one to isolate issues:import urllib3 # First confirm dependencies work import certifi import requests # Then try requestsIf a dependency fails to import, that's likely the root cause—double-check you have the right version of that dependency in your
libfolder.
内容的提问来源于stack exchange,提问作者Federico Giuriato

