Distributing Python code with M2 package

31 views
Skip to first unread message

Federico Galetto

unread,
Nov 6, 2025, 11:44:45 AMNov 6
to Macaulay2
Hello, the Python package was recently brought to my attention. In the example I was given, some Python code is used to perform certain computations in a way that is much faster than similar code I wrote for one of my M2 packages. Which brings me to my question...

Suppose I have some Python code to perform some computations. Is it possible to write a Macaulay2 package and include the Python code in one of the package files, then call upon that Python code using the Macaulay2 Python package?

Thanks!
Fred

Doug Torrance

unread,
Nov 6, 2025, 12:22:26 PMNov 6
to Macaulay2
Yes, absolutely!

I'm actually making some finishing touches to a major revamp of the Python package that should be included with the upcoming Macaulay2 1.25.11 release.  They should make running Python code even simpler.  In the meantime, here's one way to do with the current state of the package:

Let's say we have the following Python code in the file foo.py:

def f(x):
    return x**2

y = f(5)

And let's say that foo.py is in the directory /my/python/dir on our system.  In M2, we can do the following:

i1 : needsPackage "Python";

i2 : sys = import "sys";

i3 : sys@@"path"@@append "/my/python/dir";

i4 : foo = import "foo";

(Note that @@ is playing the role of Python's . -- we're not able to overload . in Macaulay2 and @@ has similar precedence.)

foo is now a Python module object, and we can access all of its variables:

i5 : foo@@y

o5 = 25

o5 : PythonObject of class int

i6 : foo@@f

o6 = <function f at 0x73916c761440>

o6 : PythonObject of class function

i7 : foo@@f 7

o7 = 49

o7 : PythonObject of class int

And to convert to a Macaulay2 object, use "value":

i8 : value oo

o8 = 49

Federico Galetto

unread,
Nov 6, 2025, 12:44:41 PMNov 6
to Macaulay2
That's great, and thanks for the quick response!
This is not urgent so I will definitely wait for the new version.

Another question in the meantime. In your example, we know the path of foo.py on our system. Could the path be given relative to the M2 installation? I am thinking something like:

../Packages/MyPackage

so that foo.py is distributed as part of MyPackage. I envision a scenario where the Python code is called upon when the user calls a method with an option like "UsePythonCode=>true" and then in this case all the M2 to Python back to M2 translation is done behind the scenes. This of course assumes Python is installed on the user's system in a way that M2 can call upon (would it be possible to check and return an error if it isn't?).

Cheers,
Fred

Doug Torrance

unread,
Nov 6, 2025, 12:55:04 PMNov 6
to Macaulay2
With the "import" code from my previous reply, we need to know the absolute path.  But we can find that out in Macaulay2 using "toAbsolutePath", so that's not a problem.

By default, Macaulay2 has been built with Python support for the last several releases, so this should work for most users out of the box.  But it's still possible to compile M2 without Python support.  One way to check (which is what I do in the Python package) is to evaluate:

Core#"private dictionary"#?"pythonTrue"

It will be true if Python is available and false if it's not.

Federico Galetto

unread,
Nov 6, 2025, 10:23:09 PMNov 6
to Macaulay2
Wonderful, thank you!

I may have future questions when I actually get to try this, but this is great to know for planning purposes.

Reply all
Reply to author
Forward
0 new messages