shiena / Unity-PythonNet

https://zenn.dev/shiena/articles/unity-python
MIT License
20 stars 1 forks source link

Adding new python file #7

Closed wightwhale closed 1 month ago

wightwhale commented 1 month ago

I have attempted to add a python file and have it execute but I continually get an error. The file is the same as plot_random but in a new folder with a new name. I'm unable to get a new file to run. Please advise as I'm likely missing a step and I'm not understanding how to add additional python files.

I changed the code in TestCallPython.cs

"PythonException: No module named 'plot_random_duplicate'"

private const string PythonProject = "new_folder";

I also added it here in PycResetter.cs

            var targetFolders = new[]
            {
                "Assets/StreamingAssets/python-3.11.3-embed-amd64/*",
                "Assets/StreamingAssets/myproject/*",
                "Assets/StreamingAssets/test_package/*",
                "Assets/StreamingAssets/new_folder/*"
            };
shiena commented 1 month ago

If you want to add a new python script folder, add the path to PythonPath.

Index: Assets/Scripts/PythonLifeCycle.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Assets/Scripts/PythonLifeCycle.cs b/Assets/Scripts/PythonLifeCycle.cs
--- a/Assets/Scripts/PythonLifeCycle.cs (revision a7663ae8206a2175ad727f468dc9923e389b5b79)
+++ b/Assets/Scripts/PythonLifeCycle.cs (date 1726253334703)
@@ -30,6 +30,7 @@
             var appendPath = string.IsNullOrWhiteSpace(appendPythonPath) ? string.Empty : $"{Application.streamingAssetsPath}/{appendPythonPath}";
             var pythonPath = string.Join(";",
+                $"{Application.streamingAssetsPath}/new_folder",
                 $"{appendPath}",
                 $"{pythonHome}/Lib/site-packages",
                 $"{pythonHome}/{PythonZip}",
                 $"{pythonHome}"
wightwhale commented 1 month ago

Unfortunately I am still confused. If I add a .py file to the StreamingAssets/myproject folder then it is ok to move that file elsewhere and I get no errors. However if I start with the .py file in another folder such as the new_folder then I get the same error.

Example: If I put new.py into the StreamingAssets/new_folder it is not found. I then move the new.py into the StreamingAssets/myproject folder and it is found. I then move new.py back to the StreamingAssets/new_folder and the program continues to run.


using Python.Runtime;
using System;
using System.Xml.Linq;
using UnityEngine;
using static UnityEngine.UIElements.UxmlAttributeDescription;

namespace UnityPython
{
    public static class PythonLifeCycle
    {
        private const string PythonFolder = "python-3.11.3-embed-amd64";
        private const string PythonDll = "python311.dll";
        private const string PythonZip = "python311.zip";
        private const string PythonProject = "new_folder";

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        private static void PythonInitialize()
        {
            Application.quitting += PythonShutdown;
            Initialize(PythonProject);
        }

        private static void PythonShutdown()
        {
            Application.quitting -= PythonShutdown;
            Shutdown();
        }

        public static void Initialize(string appendPythonPath = "")
        {
            var pythonHome = $"{Application.streamingAssetsPath}/{PythonFolder}";
            var appendPath = string.IsNullOrWhiteSpace(appendPythonPath) ? string.Empty : $"{Application.streamingAssetsPath}/{appendPythonPath}";
            var pythonPath = string.Join(";",
                Application.streamingAssetsPath + "/new_folder",
                $"{appendPath}",
                $"{pythonHome}/Lib/site-packages",
                $"{pythonHome}/{PythonZip}",
                $"{pythonHome}"
            );

            var scripts = $"{pythonHome}/Scripts";

            var path = Environment.GetEnvironmentVariable("PATH")?.TrimEnd(';');
            path = string.IsNullOrEmpty(path) ? $"{pythonHome};{scripts}" : $"{pythonHome};{scripts};{path}";
            Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("DYLD_LIBRARY_PATH", $"{pythonHome}/Lib", EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", $"{pythonHome}/{PythonDll}", EnvironmentVariableTarget.Process);
#if UNITY_EDITOR
            Environment.SetEnvironmentVariable("PYTHONDONTWRITEBYTECODE", "1", EnvironmentVariableTarget.Process);
#endif

            PythonEngine.PythonHome = pythonHome;
            PythonEngine.PythonPath = pythonPath;

            PythonEngine.Initialize();
        }

        public static void Shutdown()
        {
            PythonEngine.Shutdown();
        }
    }
}
shiena commented 1 month ago

I have committed the sample code to the add-folder-sample branch, see the 2c9492c9b2d87093034cbff1993ab974cc943987 commit. Perhaps restart Unity and the python script should work. For some reason, if I change the script after running PythonEngine.Initialize(), the old code is still there.

shiena commented 1 month ago

No reply, so I'm closing.