I want to embed python in C# code of Unity3D project. And I also want to ship python and pythonnet with the project instead of installing it on my OS.
_I tried embedding IronPython before. It works but it couldn't read .pyd file which is important for my project. As I need to import dlib.pyd package for machine learning function._
Here's what I've tried so far:
I installed pythonnet and python via NuGet package manager in Visual Studio 2017.
Here's the command I used:
Install-Package python -Version 3.5.4
Install-Package pythonnet -Version 2.3.0-py35-dotnet
Here's the code I'm trying to run:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class TestLib : MonoBehaviour {
// Use this for initialization
void Start () {
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
Debug.Log(np.cos(np.pi * 2));
dynamic sin = np.sin;
Debug.Log(sin(5));
double c = np.cos(5) + sin(5);
Debug.Log(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Debug.Log(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
Debug.Log(b.dtype);
Debug.Log(a * b);
}
}
}
The error is that it doesn't know Py. I don't know what to import. I tried using Python.Runtime; and it didn't work.
I'm suspecting that it's because I didn't provide the path to the Python.Runtime.dll yet.
I have a folder named Packages/ inside my project that contains the installation of python and pythonnet.
My question is How do I import python and pythonnet from this Packages/ folder?
I got it now. I just need to move Packages/ folder into Assets/ of my Unity project.
I experimenced the same error. But when I follow you instructions, Unity crashes when entering playmode.
In VS 2017, used your commands to install the packages.
Moved entire packages folder into Assets folder
Added Monobehaviour to scene
Press play,
Unity crashed
Any details you left out by any chance?
@AdamBebko I'm having the same issue. Have you solved it?
Sorry. I don't remember anything. It's been a long time. Nowadays I don't need to use this python in unity because I can just use Socket.IO or ZeroMQ to transfer data from python to Unity C# instead. Good luck with your error.
Any solution to this problem? I found a related tutorial on using pythonnet in Unity, but it gives the same error as mentioned here: https://pandacoredata.readthedocs.io/en/latest/tutorial/unity.html
No, I wasn't able to fix it. I was trying to read .NPZ in unity, I ended up
just writing a script to convert to JSON in python, and then read the JSON
in Unity.
On Mon, Jan 20, 2020 at 4:56 AM Huang Junye notifications@github.com
wrote:
Any solution to this problem? I found a related tutorial on using
pythonnet in Unity, but it gives the same error as mentioned here:
https://pandacoredata.readthedocs.io/en/latest/tutorial/unity.html—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pythonnet/pythonnet/issues/699?email_source=notifications&email_token=AKKFU46GGBFKB6JJQ3KWSM3Q6VYNPA5CNFSM4FIROQMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJMA7PI#issuecomment-576196541,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AKKFU434JGMAYJKG5UGGLH3Q6VYNPANCNFSM4FIROQMA
.
I am experiencing the same problem.
@off99555 Hi i have been trying to do the same thing that you have done in unity however i have gotten this error:
PrecompiledAssemblyException: Multiple precompiled assemblies with the same name Python.Runtime.dll included for the current platform. Only one assembly with the same name is allowed per platform. Assembly paths: Assets/Plugins/Python.Runtime.dll, Assets/Python.Runtime.NETStandard.3.7.1/lib/netstandard2.0/Python.Runtime.dll
Also did you only import the nuget package for pythonnet (Python.Runtime.NETStandard) or did you import other packages as well.
Thank you
Most helpful comment
I got it now. I just need to move
Packages/folder intoAssets/of my Unity project.