Python Notes

Table of Contents

Toolset

  • Emacs
  • jedi.el
  • pytest
  • virtualenv
  • virtualenvwrapper
  • virtualenvwrapper.el
  • flycheck (for Emacs)
  • pylint
  • Multiple terminal windows for the following helpers:
    • ipython
    • git
    • A running py.test instance so I can run my unit tests every time I save a file.

Setting Up A Development Environment On Windows

The only tool that changes on Windows is my terminal editor. My preference is Cmder running a Powershell instance.

This works pretty well for me but I learned a few lessons when setting this up:

sniffer Isn't Usable on Windows

Sniffer hung for me every time I tried to kill it and it wasn't able to see when a file was changed, even after I install pywin32.

I ended up using py.test instead and am very happy with the results.

Jedi's A Little Funny

I've had issues starting the jedi server automatically, so I always run this command if I'm going to be working on some Python code:

  • M-x jedi:start-dedicated-server

Virtualenv And Cmder Don't Work Very Well Together

I got an error stating that the Cmder prompt was static, so I had to make a change to c:\tools\cmder\vendor\profile.ps1. Change the last line from this:

Set-Item -Path function:\prompt  -Value $Prompt -Options ReadOnly

…to this:

Set-Item -Path function:\prompt  -Value $Prompt

After that you should be able to activate your virtualenv profile like so:

PS C:> . .\vms\py2\coolproject\Scripts\activate.ps1

virtualenvwrapper Doesn't Work On Windows

The default virtualenvwrapper script doesn't work at all on Windows, and there are a few alternatives. The best option that I've been able to find by far that also works with Powershell is regisf/virtualenvwrapper-powershell.

Code Linting In Emacs

First here's the prereqs:

  1. Install virtualenv, virtualenvwrapper, flycheck, and virtualenvwrapper.el.
  2. Create a virtualenv for your project.
  3. Install pylint or whatever in that virtualenv.

Now turn on code linting like so:

  1. Open your python file in Emacs
  2. Make sure you're using the correct virtualenv by executing the M-x venv-workon command.

And that's it. Now your code will be linted.

Per-Directory Variables

Reference: Per-directory local variables

Create a file titled .dir-locals.el in the root folder of your project and then put something in there like this:

((python-mode . ((eval . (venv-workon "robot")))))
((python-mode
  (flycheck-checker . python-flake8)))

Last Updated .