When I try to use
PythonEngine.ImportModule(mymodulename)
some of the optional modules in dependencies are attempted to be loaded
(not required for module use without embedding). This results in return
null from this method because some of these optional dependencies are not
required and hence not available. What is the proper method to use in this
PythonNET API for loading user-written module which depends on multiple
other modules?
I tried importhook.cs from both versions:
https://github.com/pythonnet/pythonnethttps://github.com/renshawbay/pythonnethttp://stackoverflow.com/questions/25101718/how-to-embed-properly-using-pyt…
Hi,
I'm trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet
I'm using the following simple test program:
//=======================================================================
using System;
using System.IO;
using Python.Runtime;
namespace TestPythonNet
{
class Program
{
static void Main(string[] args)
{
string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location);
string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime";
PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime";
PythonEngine.ProgramName = "PythonRuntime";
Environment.SetEnvironmentVariable("PYTHONPATH",
Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" +
Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" +
Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" +
binDir
);
Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1");
PythonEngine.Initialize();
PythonEngine.ImportModule("clr");
using (Py.GIL())
{
PythonEngine.RunSimpleString(
"import clr; " +
"a = clr.AddReference('System'); " +
"print(a.Location);" +
"from System import Environment;" +
"print(Environment.MachineName);");
}
}
}
}
//=======================================================================
"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime" is a folder where I've copied the DLLs and Lib folder from a python 3.2 x86 distribution.
When I run it from Visual Studio it works fine (I guess it may be related to the fact that I'm using python tools for Visual Studio).
But when I run it from the console, it fails with the output:
//=======================================================================
Traceback (most recent call last):
File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize
import sitecustomize
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
Traceback (most recent call last):
File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize
import usercustomize
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Traceback (most recent call last):
File "<string>", line 1, in <module>
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
//=======================================================================
The "print(a.Location);" works but not the "from System import Environment". There are also all these errors about mbcs.
Any idea on what I'm doing wrong?
Thanks,
Serge Weinstock
___________________________________________________________
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited.
Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures.
How do I pass a double array as a numpy list from C# (calling python code
from c#) or passing something that can become a numpy list with minimal
overhead.
Right now, the naive way would be to create a new PyList. And to fill it
with elements from the double array.
But I read on a thread on this list (
https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) that
Marshal.copy was faster.
So, I tried Marshal.copy but I can't figure out how to instantiate a numpy
array in python. Numpy can instantiate a new array from native C array by
calling numpy.ctypeslib.as_array.
So, theoretically if i can do
Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the
addrofnative (which is a IntPtr) I could be done. But I can only pass
PyObject when calling a module function.
Any ideas ?
Hi
I have a problem with python.net.
I have with py2exe build a few programs, that uses Python.NET. I have tried to run the programs build on 3 PCs.
On 2 of PCs the programs run without problems. These PCs have Windows 7 Professional with Service Pack 1 (64bit). The programs are built on of these machines.
On the 3rd PC I get this error message:
Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'clr.pyd' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
File name: 'clr.pyd'
This PC has Windows XP Professional with Service Pack 2. Highest version of installed Microsoft.NET framework is 3.5.
Version of Python.Net is pythonnet-2.0-Beta0-clr4. Python version is 2.7.3.
My questions now are:
* What is the exact reason for the error message on 3rd PC?
* Is the any way to solve this problem.
* If I am using a too new version of Python.NET, is there a version, that would work on all 3 PC?
I hope, that you can help me further?
Kind regards
Hans Peter
I'm trying to use an embedded python interpreter from C# using pythonnet (the python3 compatible version found at https://github.com/renshawbay/pythonnet)
My interpreter is located in D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime and has the "Lib" and "Libs" folder from the python distribution.
I've tested using the following code:
//====================================================================
PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime";
PythonEngine.ProgramName = "PythonRuntime";
PythonEngine.Initialize();
using (Py.GIL())
{
PythonEngine.RunSimpleString("print(1)");
}
//====================================================================
But, it doesn't work. I get a "SystemError: PyEvalCodeEx: NULL globals". Everytime I try to get an object from python, the code fails.
What am I doing wrong?
___________________________________________________________
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited.
Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures.
The dll I'm talking to is unable to accept as an argument a python list of
integers.
I can't find how to create a System.Int32[] collection. Can anyone help?