Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8015

Python • Re: compile python from source with tkinter support

$
0
0

I need to compile python from source such that the resulting python build can use tkinter.

It works out of the box, no need to compile anything. If you are having problems it is due to the module not being istalled. If you cannot run that snippet I posted you have issues going on.

Light up a venv and install everything into it and run your code in the venv. If it still does not work and while in venv do a pip freeze and post what modules you have in the venv.


Run this bash script on your box and it will install what is needed and create a venv for your testing:

Code:

#!/usr/bin/env bash#------------------------------------------------------------------------------# setup_python_venv_tkinter.sh## Purpose:#   - Ensure Python 3 is present#   - Ensure "venv" support is present (Debian package: python3-venv)#   - Ensure tkinter support is present (Debian package: python3-tk)#   - Create a virtual environment and install basic Python tooling packages## Target OS:#   - Raspberry Pi OS (Debian-based, uses apt)## Usage:#   chmod +x setup_python_venv_tkinter.sh#   ./setup_python_venv_tkinter.sh## Optional:#   VENV_DIR can be overridden:#     VENV_DIR=~/myenv ./setup_python_venv_tkinter.sh#------------------------------------------------------------------------------set -euo pipefail#---------------------------## 0) Simple helper methods  ##---------------------------#log()  { echo -e "\n[INFO] $*"; }warn() { echo -e "\n[WARN] $*" >&2; }die()  { echo -e "\n[ERROR] $*" >&2; exit 1; }#---------------------------## 1) Sanity checks          ##---------------------------## Make sure we're on a system with apt-get (Raspberry Pi OS / Debian / Ubuntu).command -v apt-get >/dev/null 2>&1 || die "This script expects apt-get (Debian/Raspberry Pi OS)."# Make sure python3 exists (apt package name is usually 'python3').if ! command -v python3 >/dev/null 2>&1; then  log "python3 not found. Installing python3..."  sudo apt-get update  sudo apt-get install -y python3else  log "python3 found: $(python3 --version)"fi#---------------------------## 2) Ensure OS packages     ##---------------------------## On Raspberry Pi OS, the following packages matter:#   - python3-venv : provides the 'venv' module (python3 -m venv ...)#   - python3-pip  : provides pip for Python 3#   - python3-tk   : provides tkinter support (Tk bindings)## Why install via apt (not pip)?#   - venv and tkinter are tied to system Python packaging on Debian systems.#   - pip is for Python packages, while Tk libraries and bindings are OS-level.REQUIRED_APT_PACKAGES=(  python3-venv  python3-pip  python3-tk)log "Installing required OS packages (idempotent: safe to re-run)..."sudo apt-get updatesudo apt-get install -y "${REQUIRED_APT_PACKAGES[@]}"# Optional but commonly helpful for building some pip packages with native extensions:# Uncomment if you want a smoother experience compiling wheels on the Pi.# sudo apt-get install -y build-essential python3-dev#---------------------------## 3) Verify venv works      ##---------------------------## If python3-venv isn't installed, this command often fails with:#   "The virtual environment was not created successfully because ensurepip is not available..."log "Verifying that 'python3 -m venv' is functional..."python3 -m venv --help >/dev/null 2>&1 || die "'python3 -m venv' is not working. Is python3-venv installed?"#---------------------------## 4) Create the virtual env ##---------------------------## Default venv location is a local folder called ".venv".# You can override it with:#   VENV_DIR=~/myenv ./setup_python_venv_tkinter.shVENV_DIR="${VENV_DIR:-./.venv}"log "Creating/refreshing virtual environment at: ${VENV_DIR}"# This creates the venv if it doesn't exist. If it exists, it won't destroy it;# it will just leave it in place. If you want to start fresh, delete the folder:#   rm -rf .venvpython3 -m venv "${VENV_DIR}"#---------------------------## 5) Activate venv          ##---------------------------## Activating modifies PATH so "python" and "pip" point into the venv.# shellcheck disable=SC1090source "${VENV_DIR}/bin/activate"log "Venv activated. Using python: $(command -v python)"log "Venv python version: $(python --version)"#---------------------------## 6) Update pip toolchain   ##---------------------------## pip/setuptools/wheel are the basics for installing packages cleanly.log "Upgrading pip, setuptools, wheel inside the venv..."python -m pip install --upgrade pip setuptools wheel#---------------------------## 7) Install 'basic modules'##---------------------------## "Basic modules" is subjective. Here are safe, common, and lightweight picks.# Edit this list to match your project needs.BASIC_PIP_PACKAGES=(  requests  rich)log "Installing basic pip packages inside the venv: ${BASIC_PIP_PACKAGES[*]}"python -m pip install "${BASIC_PIP_PACKAGES[@]}"#---------------------------## 8) Verify tkinter import  ##---------------------------## tkinter is NOT installed by pip; it comes from the OS package python3-tk.# A venv typically uses the system's standard library, so if python3-tk is installed,# importing tkinter from inside the venv should work.log "Verifying tkinter import from inside the venv..."python - <<'PY'import sysprint("Python executable:", sys.executable)try:    import tkinter as tk    print("tkinter import: OK")    print("Tk version:", tk.TkVersion)except Exception as e:    print("tkinter import: FAILED")    print("Error:", e)    raisePY#---------------------------## 9) Finish / reminders     ##---------------------------#log "All done."cat <<'EOF'Next steps:  1) Activate the venv in your shell:       source .venv/bin/activate  2) Run Python and try tkinter:       python -c "import tkinter; print('tkinter OK')"  3) When you're finished, deactivate:       deactivateTip:  If you ever want a fresh environment:    rm -rf .venv    ./setup_python_venv_tkinter.shEOF

Statistics: Posted by foxsquirrel — Wed Dec 31, 2025 5:12 pm



Viewing all articles
Browse latest Browse all 8015

Trending Articles