We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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).
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!!
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?
PDE recognizes ".jar" files inside sketch's subfolder "code/". ~O)
Any compiled code from any JVM language can be
import
this way. \m/@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
JinjaTest
jinja2-master
, copy the sub-folderjinja2
into the sketch folderjinja-master
, checksetup.py
forinstall_requires
JinjaTest
Example code from
inheritance.py
:The example sketch (using Jinja (using MarkupSafe)) runs -- correct output appears in the console window.
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 testingpureyaml
for YAML import/export and parsingpyparsing
for writing custom parsersjinja2
for templatingThese 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.