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, but we’re still missing the Kivy source itself.
- 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!
- 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. 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.