## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
lolibinve
@lolibinve
torch.tensor(data[:-1]).float().view(1, num_time_steps - 1, 1) can not be executed, any solution?
the same environment ,vscode can execute,

import torch
import torch.nn as nn
import numpy as np
import torch.optim as optim
from matplotlib import pyplot as plt

input_size = 1
batch_size = 1
hidden_size = 16
num_layers = 1
output_size = 1

class Net(nn.Module):

def __init__(self):
super().__init__()

self.rnn = nn.RNN(
input_size=input_size,       # feature_len = 1
hidden_size=hidden_size,     # 隐藏记忆单元个数hidden_len = 16
num_layers=num_layers,       # 网络层数 = 1
batch_first=True             # 在传入数据时,按照[batch,seq_len,feature_len]的格式
)

for p in self.rnn.parameters():  # 对RNN层的参数做初始化
nn.init.normal_(p, mean=0.0, std=0.001)

self.linear = nn.Linear(hidden_size, output_size) # 输出层

def forward(self, x, hidden_prev):
"""
x：一次性输入所有样本所有时刻的值(batch,seq_len,feature_len)
hidden_prev：第一个时刻空间上所有层的记忆单元(batch, num_layer, hidden_len)
输出out(batch,seq_len,hidden_len) 和 hidden_prev(batch,num_layer,hidden_len)
"""
out, hidden_prev = self.rnn(x, hidden_prev)

# 因为要把输出传给线性层处理，这里将batch和seq_len维度打平
# 再把batch=1添加到最前面的维度（为了和y做MSE）
# [batch=1,seq_len,hidden_len]->[seq_len,hidden_len]
out = out.view(-1, hidden_size)
#[seq_len,hidden_len]->[seq_len,output_size=1]
out = self.linear(out)

# [seq_len,output_size=1]->[batch=1,seq_len,output_size=1]
out = out.unsqueeze(dim=0)

return out, hidden_prev

# 训练过程

learning_rate = 0.01

model = Net()
criterion = nn.MSELoss()

hidden_prev = torch.zeros(batch_size, num_layers, hidden_size) # 初始化记忆单元h0[batch,num_layer,hidden_len]

num_time_steps = 50 # 区间内取多少样本点

for iter in range(6000):
start = np.random.randint(3, size=1)[0] # 在0~3之间随机取开始的时刻点
time_steps = np.linspace(start, start + 10, num_time_steps) # 在[start,start+10]区间均匀地取num_points个点
data = np.sin(time_steps)
data = data.reshape(num_time_step, 1) # [num_time_steps,] -> [num_points,1]

# 输入前49个点(seq_len=49)，即下标0~48 [batch, seq_len, feature_len]
x = torch.tensor(data[:-1]).float().view(1, num_time_steps - 1, 1)
# 预测后49个点，即下标1~49
y = torch.tensor(data[1:]).float().view(1, num_time_steps - 1, 1)
# 以上步骤生成(x,y)数据对

output, hidden_prev = model(x, hidden_prev)   # 喂入模型得到输出
hidden_prev = hidden_prev.detach()            # at

loss = criterion(output, y)                   # 计算MSE损失
loss.backward()
optimizer.step()

if iter % 1000 == 0:
print("Iteration: {} loss {}".format(iter, loss.item()))

# 先用同样的方式生成一组数据x,y

start = np.random.randint(3, size=1)[0]
time_steps = np.linspace(start, start + 10, num_time_steps)
data = np.sin(time_steps)
data = data.reshape(num_time_steps, 1)
x = torch.tensor(data[:-1]).float().view(1, num_time_steps - 1, 1)
y = torch.tensor(data[1:]).float().view(1, num_time_steps - 1, 1)

predictions = []

input = x[:, 0, :] # 取seqlen里面第0号数据
input = input.view(1, 1, 1) # input：[1,1,1]
for
in range(x.shape[1]): # 迭代seq_len次

pred, hidden_prev = model(input, hidden_prev)
input = pred                        # 预测出的(下一个点的)序列pred当成输入(或者直接写成input, hidden_prev = model(input, hidden_prev))
predictions.append(pred.detach().numpy().ravel()[0])

