1
votes

I'm fairly new to Python virtualenvs and Emacs. I wanted to try and keep the python packages I use for checking and formatting code (flake8 and black) from the packages my actual code base uses (e.g. numpy etc.).

My thought was to create two separate virtualenvs (with pyenv), one for packages needed for Emacs extensions and another for the rest of my code.

I am using the Emacs package elpy; after installing blacken the auto-formatting by black worked right away. Even when I was in my project-venv and black was installed in my emacs-venv.

Flycheck was not so lucky; it has difficulty finding the flake8 executable. Putting the following in my Emacs init file does nothing; it appears to default instead to the python executable in my current virtualenv.

(setq flycheck-python-flake8-executable <PATH>) 

I can however get the proper executable by going through the Emacs interactive mode.

M-x flycheck-set-checker-executable RET python-flake8 RET <PATH>

I check the checker status and it says that 'it can be enabled' but when I use the interactive enable command it responds 'checker may not be used in this buffer'.

2

2 Answers

0
votes

keep the python packages I use for checking and formatting code (flake8 and black) from the packages my actual code base uses

It's by default this way. black and flake8 entry script shebangs specify absolute python path:

$ head -n1 $(which black)
#!/usr/bin/python3.8
$ head -n1 $(which flake8)
#!/usr/bin/python3.8
0
votes

I typed this question late at night and did not do a good job of explaining what I wanted. My goal was to have pip freeze not output python packages unrelated to my project's codebase.

After thinking about this more, I found a more direct way to get what I wanted (instead of having multiple virtualenvs).

  1. Install flake8 (or anything else) in the virtualenv of your project.

  2. Use pigar to generate your list of requirements based on imports.

Here's a short Makefile; make freeze.

# Use pigar to generate 'pip freeze' requirements based on imports, no comments.
freeze:
    pigar -p .tmpreqs --without-referenced-comments
    tail -n +3 .tmpreqs > requirements.txt
    rm .tmpreqs