How to use Python Tools for Visual Studio to develop and run Kivy Applications

This is a companion post to go with this one. From what I have heard, PTVS has been gaining popularity recently, so there have been a few requests for this post. Here it is!

I’m going to assume that you’ve already got PTVS installed, and Kivy extracted. I myself am running PTVS version 2, on Visual Studio Premium 2012, and I have Kivy located at C:\Kivy141\. You’ll obviously need to change some of the paths below, depending on where you have put Kivy.

  1. Create a new Python project, with a blank source file. You’ll see on the left that there is an item in the Solution Explorer for Python Environments, but if you try to play with that now, you’ll discover that you can’t select your Kivy installation.
  2. So we need to add it. Go to Tools->Options, and then scroll down to Python Tools->Environment Options (this may show as Interpreter Options, depending on your version). You’ll see there the settings for your existing Python interpreter, named something like “Python 2.7″. So click Add Environment, name it something creative like “Kivy Interpreter”, fill out the settings and click OK:
    • Path: C:\Kivy141\Python\python.exe
    • Windows Path: C:\Kivy141\Python\pythonw.exe
    • Library Path: C:\Kivy141\Python\Lib
    • Architecture: x86
    • Language Version: 2.7
    • Path Environment Variable: PYTHONPATH
  3. Now in the Solution Explorer, right click Python Environments->Add/Remove Python Environments…, and you should be able to check the box for your new “Kivy Interpreter”. Now if you expand the new node in the Solution Explorer, you’ll see a bunch of libraries included (like PIL and pygame), which are Kivy’s dependencies, but we’re still missing the Kivy source itself.
  4. By default, the Kivy source is totally separate from Kivy’s Python interpreter (and all the Python libraries that Kivy uses), so you need to add a Search Path. Again in the Solution Explorer, right click Search Paths->Add Folder to Search Path…, then browse to C:\Kivy141\kivy\, and click Select Folder. At this point, you can copy in some simple Kivy code, like for example, the Paint Widget, and try running it. The code will crash with a DLL error, but at least the imports (almost) work!
  5. Now here things get a little bit hack-ish. Kivy needs some environment variables to run properly, but PTVS doesn’t support arbitrary environment variables yet. However, what we can do is set them manually as the very first thing our application does. Right at the top of your code, before you import any kivy modules, add this:

import os
os.environ['GST_PLUGIN_PATH'] = r”C:\Kivy141\gstreamer\lib\gstreamer-0.10″
os.environ['GST_REGISTRY'] = r”C:\Kivy141\gstreamer\registry.bin”
os.environ['PATH'] = r”C:\Kivy141;C:\Kivy141\Python;C:\Kivy141\gstreamer\bin;C:\Kivy141\MinGW\bin;%PATH%”

It’s not an ideal solution, but without a proper way to set environment variables in PTVS, the best we can do is set them at run-time. Edit: As some commenters have noted, you can also solve this problem by setting the environment variables globally, but I prefer to set them within the project, because I think it keeps your system cleaner, and it’s also one less step to get your code running if someone else checks it out from a repository. I won’t put instructions here for setting them globally, as if you don’t already know how to do it, you should probably stick to my method above.

And that should do it! Run your code, and you should see your blank widget pop up, with a Python console behind it.

So there you have it. I mostly use PyDev myself, but with PyCharm now having a free community edition I’m going to give that a go too. If someone asks for it, or if I find myself motivated, I might write up a similar post to this one for PyCharm at some point in the future.

 

How to use PyDev to develop and run Kivy Applications (on Windows)

(Update: I now have an equivalent post to this one for Python Tools for Visual Studio. If you’re using PTVS, you want to go here.)

So I found this awesome looking project called Kivy, which is a Python UI library that works across Windows, OSX, Linux, IOS and Android (pretty impressive!), and I decided I wanted to give it a try. Now I know a lot of people prefer working with editors like Vim and invoking their compilers/interpreters/debuggers manually, but I’m very much an IDE kind of a guy. So I set about trying to get Kivy up and running in my favourite Python IDE, PyDev.

I ran into trouble pretty quickly. Because Kivy has a lot of dependencies on other Python libraries, the developers have thoughtfully packaged Kivy up with everything it needs, including a Python interpreter. The down side of this is that it makes it a pain to install Kivy as an external library into an existing Python installation if you already have one, as you’d need to know exactly which files to copy over. The Kivy README says:

Install Kivy as a standard python module
========================================

Please refer to the install instructions in the complete README :
* Inside the kivy folder inside this one
* Kivy documentation at http://kivy.org/docs/

However, the ‘complete README’ doesn’t exist, and the online docs say nothing about how to install Kivy as a standard Python module. I tried asking on the mailing list (as had others in the past), and didn’t get much of a response.

So if you want to use Kivy from PyDev, you can’t use your existing Python interpreter. Instead, you need to set up and configure a new interpreter in PyDev. Without further ado, here are the step-by-step instructions, beginning right after you’ve downloaded the zip file from the Kivy website: Read more…

Fancy Cats

Fancy Cats

 

My partner, Bianca, has been busy over at her foodie blog, The Cooking Capers. She’s also spent a fair bit of time looking around at other blogs, and told me that she wanted something a bit different to the standard options for WordPress categories widgets.

Having learned the basics of PHP earlier this year, I set about creating one for her. It only took roughly one evening and one afternoon to create, but it’s now finished. It uses AJAX to expand the category with a list of posts when clicked on. If you want to know more (you do!), there’s more info here, or head over to the blog I linked above to see it in action.