x = x.data.numpy()
y = y.data.numpy()
plt.plot(time_steps[:-1], x.ravel())

plt.scatter(time_steps[:-1], x.ravel(), c='r') # x值
plt.scatter(time_steps[1:], y.ravel(), c='y') # y值
plt.scatter(time_steps[1:], predictions, c='b') # y的预测值
plt.show()

can not be execute
how to solve
lolibinve
@lolibinve
x = torch.tensor(data[:-1]).float().view(1, num_time_steps - 1, 1)
y = torch.tensor(data[1:]).float().view(1, num_time_steps - 1, 1)
help
András Mózes
@mozesa
Hello! I would like to try out pythonnet for QuickOPC, but I see that currently Python 3.9 is not supported. Is there a plan for Python 3.9 support? And if so, when?
elTRexx
@elTRexx

Hi. After exploring IronPython, and being stuck with it + numpy, I explore Pythonnet alternative.
I try a simple example C# app embedding python file *.py execution for start, but I'm surprised there is no wrapper for file processing in pythonnet.

Is there a reason

PyRun_FileExFlags(FILE fp, const char filename, int start, PyObject globals, PyObject locals, int closeit, PyCompilerFlags *flags)

isn't available ?

Ivan Diep
@idiep_gitlab
Does anyone know how to build pythonnet to obtain clr.pyd on Ubuntu? I'm able to build the .whl file, which gives me a clr.cpython-38-x86_64-linux-gnu.so and Python.Runtime.dll, but I'm looking for a clr.pyd. Any help on this would be appreciated.
William Roseberry
@RoseberryPi
I'm running PythonNet from a Blazor project, when I run the project using IIS, pythonnet works as expected. When running the Blazor app using Kestrel PythonNet no longer works. Any ideas why this could be?
zergmk2
@zergmk2

I'm running PythonNet from a Blazor project, when I run the project using IIS, pythonnet works as expected. When running the Blazor app using Kestrel PythonNet no longer works. Any ideas why this could be?

You can check if the environment variable set in IIS and kestrel are different.

