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:

  1. 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.
  2. 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:
    1. 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)
    2. Create a symbolic link:  open up cmd, navigate to the above site-packages directory, and run this: mklink /J kivy ..\..\..\kivy\kivy. (preferable option)
  3. 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
  4. The site-packages directory should be added as a library by default, so you shouldn’t need to do anything on the Libraries tab
  5. 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 for figuring these out by looking at the Kivy windows batch file):
    1. GST_PLUGIN_PATH = C:\Kivy141\gstreamer\lib\gstreamer-0.10
    2. GST_REGISTRY = C:\Kivy141\gstreamer\registry.bin
    3. PATH = C:\Kivy141;C:\Kivy141\Python;C:\Kivy141\gstreamer\bin;C:\Kivy141\MinGW\bin;%PATH%
And that should do it! Create a new PyDev project, making sure to select your new Kivy Python interpreter as the interpreter for the project, add a new module, copy in the code from one of the examples (say, Paint Widget), and everything should work. PyDev shouldn’t give import errors on the Kivy imports, it should give you code completion when importing or using Kivy modules, and it should be able to run your Kivy-based applications!

Still having problems?

If you’re getting import errors in the PyDev editor:

  1. 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.
  2. 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
  3. 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:

  1. 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:
import os
for key in os.environ.keys()
    print “Key: ” + key + “, Val: ” + os.environ[key]

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.

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

  • Pavel

    Hi,
    thanks a lot for very helpful article, how to configure. It took me longer time to get it running: – I used first some preconfigured Eclipse Version, which did not have “Environment” Tab, then I forgot to change to my Kivy Version (151).
    Now it is running !!!

  • Nikos

    Hello there.

    Very good article but still I got issues… How did you make it run?
    I have changed all that you talk about but its still not working :S
    In libraries all the C:\Kivy\Python\lib… (where c:\Kivy is where i added the kivy package) are with lib small and not capital. I have changed all of them to capital and removed the ..\lib\.. ones but still it doesnt work. In addition the plat-win and lib-tk does not exist under Lib folder. Can you maybe add some screenshots of the step by step procedure?

    Thanx

  • Cam

    Hi Nikos,

    First things first: What exactly do you mean when you say it’s not working? At what point do you get stuck, and what are the error messages?

    I just checked and I actually don’t have the plat-win and lib-tk folders either, so it can’t be that.

    Unless you can give me some more info on what’s going wrong, my only advice would be to start over and make sure you follow my steps very carefully, as it has worked for me and at least one other reader.

  • Nikos

    Well sorry for that.. of course of course I should give more details :)
    First things first. Not working means I get import errors when trying to run a .py with a Kivy interpreter. So:
    a) I created a C:\Kivy folder and threw everything from Kivy in there (folders gstreamer, kivy, MinGW, Python and the bat, sh files)
    b) After that I created the “link” of the kivy/kivy folder through cmd as described in your text.
    c) In Eclipse menu, Window->Preferences->Interpreter-Python I added my new awesome interpreter called kivy and pointing to C:\Kivy\Python\python.exe. This step loaded the libraries etc but as you noticed as well, all of the ..\lib\.. are written “lib” instead of “Lib”, so I added the same folders and removed the “lib” ones respectively
    d) Next step: to configure the environments tab:
    I added in each one of them as they can be seen below:
    Name: GST_PLUGIN_PATH
    Value: C:\Kivy\gstreamer\lib\gstreamer-0.10

    Name: GST_REGISTRY
    Value: C:\Kivy\gstreamer\registry.bin

    Name: PATH
    Value: C:\Kivy;C:\Kivy\Python;C:\Kivy\gstreamer\bin;C:\Kivy\MinGW\bin;%PATH%

    (In addition I checked the *.bat file to see if there are differences of the actual values)

    I thought that would do the job… but nope. I get always the following error:
    File “C:\Users\..my.cool.user..\My Documents\Workspace\KivyExamples\src\main.py”, line 1, in
    from kivy.app import App
    File “C:\Kivy\Python\Lib\site-packages\kivy\app.py”, line 222, in
    from kivy.uix.widget import Widget
    File “C:\Kivy\Python\Lib\site-packages\kivy\uix\widget.py”, line 86, in
    from kivy.graphics import Canvas
    File “C:\Kivy\Python\Lib\site-packages\kivy\graphics\__init__.py”, line 76, in
    from kivy.graphics.instructions import Callback, Canvas, CanvasBase, \
    ImportError: DLL load failed: The specified module could not be found.

    So as you may understand I have NO IDEA why is this happening and what is going wrong :S

  • Cam

    I’ll take a closer look at this tomorrow (almost midnight here in Melbourne), but I do have a couple of ideas.

    First, I assume you’ve already gone through my “If you’re getting import errors” list? Especially points 2 and 3?

    Also, is your project configured to use the new interpreter that you created? Right-click on your project in the Package Explorer, and go to Properties, then click the PyDev – Interpreter/Grammar node, and make sure that the interpreter is set to ‘kivy’, and not some other regular installation of Python.

  • Nikos

    Hello Cam.

    Yes I have gone through the points you mention in case of import errors. As I said above I added all Lib folders (point 1) not only the /Lib/site-packages (as I mention on [c] above), and the interpreter points to kivy interpreter of course.

    No idea why I still get issues in imports and google was not helpful enough :)

  • Cam

    Sorry about the delay. I actually moved house over the weekend so it’s been a hectic few days.

    I’m starting to run out of ideas for your problem. Maybe at this point you should verify that your Kivy installation is valid, outside of Eclipse I mean. You could try running your program using the included .bat file, using any of the methods listed here (The “Right-click->Open With…” method is probably easiest).

    If the Kivy developers’ .bat file won’t run your program then clearly the problem is not to do with PyDev. If it does run though, then there must be some difference between the environment’s of the .bat file and PyDev. To determine exactly what the problem is, you could first try going through the .bat file to see exactly what it does, and make sure that you have analogous settings in PyDev. If nothing stands out from reading the .bat file, then you could try using various python commands (print environment variables etc) within each of the environments to try to figure out what’s different.

  • Nikos

    Thank you for your answer Cam..

    I have been in an out of several solutions but none would work with it. As a result I know type my programs and drag them to the bat file which makes them work like a charm..

    Thank you for your support man. Hope you will enjoy the new place.

    Cheers,

    P.S. Another question that does not regard this topic. Can someone with Kivy create absolutely custom graphics? Like his own designed buttons, menubars, etc etc?

  • Sirch

    Hi Cam,

    I configured Eclipse and pyDev, and created a new pyDev project.

    On the adding a module part, on the New Module window, which package and template should be choosen?

    Thanks!

  • Sam

    > an someone with Kivy create absolutely custom graphics? Like his own designed buttons, menubars, etc etc?

    Yes, that’s actually where Kivy shines most IMO. You get a very thin wrapper over OpenGL that makes it do pretty much anything you want graphics-wise

  • Cam

    Sirch, I don’t have PyDev handy at the moment and I can’t remember exactly what the window looks like, but you should just be adding it to your project’s main package (So it should be like MyProject->MyPackage->MyModule), and just add a blank file. The templates just provide you with some boilerplate code (like a class definition), and you’re going to paste in the Kivy test code anyway, so you don’t need to use any template.

  • Gareth Bell

    Hi Cam, Thanks very much for posting this – worked first time with 1.6.0 – Gareth

  • Daniel

    Hello Cam, i am facing the same problems you did and people in the irc channel pointed me to your website. Thank you a lot for posting your solution, I am going to try it right now. What I wanted to ask you is, did you actually find out how to use kivy as a separated python module with your current python interpreter? I am worried about installing other third party libraries on this second interpreter. I don’t know if you had to do anything like that. Thanks again.

  • Daniel

    Oh, and I just finished your method of installation and I am getting the same error that Nikos had:

    File “C:\Users\..my.cool.user..\My Documents\Workspace\KivyExamples\src\main.py”, line 1, in
    from kivy.app import App
    File “C:\Kivy\Python\Lib\site-packages\kivy\app.py”, line 222, in
    from kivy.uix.widget import Widget
    File “C:\Kivy\Python\Lib\site-packages\kivy\uix\widget.py”, line 86, in
    from kivy.graphics import Canvas
    File “C:\Kivy\Python\Lib\site-packages\kivy\graphics\__init__.py”, line 76, in
    from kivy.graphics.instructions import Callback, Canvas, CanvasBase, \
    ImportError: DLL load failed: The specified module could not be found.

    It works with the kivy.bat. I will try to investigate this. Thanks for the help!

  • Daniel

    Sorry for all the messages, maybe you should moderate them. But it is working perfetly now. Nikos probably did the same thing that I did, copied the Name/Value that you used in environment but set a different path for kivy (mine for example was on D: ). Thank you very much.

  • Cam

    Hi Daniel. Glad you got it working in the end. As I mentioned in the first part of the post, if you want to install Kivy as a standard module, you’ll need to also make sure that you install all of Kivy’s dependencies, which might be a bit tedious. As for adding further third-party libraries to your ‘second’ interpreter, which I’m assuming is your Kivy one, I don’t see any reason why that wouldn’t work. It’s a Python installation like any other, it just happens to have all the Kivy stuff already there.

  • Ben Woodward

    Any chance of the guide for Visual Studio? Thanks!

  • Cam

    Hi Ben. I’m flat out with uni work at the moment, but I might see if I can write something up next week.

  • Tim Swisher

    If anyone wants to use Pyscripter with Kivy all I did was modify the end of the kivy.bat file to:

    SET PYTHONHOME=%kivy_portable_root%Python
    :runkivy
    start “” “C:\Program Files (x86)\PyScripter\PyScripter.exe” –PYTHON27 –PYTHONDLLPATH “%kivy_portable_root%Python” %1 %2 %3 %4 %5

  • Ben Woodward

    Thanks Cam, I understand if you’re busy!

    I’ve got most of it working (I think), it’s just running the code, I’m not sure how to pass GST_PLUGIN_PATH and GST_REGISTRY from Visual Studio

  • Tom H.

    Hi,

    I just wanted to echo what Ben said, I’m trying to set up kivy to work with python tools for visual studio and not sure how to go about setting up the GST_* stuff.

    Thanks

  • Cam

    Hey everyone. Sorry for neglecting this a bit, I’m in the middle of my final uni assessments and exams at the moment.

    I’ve just written up a post on getting Kivy working in Visual Studio. The link is at the top of this page. Hope it helps!

  • Anne

    Tim,
    I have been trying to get pyscripter to work with kivy, no luck as yet.

    When you modified kivy.bat did you delete any lines or just add the “SET PYTHONHOME=%kivy_portable_root%Python
    :runkivy
    start “” “C:\Program Files (x86)\PyScripter\PyScripter.exe” –PYTHON27 –PYTHONDLLPATH “%kivy_portable_root%Python” %1 %2 %3 %4 %5″ script to the end?

    BTW I’m using kivy for python33

  • sandman42

    Hi,

    any clue for having autocomplete working with eclipse and ubuntu using the “Kivy Python for android VM ” available for download????

    Thanks

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>