It works perfectly, but only when ‘seriesModule_code.py’ file is renamed to ‘seriesModule.py’: essentially the ‘seriesModule.py’ source code file, serves as some sort of dependency for the ‘seriesModule.dll’.
When ‘seriesModule_code.py’ remains with such a name, then a message is raised:
‘No module named seriesModule’ on line 14 (import seriesModule)
# gets the saved Grasshopper file location and adds it to the search path
ghcanvas = ghenv.Component.OnPingDocument()
if ghcanvas and ghcanvas.IsFilePathDefined:
import os, sys
folder = os.path.split(ghcanvas.FilePath)[0]
if not folder in sys.path:
sys.path.append(folder) # adds location to sys.path, if not present
# adds a reference to the seriesModule.dll file
import clr
clr.AddReference("seriesModule")
# now we can import the seriesModule module as though it was text
import seriesModule
a = seriesModule.ThreeSeries(10) # this is code from that module
At least this is the case on my PC.
Is there a way, to compile .dll to .py, and remove the source .py file from that folder?
I am grateful for any input from your side.
Sorry for bringing such an old topic.
I attached your files in this topic as well. compiledPy.zip (6.1 KB)
Not sure if this is what you are after, but, sure thing, clr.CompileModules() takes a list of files. Would adding the other file to the list work for you?
Or, maybe I’m confused and didn’t get the gist of the question.
Well that would be because seriesModule_code is the Python name of that module, right?
Thanks,
Giulio
–
Giulio Piacentino
for Robert McNeel & Associates giulio@mcneel.com
Hi @piac ,
Thank you for replying.
Sorry, I seemed to have explained it badly.
What I meant is that the .dll and .py files of the exactly same name need to be in the same folder.
In this case those are seriesModule.dll and seriesModule.py.
Otherwise an error will be raised:
‘No module named seriesModule’ on line 14 (import seriesModule)
This hinders the intent of code hiding, due to necessity of the .py file being in the same folder as the .dll file.
Or is this only the case on my PC?
After you used clr.CompileModules(), you can delete the .py file, and the implementation of it can be used, provided that clr.AddReference() is first loading the .dll. I don’t think it matters, what the name of the .dll is, but it must be loaded first. Does it make sense?
This is all IronPython, though – I don’t know all details. Most of this stuff, I discovered with trial-and-error as well.
Hi @piac,
I haven’t compiled the .dll by myself. I am using the one you compiled (seriesModule.dll).
The name of the .dll does not matter and can be changed. The problem I have is that the source code .py file name can’t be changed, and it needs to reside in the same folder as the .dll. Otherwise a ‘No module named seriesModule’ on line 14 (import seriesModule) error is thrown.
When you say: dll must be loaded first: I am using your code from the first post of this topic. Should the code be changed in some way?
Hi @piac ,
Thank you for the patience and detailed reply.
Something must be wrong with my PC then. Interestingly I tried on Windows 10 and 7 with Rhino 6 and Rhino 7, and when I repeat the steps from your last reply, I still get an error on the line 14: No module named seriesModule. The error goes away only when I have in the same folder the seriesModule.py.
I guess I would need to do some googling.
Thank you for the help once again.