I'm trying to run the pythonnet with .net5 under MacOS now, when I run the sample code in github readme file, I found there were many space in the result.
above is the source code
elTRexx
@elTRexx
Hi again.
Well After testing and trying pythonnet a little bit, here I am :
///Kept all my empirical previous testing in comment
class Program
{
static string _scriptsPath = @"C:\.....\ConsoleApp_TestPythonNet\Scripts";

static string[] _pyExts = { ".py" };

static void Main(string[] args)
{
Console.WriteLine("In C#");

//string before = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

//Console.WriteLine("PATH (process): " + before);

PythonSetupEnv.UpdateEnvPaths();

//Console.WriteLine("------------------------------------");

//string after = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

//Console.WriteLine("PATH (process): " + after);

//Environment.SetEnvironmentVariable("PYTHONPATH", _scriptsPath, EnvironmentVariableTarget.Process);

//var bf = before.Split(Path.PathSeparator);

//var af = after.Split(Path.PathSeparator);

//string[] res;

//if (bf.Length > af.Length)
//    res = bf.Except(af).ToArray();
//else
//    res = af.Except(bf).ToArray();

//Console.WriteLine("------------------------------------");
//foreach (var p in res)
//    Console.WriteLine(" - " + p);

using (Py.GIL())
{
//Console.WriteLine("PYTHONPATH (process): " + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process));

//PythonSetupEnv.UpdateEnvPaths();

//PythonEngine.RunSimpleString("import sys;print(sys.path)");

//Console.WriteLine("PYTHONPATH (process): " + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process));

//PythonEngine.RunSimpleString("import sys;print(sys.path)");

//PythonEngine.Exec(FileUtilities.GetStringCodeFromFile(_filepath, _pyExts));

PythonEngine.Initialize();
using (PyScope scope = Py.CreateScope())
{
string code = FileUtilities.GetStringCodeFromFile(_scriptsPath+@"\poc.py", _pyExts);

var scriptCompiled = PythonEngine.Compile(code);

scope.Execute(scriptCompiled);

//dynamic pocPy = Py.Import("poc");

//dynamic pocPy = scope.Import("poc");

//var pocPy = PythonEngine.ImportModule("poc");
//scope.Execute(pocPy);

dynamic func = scope.Get("PrintInput");
dynamic res = func();

dynamic inputText = scope.Get("InputText");

Console.WriteLine("Inside C# code : "+inputText.ToString());
}
}

}
}
}
For completeness the rest of my code (not important here, if is just utility class to read code from .py file, and setup the Environment Variables (PATH, PYTHONHOME and PYTHONPATH) :
class FileUtilities
{
public static string GetStringCodeFromFile(string filepath, string[] ext)
{
/// if we have an existing file with a correct extension
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath) && ext != null && ext.Length > 0 && ext.Contains(Path.GetExtension(filepath)))
return null;
}
}
elTRexx
@elTRexx
public class PythonSetupEnv
{
private static Dictionary<string, List<string>> _pythonEnvPaths;

private static string _pythonPath;

public static Dictionary<string, List<string>> PythonEnvPaths
{
get
{
if (_pythonEnvPaths == null || _pythonEnvPaths.Count < 3)
_pythonEnvPaths = InitEnvPathsDict(_pythonPath);
return _pythonEnvPaths;
}
set => _pythonEnvPaths = value;
}

public static string PythonPath { get => _pythonPath; set => _pythonPath = value; }

private static string KeysToString()
{
string result = "";
foreach (var key in PythonEnvPaths.Keys)
result += (key + " ");
return result;
}

private static List<string> ParseEnvPaths(string rawPaths, char separator)
{
if (rawPaths != null)
return rawPaths.Split(new char[1] { separator }).ToList();
else
return new List<string>();
}

/// <summary>
/// Standard Environment Variable Initialisation, from a given Python directory path, or not
/// </summary>
/// <param name="path">The path to the pythyon directory</param>
/// <returns></returns>
private static Dictionary<string, List<string>> InitEnvPathsDict(string path = null)
{
var alreadyDefinedPATH = ParseEnvPaths(Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine), Path.PathSeparator);

var envDict = new Dictionary<string, List<string>>() {
{ "PYTHONHOME", ParseEnvPaths(Environment.GetEnvironmentVariable("PYTHONHOME", EnvironmentVariableTarget.Machine), Path.PathSeparator) },
{ "PYTHONPATH", ParseEnvPaths(Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Machine), Path.PathSeparator) }
};
if (path != null && Directory.Exists(path))
{
try
{
//PATH
//PYTHONHOME
//PYTHONPATH
}
catch (Exception e) { Console.WriteLine(e.Message); }
}
return envDict;
}

/// <summary>
/// Add a custom module | script to look for
/// </summary>
/// <param name="path">The path to the script, library / directory</param>
{
try
{
}
catch(Exception e) { Console.WriteLine(e.Message); }
}
...... -->
<-- ......
public static void AddEnvPath(string envKey, string envPath, Dictionary<string, List<string>> dict = null)
{
if (dict == null)
dict = @PythonEnvPaths;
if (!Directory.Exists(envPath))
throw new DirectoryNotFoundException("Incorect path to this directory : " + envPath);
if (!dict.ContainsKey(envKey))
throw new KeyNotFoundException(envKey + " is not a correct environment key. Allowed ones are : " + KeysToString());

}

public static void UpdateEnvPaths()
{
string envPath = null;

foreach (var env in PythonEnvPaths)
{
//foreach (var path in env.Value.FindAll(x => Directory.Exists(x)))
foreach (var path in env.Value)
{
envPath = envPath + Path.PathSeparator.ToString() + path;
}

//trim first path separator
if (!string.IsNullOrEmpty(envPath))
envPath = envPath.Substring(1);

Environment.SetEnvironmentVariable(env.Key, envPath, EnvironmentVariableTarget.Process);
envPath = null;
}
}
}
elTRexx
@elTRexx
#poc.py
print("Inside Python poc.py")

InputText = "Python Text"

def PrintInput():
print("inside poc.py PrintInput function : " + InputText)

#PrintInput()

So As you can see I tried

