architecture-building-systems / revitpythonshell

An IronPython scripting environment for Autodesk Revit and Vasari
MIT License
517 stars 118 forks source link

[question] use anaconda/pip libraries in RPS #105

Open amastrobera opened 4 years ago

amastrobera commented 4 years ago

Does RPS runs only on IronPython or is it possible to include (CPython) libraries from Anaconda / PIP ? I believe only Python2 is allowed.

I tried to tweak the Configuration --> Search Path and add my C:\Users\angelo\Anaconda2\Lib\site-packages but I get errors when trying to use it.

daren-thomas commented 4 years ago

What are the exact errors you get?

Pure python libraries should not be a problem - adding libraries that include native code (*.pyd) or similar is much more tricky.

Your best bet is probably to shell out (subprocess module) to a CPython script and handle input / output...

amastrobera commented 4 years ago

on the windows command prompt it's all clean

conda install -c conda-forge numpy
python -c "import numpy" # no error

on the RPS

IronPython 2.7.7 (2.7.7.0) on .NET 4.0.30319.42000 (64-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\angelo\Anaconda2\Lib\site-packages\numpy\__init__.py", line 142, in <module>
  File "C:\Users\angelo\Anaconda2\Lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
  File "C:\Users\angelo\Anaconda2\Lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
  File "C:\Users\angelo\Anaconda2\Lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
  File "C:\Users\angelo\Anaconda2\Lib\site-packages\numpy\core\__init__.py", line 26, in <module>
ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: cannot import multiarray from numpy.core

Yes, I have used subprocess before. I think we can do better if we manage to import some python2 libraries

daren-thomas commented 4 years ago

so here's the thing. numpy is one of those libraries that aren't pure python. This link here might help you, but it looks a bit like a rabbit hole: https://stackoverflow.com/questions/29397540/how-to-install-numpy-and-scipy-for-ironpython27-old-method-doesnt-work

htlcnn commented 4 years ago

@amastrobera I guess you would find answer at pyRevit repo: https://github.com/eirannejad/pyRevit because @eirannejad managed to create CPython engine to run inside .NET environment. I haven't digged into where he wrote it, but you might ask him.

amastrobera commented 4 years ago

@daren-thomas I am trying that. thanks for the link.

Installing this ironpkg, gives an error ...

C:\Users\angelo>ironpkg scipy
Traceback (most recent call last):
  File "C:\Program Files (x86)\IronPython 2.7\ironpkg-script.py", line 10, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\enstaller\main.py", line 363, in main
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\enstaller\indexed_repo\chain.py", line 27, in __init__
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\enstaller\indexed_repo\chain.py", line 67, in add_repo
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\enstaller\utils.py", line 94, in write_data_from_url
urllib2.URLError: <urlopen error
<urlopen error [Errno errors while performing handshake: ] System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted
   --- End of inner exception stack trace ---
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
   at IronPython.Modules.PythonSocket.ssl.do_handshake()>
Cannot open URL:
    http://www.enthought.com/repo/.iron/eggs/index-depend.txt>
amastrobera commented 4 years ago

@daren-thomas

... but I had better luck with this old post. I run the script from Tess on these eggs of numpy and scipy.

I now have the IronPython version of Numpy and Scipy. What should I change in Environment Variables / Revit Configuration / etc to be able to import numpy ? I still get this error

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\numpy\__init__.py", line 155, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\numpy\core\__init__.py", line 6, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\site-packages\numpy\core\multiarray.py", line 6, in <module>
IOError: System.IO.IOException: Could not add reference to assembly NumpyDotNet
   at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
   at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object[] references)
   at Microsoft.Scripting.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
   at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
   at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
   at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
   at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
   at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
   at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
   at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
   at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
   at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
   at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
   at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
   at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
   at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
   at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
   at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
   at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
   at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
   at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
   at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
   at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
   at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
   at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass27_0.<RunOneInteraction>b__0()

Question for later.

Now, what happens if I want to import a library that uses numpy, but it's not in IronPython, such as osgeo ? Such a library will automatically look for the CPython/other version of numpy, unless I add IronPython2.7/Lib/site-package in the PYTHONPATH, but this may be misleading ...

... and anyway,