I'm setting up my python lsp using this video tutorial.

My emacs init.el configuration file is here

By default projectile-project-test-cmd runs the command python -m unittest discover. I want to change it to pytest instead as explained by tutor.

I followed the below steps -

1. M-x add-dir-local-variable RET
2. choose mode 'python-mode'
3. Add local-variable 'projectile-project-test-cmd'.
4. Add local variable value "pytest".

It creates .dir-locals.el file with below contents -

((python-mode . ((projectile-project-test-cmd . "pytest"))))

Now when I run M-x projectile-test-project it runs pytest command.

But when I close emacs and reopen it, it stops running pytest and runs python -m unittest discover instead.

How to fix this issue?

  • Are you in a file-visiting buffer when you run the command? dir-locals don't affect other buffers automatically (i.e. the major mode needs to support the behaviour explicitly). – phils Feb 15 at 22:26
  • I don't work with python, but python -m unittest discover seems like a very specific command. Where does that come from? Perhaps your project is already setting this command in some other way, taking precedence over your dir-local value. – phils Feb 15 at 22:28
  • What's the output of (gethash (projectile-compilation-dir) projectile-test-cmd-map)? Related maybe? emacs.stackexchange.com/questions/61493/… – TerryTsao Mar 18 at 2:45

Did you save the .dir-locals.el file? Are you loading these files via TRAMP? By default, Emacs doesn't look for the .dir-locals.el file when loading files via TRAMP, as it might add significant delays. If this is the problem, you can set enable-remote-dir-locals to t to opt in.

  • Yes I saved the file in the project root. Also I'm not loading it via tramp – Rajkumar Natarajan Feb 15 at 22:20
  • Test with emacs -Q to see if it sets that variable when you open the file. If it works there, perhaps some part of your Emacs config is incorrect. – db48x Feb 15 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.