Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:02:16+00:00 2026-05-20T12:02:16+00:00

I have defined a .dir-locals.el file with the following content: ((python-mode . ((cr/virtualenv-name .

  • 0

I have defined a .dir-locals.el file with the following content:

((python-mode . ((cr/virtualenv-name . "saas"))))

In my .emacs I have the following function to retrieve this value and provide a virtualenv path:

(defun cr/virtualenv ()
  (cond (cr/virtualenv-name (format "%s/%s" virtualenv-base cr/virtualenv-name))
        ((getenv "EMACS_VIRTUAL_ENV") (getenv "EMACS_VIRTUAL_ENV"))
        (t "~/.emacs.d/python")))

Finally, in my python-mode-hook list, I have this hook function:

(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)

(defun cr/python-mode-shell-setup ()
  (message "virtualenv-name is %s" cr/virtualenv-name)
  (let ((python-base (cr/virtualenv)))
    (cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
           (setq python-python-command (concat python-base "/bin/ipython"))
           (setq py-python-command (concat python-base "/bin/ipython"))
           (setq py-python-command-args '( "-colors" "NoColor")))
          (t
           (setq python-python-command (concat python-base "/bin/python"))
           (setq py-python-command (concat python-base "/bin/python"))
           (setq py-python-command-args nil)))))

When I open a new python file, the message logged by cr/python-mode-shell-setup indicates that cr/virtualenv-name is nil. However, when I C-h v the name, I get “saas” instead.

Obviously there’s a load order issue here; is there a way to have my mode hook statements respond to directory-local variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:02:17+00:00Added an answer on May 20, 2026 at 12:02 pm

    This happens because normal-mode calls (set-auto-mode) and (hack-local-variables) in that order.

    However hack-local-variables-hook is run after the local variables have been processed, which enables some solutions:

    1. The first is to make Emacs run a new “local variables hook” for each major mode:

      (add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
      (defun run-local-vars-mode-hook ()
        "Run a hook for the major-mode after the local variables have been processed."
        (run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
      
      (add-hook 'python-mode-local-vars-hook 'cr/python-mode-shell-setup)
      

      (Your original function can be used unmodified, with that approach.)

    2. A second option is to utilise the optional LOCAL argument to add-hook that makes the specified function buffer-local. With this approach you could write your hook as follows:

      (add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
      
      (defun cr/python-mode-shell-setup ()
        (add-hook 'hack-local-variables-hook
                  (lambda () (message "virtualenv-name is %s" cr/virtualenv-name)
                    (let ((python-base (cr/virtualenv)))
                      (cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
                             (setq python-python-command (concat python-base "/bin/ipython"))
                             (setq py-python-command (concat python-base "/bin/ipython"))
                             (setq py-python-command-args '( "-colors" "NoColor")))
                            (t
                             (setq python-python-command (concat python-base "/bin/python"))
                             (setq py-python-command (concat python-base "/bin/python"))
                             (setq py-python-command-args nil)))))
                  nil t)) ; buffer-local hack-local-variables-hook
      

      i.e. python-mode-hook runs first and registers the anonymous function with hack-local-variables-hook for the current buffer only; and that function is then called after the local variables have been processed.

    3. Lindydancer’s comment prompts a third approach. It’s not nearly as clean as the other two, but proved interesting regardless. I didn’t like the idea of causing (hack-local-variables) to be called twice, but I see that if you set the local-enable-local-variables buffer-locally, it prevents (hack-local-variables) from doing anything, so you could do this:

      (defun cr/python-mode-shell-setup ()
        (report-errors "File local-variables error: %s"
          (hack-local-variables)))
        (set (make-local-variable 'local-enable-local-variables) nil)
        (let ((python-base (cr/virtualenv)))
          ...))
      

      Obviously that modifies the normal sequence of execution a little, so side effects may be possible. I was worried that if the same major mode is set by a local variable comment in the file, this might cause infinite recursion, but that doesn’t actually appear to be a problem.

      Local variable header comments (e.g. -*- mode: foo -*-) are handled by (set-auto-mode), so those are fine; but a mode: foo Local Variables: comment seems like it would be an issue as it is handled by (hack-local-variables), and so if the mode is set that way I thought it would cause recursion.

      In practice I was able to trigger the problem by using a simple function as a ‘mode’ which did nothing more than try to run its hooks; however testing with a ‘proper’ mode did not exhibit the problem, so it’s probably safe in reality. I didn’t look into this further (as the other two solutions are much cleaner than this), but I would guess the delayed mode hooks mechanism probably explains it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following defined in my app.yaml: handlers: - url: /favicon.ico static_files: img/favicon.ico
I have defined a custom Sharepoint list for special attributes related to a software
I have defined an interface in C++, i.e. a class containing only pure virtual
I have defined a personalizable property on a web part and I would like
I have defined a new dialog and its controls in an already existing resource
I have defined the global nls_date_format on Oracle 10.2 XE as follows: alter system
I have an interface that I have defined in C++ which now needs to
Let's say I have defined a route: routes.Add(new Route(Users/{id}, new MvcRouteHandler()) { Defaults =
Let's say we have defined a CSS class that is being applied to various
I have a div container and have defined its style as follows: div#tbl-container {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.