Python自定义Google助手:法语版本报NameError: name 'Google_Assistant'未定义
Let’s break down why your identical French code is failing while the English version works—this almost always boils down to import path issues or subtle environment differences between how you’re running the two scripts. Here are the most likely fixes:
1. Verify File Structure and Import Paths
First, double-check that your French script (action_test_fr.py) can actually find the google_assistant.py module:
- Make sure
google_assistant.pyis in the same directory asaction_test_fr.py. Linux (Raspberry Pi OS) is case-sensitive, so confirm the filename matches exactly (no typos likeGoogle_Assistant.pyinstead ofgoogle_assistant.py). - Add a quick debug check at the top of
action_test_fr.pyto see where Python is looking for modules:
If your project directory (import sys print("Python module search paths:", sys.path)/home/pi/Projects/MyAssistant/) isn’t in this list, Python can’t findgoogle_assistant.py. Fix this by either running the script from the project root, or adding the path explicitly:import sys sys.path.append("/home/pi/Projects/MyAssistant/") from google_assistant import *
2. Clear Python’s Module Cache
Python caches compiled modules in __pycache__ folders. Sometimes, if you copied files around, the cache might be outdated or corrupted:
- Delete the
__pycache__folder in your project directory (it’s safe to remove—Python will rebuild it). - Restart your French script after deleting the cache.
3. Check How You’re Running the French Script
If you’re running the English version from the project root (python action_test_en.py) but running the French version from a different directory (e.g., python MyAssistant/action_test_fr.py), Python will look for google_assistant.py in your current working directory, not the project root.
- Always run both scripts from the same working directory:
cd /home/pi/Projects/MyAssistant/ python action_test_fr.py
4. Verify the Import Worked
Add a debug line right after your imports in action_test_fr.py to confirm Google_Assistant is being imported:
from google_assistant import * print("Imported items from google_assistant:", dir())
If Google_Assistant doesn’t show up in the output, your import is failing. This could mean:
- The
google_assistant.pyfile has a syntax error (but since the English version works, this is unlikely). - You accidentally modified the French script’s import line (e.g., deleted
from google_assistant import *or changed the filename).
5. Rule Out Scope Issues
While you said the code is identical, double-check that the from google_assistant import * line is at the top level of action_test_fr.py (not inside a function or conditional block). If it’s inside a function, the Google_Assistant class won’t be available in the action_test_fr function’s scope.
内容的提问来源于stack exchange,提问作者RHarris

