     (goto-char start))
 
   ;; install imenu
-  (make-variable-buffer-local 'imenu-create-index-function)
-  (setq imenu-create-index-function
-	(function imenu-example--create-python-index))
-  (setq imenu-generic-expression
-	imenu-example--generic-python-expression)
-  (if (fboundp 'imenu-add-to-menubar)
-      (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
+  (if (py-safe (require 'imenu))
+      (progn
+	(make-variable-buffer-local 'imenu-create-index-function)
+	(setq imenu-create-index-function
+	      (function imenu-example--create-python-index))
+	(setq imenu-generic-expression
+	      imenu-example--generic-python-expression)
+	(if (fboundp 'imenu-add-to-menubar)
+	    (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
+	))
 
   ;; run the mode hook. py-mode-hook use is deprecated
   (if python-mode-hook

(defvar imenu-example--python-generic-regexp nil)
(defvar imenu-example--python-generic-parens nil)


;;;###autoload
(eval-when-compile
  ;; Imenu isn't used in XEmacs, so just ignore load errors
  (condition-case ()
      (progn
	(require 'cl)
	(require 'imenu))
    (error nil)))

(defun imenu-example--create-python-index ()
  "Python interface function for imenu package.

Primarily for entering new code:
\t\\[indent-for-tab-command]\t indent line appropriately
\t\\[py-newline-and-indent]\t insert newline, then indent
\t\\[py-electric-backspace]\t reduce indentation, or delete single character

Primarily for reindenting existing code:
\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