• to loop up my script using PYTHONPATH environment variable setup before (that worked)
• executing that script using dynamic pocPy = Py.Import("poc")
• calling a python func from C# usign pocPy.PrintInput() (that worked)

But now when I want to "play" with my python variables / function from C# usign dynamic pocPy = Py.Import("poc") doesn't help me.
After looking on this I found some code example leading me to my current state :

PythonEngine.Initialize();
using (PyScope scope = Py.CreateScope())
{

string code = FileUtilities.GetStringCodeFromFile(_scriptsPath+@"\poc.py", _pyExts);

var scriptCompiled = PythonEngine.Compile(code);

scope.Execute(scriptCompiled);

dynamic func = scope.Get("PrintInput");
dynamic res = func();

dynamic inputText = scope.Get("InputText");

Console.WriteLine("Inside C# code : "+inputText.ToString());
}
So yeah that work, but I don't get back the more "elegant" way I was using before with environment variables to just "import" a script .
Doesn't it be possible to play with "scope" variable like I did finally, but using Py.Import() (or something similar involving scope I suppose) ?
Amaury Levé
Hi there,
Sorry for the stupid question but I am pretty new to your tool and I can't find any help (looked at SO and issues) about converting a numpy array into a c# collection. This thread seems to be the closestpythonnet/pythonnet#623
LuckyGuySam
@LuckGuySam
Hi, I try to use C# call python with pythonnet. I need to switch the python visual env, but after PythonEngine.Initialize() I can't change my visual env. How can I do? Can I initialize a lot of python visual env and switch when I need ?
Andy Salerno
@andysalerno:matrix.org
[m]
Hi all, is there any possibility of a RC/beta/pre-release of 3.0 on pypi? I've been playing with the "netcore3.1" integration by pulling builds from the build agent, and I'm curious how far off an RC is (apologies if this is asked 100x a day)
maye1115
@maye1115
Hello,there are python37 38 on my computer.I set the system variable to pyhton38 in the code "Environment.SetEnvironmentVariable...EnvironmentVariableTarget.Process". But I uninstall python38, it prompts that DLL python38 cannot be found,I repointed to python37 and it didn’t work.
András Mózes
@mozesa
Hello! When there is a String argument, should I use String("string") or it is just enough to use bare "string"? Thanks in advance.
slide-o-mix
@slide-o-mix:matrix.org
[m]
I use the latter
András Mózes
@mozesa
:thumbsup: I noticed too, that it works, but I was confused.
There should be one-- and preferably only one --obvious way to do it. :smile:
András Mózes
@mozesa
Hello! I use clr within a python worker thread. There is test for the exactly same situation, but I am a bit confused with threading support 😕 I don't own the c# assemblies I use with clr so I don't think it contains any PythonEngine.BeginAllowThreads() calls. Should I deal with GIL, or it's just not concerned to me? I appreciate any help as I have been dealing with it only for a couple of days. Thanks!
András Mózes
@mozesa
Hello! Could someone elaborate on call into managed code by Python-spawned threads.
I would like to avoid the starvation of main Python thread - the managed code is an OPC UA client implementation.
SB6988
@SB6988

Hi everybody, I am trying to use some nugets (Microsoft.ML. ProbabilisticModels specifically) in Python and I am trying to leverage pythonnet. However, I get the following error:

SystemPlatformNotSupportedException: Operation is not supported on this platform

After a list of functions that throw this error. I get:

The above exception was the direct cause of the following exception:

SystemPlatformNotSupportedError: Current platform is not supported by the current compiler choice Auto. Try a different one.

Does anybody now what is the root cause of this platform error? How do I change a compiler choice? Or is there a known workaround for this?

YoonWatch
@JHWatch
Hello! Is Pythonnet not available yet with Python 3.9?
YoonWatch
@JHWatch
Help me I think it might be a version problem when I did Python 3.9 version, so I changed it to 3.8 version and executed the example code, but still TypeInitializationException exception treatment occurs
Runtime.dll has been referred to in "C:\Users\user\AppData\Local\Programs\Python\Python38\Lib\site-packages\pythonnet\runtime" in the Python installation path
chanyam
@chanyam:matrix.org
[m]

