Ubuntu 17.10中Python 3.6导入tkinter失败的问题求助
Hey there, let’s work through this tkinter issue you’re facing—since your projects depend on it, we’ll get this sorted out step by step. Here are targeted fixes to try:
1. Double-Check python3-tk Installation for Python 3.6
First, confirm the tkinter package is properly linked to your Python 3.6 setup:
- Run this command to list installed python3-tk packages:
You should see an entry fordpkg -l | grep python3-tkpython3-tkmatching your Python 3.6 version. If not, reinstall it with:sudo apt update && sudo apt install --reinstall python3-tk
2. Verify Python 3.6’s Module Path
Sometimes tkinter exists but isn’t in your Python 3.6’s system path. Launch Python 3.6 in interactive mode and run:
import sys print(sys.path)
Look for a path like /usr/lib/python3.6/tkinter—if it’s missing, test adding it temporarily:
sys.path.append("/usr/lib/python3.6/tkinter") import tkinter # Try importing again
If this works, add the path permanently by editing your ~/.bashrc file:
export PYTHONPATH="/usr/lib/python3.6/tkinter:$PYTHONPATH"
Reload the file with source ~/.bashrc to apply changes.
3. Fix Manually Compiled Python 3.6 Issues
If you installed Python 3.6 from source (not via apt), you likely missed tkinter dependencies during compilation:
- Install required development libraries:
sudo apt install tk-dev tcl-dev - Recompile Python 3.6 with tkinter support:
# Navigate to your Python 3.6 source directory ./configure --with-tcltk --enable-shared make sudo make install
This ensures tkinter is baked into your custom Python build.
4. Confirm You’re Using the Correct Python 3.6 Instance
Multiple Python 3 versions might be installed, causing python3 to point to a different release:
- Check which Python 3 is active:
which python3 python3 --version - If it’s not 3.6, run your script directly with:
python3.6 your_script.py
5. Diagnose with Exact Error Messages
If none of the above work, run this command to get the full error traceback:
python3.6 -c "import tkinter"
The specific error (e.g., missing shared libraries, corrupted modules) will help narrow down the root cause even further.
内容的提问来源于stack exchange,提问作者Dogeek

