(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:
- Place the contents of the Kivy zip file into a folder at the root of your hard drive. So you should have folders like C:\Kivy141\kivy\ and C:\Kivy141\Python\. I initially tried having it installed in C:\Kivy\Kivy-1.4.1\, but that seemed to cause problems, so just go with the above location.
- By default, the Kivy source is totally separate from Kivy’s Python interpreter (and all the Python libraries that Kivy uses), and adding ‘outside’ directories as Libraries doesn’t seem to work in PyDev. So your two options here are:
- Copy the directory C:\Kivy141\kivy\kivy\ into C:\Kivy141\Python\Lib\site-packages\ (not preferable, as you’re making a copy of the entire Kivy source)
- Create a symbolic link: open up cmd, navigate to the above site-packages directory, and run this: mklink /J kivy ..\..\..\kivy\kivy. (preferable option)
- Open up PyDev, click “Windows->Preferences”, select “Interpreter – Python” from the tree view on the left, and then click the “New…” button at the top right to add a new interpreter. Give your interpreter a name (I called mine kivy), and specify the executable as C:\Kivy141\Python\python.exe
- The site-packages directory should be added as a library by default, so you shouldn’t need to do anything on the Libraries tab
- Click over to the Environment tab, and use the “New…” button to add the following Name/Value pairs (credit goes to this Stack Overflow answer, which figured these out by looking at the Kivy windows batch file):
- GST_PLUGIN_PATH = C:\Kivy141\gstreamer\lib\gstreamer-0.10
- GST_REGISTRY = C:\Kivy141\gstreamer\registry.bin
- PATH = C:\Kivy141;C:\Kivy141\Python;C:\Kivy141\gstreamer\bin;C:\Kivy141\MinGW\bin;%PATH%
Still having problems?
If you’re getting import errors in the PyDev editor:
- Try going back into the interpreter settings, and adding a new Library entry for C:\Kivy141\Python\Lib\site-packages. I know it’s already automatically there, but you’ll notice that it has a lower case ‘lib‘ , whereas the actual Windows directory is a capitalised ‘Lib‘. I think it depends on your version of Windows/Eclipse/PyDev (not really sure) as to whether this is a problem or not, but it’s definitely helped me in the past at least once.
- Again from the interpreter settings, click the Apply button to force PyDev to refresh its list of what’s located in the Library folders you’ve told it about. You definitely need to do this if you add the Kivy source files to the site-packages directory (or link to them) after you’ve already set up the interpreter in PyDev
- As a more extreme version of the above, File->Restart PyDev. I know Eclipse can take ages to start up, but this definitely helps sometimes.
If your application is crashing at startup:
- Make sure you had the correct interpreter selected when you added the environment variables in step 5, and double check that you typed/copied them correctly. To be really sure, you can check your environment variables at run time by doing:
Hopefully I’ve covered everything. It certainly was a hassle to get everything up and running. If after reading this you’re still having trouble getting this to work, let me know in the comments and I’ll see if I can help. I might also put up a post on how to get this working in Visual Studio. Visual Studio was actually a lot easier, but I personally prefer working in PyDev, so for me it was worth the hassle.