Hi! Using Pythonnet with Python 3.7 to load a .NET assembly, which seems to work just fine, but when I try to import an object from a namespace in that assembly, I get this error:

Traceback (most recent call last):
File "ForzaTextureExporter", line 10, in
File "E:\Tools\Scripts\Houdini\python\hdi\TextureExporter.py", line 38, in main

The assembly I'm loading has a bunch of external references to some of our other assemblies, but even copying those dlls into the same dir as python37.exe doesn't seem to get me past this.

the code I'm using is pretty straight forward:
assemblyPath = r"C:\python37" #moved all assemblies here, where python37.exe is

Oh god, sorry about the formatting!
chanyam
@chanyam:matrix.org
[m]

:point_up: Edit: Hi! Using Pythonnet with Python 3.7 to load a .NET assembly, which seems to work just fine, but when I try to import an object from a namespace in that assembly, I get this error:

Traceback (most recent call last):
File "ForzaTextureExporter", line 10, in
File "E:\Tools\Scripts\Houdini\python\hdi\TextureExporter.py", line 38, in main

The assembly I'm loading has a bunch of external references to some of our other assemblies, but even copying those dlls into the same dir as python37.exe doesn't seem to get me past this.
The code I'm using is pretty straight forward:

assemblyPath = r"C:\python37" #moved all assemblies here, where python37.exe is

slide-o-mix
@slide-o-mix:matrix.org
[m]
chanyam
@chanyam:matrix.org
[m]
sorry, yes
chanyam
@chanyam:matrix.org
[m]
OMG - I just realized this looking at your question slide-o-mix
thanks for pointing that out - for some reason my brain wasn't seeing it until just now
slide-o-mix
@slide-o-mix:matrix.org
[m]
It works with the correct namespace?
chanyam
@chanyam:matrix.org
[m]
yeah. i wouldn't call this a high point in my coding career, but hey, its working :D
slide-o-mix
@slide-o-mix:matrix.org
[m]
happens to everyone...
IamMusavaRibica
@IamMusavaRibica

How to write this method in pythonnet?

public override T[] ReadArray<T>(Func<T> func1)  {
var arr = new T[count];
for (var i = 0; i < count; i++)  {
arr[i] = (func1.Invoke());
}
return arr;
}

chanyam
@chanyam:matrix.org
[m]
chanyam
@chanyam:matrix.org
[m]

OK, I'm getting the 'Module not found" errors again (and I've doubled checked my spelling :D)

It worked for a while on Friday and now after the weekend, its stopped. I haven't changed anything in my environment afaik. As a sanity check, I decided to print the assembly's ExportedType property to see what exactly was available from our assembly post-load. My code looks like this:

cblib = clr.AddReference(os.path.join(assemblyPath, "ContentBrowserMetadataObjects"))
if cblib == None:
else:
print(cblib.ImageRuntimeVersion)
for t in cblib.ExportedTypes:
print(t)

When I try calling cblib.ExportedTypes, I get the following exception message:

File "ForzaTextureExporter", line 9, in <module>
File "E:\P4\FMBase_Main\Tools\Scripts\Houdini\python\hdi\Forza_TextureExporter.py", line 26, in main
for t in cblib.ExportedTypes:
System.IO.FileNotFoundException: Could not load file or assembly 'Autofac, Version=4.0.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()

So where is it looking for Autofac? I used Procmon to find all the events related to Houdini (the app calling Python3.7) and right after the event where it loads our assembly (ContentBrowserMetadataObjects.dll) I see the event in the image posted above. It looks like it took the Autofac assembly header and is using it for the name? If i copy Autofac.dll into that dir, it still complains about not finding the dll, but if I copy the name in the event (Autofac, Version=4. etc, etc) and rename the assembly to that, then it works - which is to say that it gets past the Autofac assembly reference but then dies on the next assembly reference, where it does the same thing (uses the header as the assembly name and not finding the dll). Does anyone know how pythonnet resolves assembly references?

chanyam
@chanyam:matrix.org
[m]
I assume this is why copying all of our assemblies into the same folder as the python3.7 exe didn't work - its looking for these modified named assemblies instead of the original assembly names.