Possibility to run Compiled Python Script (.pyc)?

Hi everyone,

I was wondering if there was a possibility to launch .pyc scripts in the Run Python script menu ? Or is there another way to do it ?

Thanks,
Nathan

Hi @Nathan_Clero

RhinoPython runs on IronPython. As such, it does not use .pyc files: it does not create them and cannot run them. They are the expression of CPython, the more canonical implementation of Python.

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Hi @piac,

Thanks for your reply, I’m sorry I just saw it. I tried a workaround but yes it definitely seems that there is no way to use .pyc …

I tried this:

  • file 1: test.pyc

def test(a):
print a

  • file 2: RunTest.py

import test
test.test(‘Hello’)

When I run RunTest.py, it works in Python shell, but with Rhino I get “Message: No module named test” (referring to the first line of file 2) …

Thanks anyway !

Nathan

1 Like

Hi @piac,

Just a few last questions.
I’ve been thinking about compiling my script into a .dll and then call this .dll inside RunScript.py that will be called by Python. I tried with a simple MainScript.py only containing "def test(): print ‘Hello’ " that I compiled using “ipy.exe pyc.py”. However, I couldn’t manage to make it work so far. I’ve read a thousand forums and stuff and I begin to get completely lost between ctypes, clr, cdll, ordinal values, changed function names, etc …

Do you know if this is possible ? If yes, could you explain me or redirect me to a clear article about it ?

Thanks a lot,
Nathan

It is indeed, you can compile to a .dll using the native functions of IronPython and .NET. See these articles:

http://www.voidspace.org.uk/ironpython/dark-corners.shtml#static-compilation-of-python-code
and:

Hope that helps.

2 Likes

Well, I tried so many complicated things, and this simple thing works like a charm … Thanks a lot @AndersDeleuran !!

Even if this is not the good topic for that, here a little summary (even if it’s already simple):

Create this little test script
MainScript.py

def test():
print “This is simple actually”

Create the following script and Run it with ipy.exe (path\to\ipy.exe CompLib.py)
CompLib.py

import clr
clr.CompileModules(“MainScript.dll”, “MainScript.py”)

In the end Run the following PythonScript with Rhino
RunScript.py

import clr
clr.AddReference(‘MainScript.dll’)
import MainScript as MS
MS.test()

Thanks again !
Nathan

1 Like

Yes, clr.CompileModules() will work.

Additionally, you can use that functionality to create .ghpy files (Python version of .gha files). The tutorial is here: Tutorial: creating a Grasshopper component with the Python GHPY compiler

1 Like