Python Notes

Table of Contents


  • 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.

Solving PYTHONPATH Issues

I had an issue lately where a Python script in a virtualenv was not using the versions of the libraries that were installed in that virtualenv. Here's some tips that I learned when debugging the issue.

Checking The Instance Of Python Being Executed

First, are you sure that you're running the Python executable that you think you're running?

The proper version of Python may not be executed just because you're running a script within virtualenv. This is easy to test by simply checking the process table. Also, you can try prepending your script like this:

python -m

This was my big mistake the last time I had PYTHONPATH issues. After checking the version of Python that was running when I executed my script I noticed that it was from a completely different virtualenv.

Checking The Loaded Libraries At Runtime

If you'd like to see what libraries are being loaded and from where, simply prepend the following to the command that you run to execute your Python script:

python -v -m

Checking The Library Version In The REPL

Of course, you can also check library versions in the Python REPL like so:

python -c "import robot; print(robot.__version__)"

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 in Emacs

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")))))
  (flycheck-checker . python-flake8)))

Last Updated .