Importing Complex Python Modules into Processing

edited September 2016 in Python Mode

Hello people,

I'm currently using PyGame to build games on python, however exporting becomes rather difficult as Py2Exe and Py2App are almost the only ways to do so and are not very effective. I tried to using the wonderful exporting mechanic in Processing (in Python mode), but this opened a Pandora's box of problems.

  1. How do you import modules in Processing.py ? I read that if the module is in the form of a PY file it is simply to be located in the same folder as the sketch. But some modules, like PyGame, are more complex and require an installer or a wheel (WHL file, which is installed through PIP).

  2. During some testing, I tried to export a simple one-line program ( print('a') ) but the 'application.windows64' folder was missing an EXE file. I'm not an expert, but I think that might be a problem :)

Thanks in advance!!

Tagged:

Answers

  • Python Mode is based on Jython, which in turn is written in Java.
    So we can only use compiled Java libraries in Jython, sorry. :(

    https://en.Wikipedia.org/wiki/Jython

  • It seems as though I can import libraries from external PY files though...

  • As long as it's a "pure" Python library, not some "compiled" 1, we can use it in Jython.

  • Hey follks. Olher than adding a 'x.py' tab and using 'form x import *' how to add Python libraries to the Jython that makes Processing Python Mode work?

  • edited August 2017

    PDE recognizes ".jar" files inside sketch's subfolder "code/". ~O)
    Any compiled code from any JVM language can be import this way. \m/

  • edited August 2017

    @villares -- it seems like you want to install a library into the mode itself, so that the library is then available for any sketch to import on that machine without the code needing to be present in each sketch folder?

    This is untested, but Jython is installed in Processing/modes/PythonMode/mode/jython.jar -- by running that jar and interacting with it directly you might be able to install setuptools and add libraries directly to that Jython instance of python, e.g. https://stackoverflow.com/a/6787069/7207622 or http://secretmustache.com/2010/04/jython-how-to-install-python-libraries.html

    If you try this and run into trouble it might be worth asking @jdf if it is possible / opening an issue on https://github.com/jdf/processing.py

  • A follow-up to the original question:

    In case it isn't clear, here is an example of adding a complex module to a processing.py sketch as long as it and all its dependencies are pure python

    You can follow this method with simple one-file python modules as well.

    Jinja2 web templates in a Processing.py sketch

    1. create a sketch JinjaTest
    2. get the library
    3. get any requirements
      • in jinja-master, check setup.py for install_requires
      • it requires MarkupSafe; download that
      • move the jinja2 and markupsafe folders into the sketch folder
    4. test it
      • choose an example from the jinka-master examples folder, e.g. inheritance.py
      • cut and paste it into the main sketch tab
      • run the processing.py sketch JinjaTest

    Example code from inheritance.py:

    from jinja2 import Environment
    from jinja2.loaders import DictLoader
    
    env = Environment(loader=DictLoader({
    'a': '''[A[{% block body %}{% endblock %}]]''',
    'b': '''{% extends 'a' %}{% block body %}[B]{% endblock %}''',
    'c': '''{% extends 'b' %}{% block body %}###{{ super() }}###{% endblock %}'''
    }))
    
    print env.get_template('c').render()
    

    The example sketch (using Jinja (using MarkupSafe)) runs -- correct output appears in the console window.

    [A[###[B]###]]

  • I actually added support for a "site-packages" directory recently. Just create a folder called "site-packages" in your skectchbook's "libraries" directory, and place Python modules in there. They'll be available to every sketch.

  • I have posted Processing.py Import Examples -- a collection of sketches demonstrating how to import modules and packages into Python Mode for Processing.

    Examples currently include:

    • unittest for unit testing
    • pureyaml for YAML import/export and parsing
    • pyparsing for writing custom parsers
    • jinja2 for templating

    These are all self-contained sketches with all resources in the sketch folder -- however you may copy the module/package resources into Processing / libraries / site-packages in order to make them available site-wide.

Sign In or Register to comment.