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.
- 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.
- 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
- 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. 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, but at least the imports work!
- 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:
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.
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.