Setting up pythonnet for .net using NuGet - python.net

Iam trying to develop a simple calculator program embeddeding python in .net,I wanna reference pythonnet from NuGet to include it in my project
I installed pythonnet v2.3.0 using NuGet,I also have python 3 installed in my system
It would be nice if some one give me step by step instruction to embedd python net
form1.cs code :
using System;
...
using Python.Runtime;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PythonEngine.Initialize();
}
private void button1_Click(object sender, EventArgs e)
{
int num1 = int.Parse(a.Text);
int num2 = int.Parse(b.Text);
result.Text = (num1 + num2).ToString();
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
}
}
}
}
When I use using(Py.GIL()) line in my code it compiler shows
System.BadImageFormatException: 'Could not load file or assembly
'Python.Runtime, Version=2.3.0.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies. An attempt was made
to load a program with an incorrect format.'

This issue is likely caused by architecture (32-bit/64-bit) mismatch between Python.Runtime.dll and your Python.
pythonnet 2.3.0 NuGet packages currently published on nuget.org include two versions of the Python.Runtime.dll assembly: 32-bit(x86) and 64-bit(x64). There is a known issue with this package not installing the correct reference in the project, even if the project platform is set to 64-bit:
https://github.com/pythonnet/pythonnet/issues/472.
Usually the x86 reference gets installed, and if your Python is 64-bit, you get the above exception.
To fix this:
Remove the existing assembly reference from the project.
Manually add reference to the correct assembly from the installed nuget package (e.g. your_solution_dir\packages\pythonnet_py35_dotnet.2.3.0\lib\net40\x64\Python.Runtime.dll)

Changed the CPU architecture from AnyCPU to x64 and it fixed the issue.

I had same problem. I was running KerasExampleWinApp Sample (keras.net). The error was:
"...Python.Runtime, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies..."
To solve it, I just deleted the Nugget Reference and reinstalled it.

Related

The type 'Application' is defined in an assembly that is not referenced. gtk-sharp, Version=2.12

I am building an application using GTK# 2.12 version. I installed MonoDevelop for that. Made a sample application.
When I changed the setting from GTK#3 to GTK# 2.12 on the project then all references are updated to GTK#2.12.
When I am compiling the project then compiler throws an error.
/local/mnt/workspace/vkumbhan/work/Program.cs(25,25): Error CS0012: The type 'Application' is defined in an assembly that is not referenced. You must add a reference to assembly 'gio-sharp, Version=3.0.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f'. (CS0012)
class MainClass
{
public static void Main(string[] args)
{
Application.Init(); //error came this line
MainWindow win = new MainWindow();
win.Show();
Application.Run(); //error came this line
}
}
When I looked at the references for the Application class then I found it the Application refer to gtk-sharp 3.0.0.0.
For checking GTK# 2.12 installed or not.
apt-cache policy libgtk2.0-0 libgtk-3-0
both versions are installed on the machine.
Could you please help me, how to resolve the issue?
Find the command line: Please help to update references 2.12.0.0 instead of 3.0.0.0
/usr/lib/mono/msbuild/15.0/bin/Roslyn/csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:prompt /warn:4 /define:DEBUG /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:/usr/lib/mono/gac/atk-sharp/3.0.0.0__35e10195dab3c99f/atk-sharp.dll /reference:/usr/lib/mono/gac/gdk-sharp/3.0.0.0__35e10195dab3c99f/gdk-sharp.dll /reference:/usr/lib/mono/gac/glade-sharp/2.12.0.0__35e10195dab3c99f/glade-sharp.dll /reference:/usr/lib/mono/gac/gtk-sharp/3.0.0.0__35e10195dab3c99f/gtk-sharp.dll /reference:/usr/lib/mono/4.5-api/Mono.Posix.dll /reference:/usr/lib/mono/4.5-api/mscorlib.dll /reference:/usr/lib/mono/gac/pango-sharp/3.0.0.0__35e10195dab3c99f/pango-sharp.dll /reference:/usr/lib/mono/4.5-api/System.Core.dll /reference:/usr/lib/mono/4.5-api/System.dll /debug+ /debug:portable /optimize- /out:obj/Debug/Sample.exe /subsystemversion:6.00 /resource:gtk-gui/gui.stetic,gui.stetic /target:winexe /utf8output MainWindow.cs Program.cs Properties/AssemblyInfo.cs gtk-gui/generated.cs gtk-gui/MainWindow.cs "/tmp/.NETFramework,Version=v4.5.AssemblyAttributes.cs"
Thanks!
The easiest way to solve this is to create a new Gtk# app in MonoDevelop, which defaults to Gtk# 2.12, since the designer needs that version.
Then you can copy the class files (*.cs) from the project that does not compile.

Error while Building (compile) wixproj using MSBuild api 15.9.20

I'm using wix in a project and i have a specific need :
Build wixproj using MSBuild from c#.
For that i'm using MSBuild api as folowing :
static void Main(string[] args)
{
string toolsPath = ToolLocationHelper.GetPathToBuildToolsFile("msbuild.exe", ToolLocationHelper.CurrentToolsVersion);
ProjectCollection projectCollection = new ProjectCollection();
projectCollection.AddToolset(new Toolset(ToolLocationHelper.CurrentToolsVersion, Path.GetDirectoryName(toolsPath), projectCollection, string.Empty));
Project project = projectCollection.LoadProject("TestWix.wixproj");
var result = project.Build(new ConsoleLogger());
Console.ReadKey();
}
And i get this error :
C:\Program Files (x86)\MSBuild\Microsoft\WiX\v3.x\wix2010.targets(1988,5): error MSB4127: The "GenerateCompileWithObjectPath" task could not be instantiated from the assembly "C:\Program Files (x86)\WiX Toolset v3.11\bin\WixTasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Tools.WindowsInstallerXml.Build.Tasks.GenerateCompileWithObjectPath' to type 'Microsoft.Build.Framework.ITask'.
But when i use msbuild command line it's work fine.
Do you have any idea please ?
Thank you in advance.

Can't compile ASP.NET 5 Web API when adding XML support

I'm trying to figure out how ASP.NET 5 Web API supports Content Negotiation. In order to support XML (in addition to the default JSON support), according to another answer here on Stack Overflow, I need to add that support from an optional NuGet package.
Things have changed a bit since that answer, but I pulled down the Microsoft.AspNet.Mvc.Xml package and edited my Startup.cs file:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc().AddMvcOptions(options =>
{
options.InputFormatters.Add(new XmlSerializerInputFormatter());
});
}
This, however, doesn't compile:
The type 'InputFormatter' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null'.
Here are the full compilation error messages:
1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,17,33,44): DNX 4.5.1 error CS0012: The type 'InputFormatter' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null'.
1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,45,33,78): DNX 4.5.1 error CS1503: Argument 1: cannot convert from 'Microsoft.AspNet.Mvc.Xml.XmlSerializerInputFormatter' to 'Microsoft.AspNet.Mvc.Formatters.IInputFormatter'
1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,17,33,44): DNX Core 5.0 error CS0012: The type 'InputFormatter' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null'.
1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,45,33,78): DNX Core 5.0 error CS1503: Argument 1: cannot convert from 'Microsoft.AspNet.Mvc.Xml.XmlSerializerInputFormatter' to 'Microsoft.AspNet.Mvc.Formatters.IInputFormatter'
The error message seems fairly clear, but the project already includes Microsoft.AspNet.Mvc.Core, although not quite in the desired version:
Id Versions
-- --------
Microsoft.AspNet.IISPlatformHandler {1.0.0-rc1-final}
Microsoft.AspNet.Mvc {6.0.0-rc1-final}
Microsoft.AspNet.Mvc.Core {6.0.0-rc1-final}
Microsoft.AspNet.Mvc.Xml {6.0.0-beta5}
Microsoft.AspNet.Server.Kestrel {1.0.0-rc1-final}
Microsoft.AspNet.StaticFiles {1.0.0-rc1-final}
Microsoft.Extensions.Configurati... {1.0.0-rc1-final}
Microsoft.Extensions.Configurati... {1.0.0-rc1-final}
Microsoft.Extensions.Logging {1.0.0-rc1-final}
Microsoft.Extensions.Logging.Con... {1.0.0-rc1-final}
Microsoft.Extensions.Logging.Debug {1.0.0-rc1-final}
As I'm writing this, ASP.NET 5 is only available in a Release Candidate version, so the above packages are the most recent available.
Clearly, Microsoft.AspNet.Mvc.Core 6.0.0.0 isn't available. In a normal .NET project, I'd attempt to solve this issue with a binding redirect, but since this is ASP.NET 5, that's apparently no longer the way to do things.
How can I make my code compile?
I believe you have to use the Microsoft.AspNet.Mvc.Formatters.Xml package instead. Also see this issue on github.
Also a hot tip: Use http://packagesearch.azurewebsites.net/ to search for which NuGet-package contains the class you are looking for.

docx4j.NET installation issue

I've installed docx4j.NET 3.0.1 from Nuget with vs2012.
When I try to use the library with this call
WordprocessingMLPackage.load(inputS, "");
I receive this error:
System.TypeInitializationException:
The type initializer for 'org.docx4j.openpackaging.packages.OpcPackage' threw an exception.
---> System.IO.FileNotFoundException: Could not load file or assembly 'slf4j-api,
Version=1.7.6.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies
The version of slf4j-api in the docx4j.net 3.0.1 distribution is 1.7.5.0.
Is there a problem in the distribution of package?
Check that the list of referenced DLLs in your VS project includes slf4j-api. If not, add it.
If you have the DLL properly referenced, then as per the sample code in say https://github.com/plutext/docx4j.NET/blob/master/docx4j.NET/src/samples/c%23/Docx4NET/DocxToHTML.cs, you might need something like (note the bottom line):
// Programmatically configure Common Logging
// (alternatively, you could do it declaratively in app.config)
NameValueCollection commonLoggingproperties = new NameValueCollection();
commonLoggingproperties["showDateTime"] = "false";
commonLoggingproperties["level"] = "INFO";
LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(commonLoggingproperties);
ILog log = LogManager.GetCurrentClassLogger();
log.Info("Hello from Common Logging" );
// Necessary, if slf4j-api and slf4j-NetCommonLogging are separate DLLs
ikvm.runtime.Startup.addBootClassPathAssembly(
System.Reflection.Assembly.GetAssembly(
typeof(org.slf4j.impl.StaticLoggerBinder)));

NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException

I have the following code set up in my Startup
IDictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", "Data Source=ZEUS;Initial Catalog=mydb;Persist Security Info=True;User ID=sa;Password=xxxxxxxx");
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
source.Add(typeof(ActiveRecordBase), (IDictionary<string, string>) properties);
Assembly asm = Assembly.Load("Repository");
Castle.ActiveRecord.ActiveRecordStarter.Initialize(asm, source);
I am getting the following error:
failed: NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException : Unable to load type 'NNHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class.
Possible causes are:
The NHibernate.Bytecode provider assembly was not deployed.
The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.
I have read and read I am referecning the All the assemblies listed and I am at a total loss as what to try next.
Castle.ActiveRecord.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
NHibernate.ByteCode.Castle.dll
Castle.Core.dll.
I am 100% sure the assembly is in the bin. Anyone have any ideas?
This problem occurs when NHibernate.ByteCode.Castle.dll was built with a different target platform as your project. To test this, change your program target platform from one or more of the following:
x64 to x86
x86 to x64
"Any CPU" to x86
"Any CPU" to x64
If any of those solve your problem, then you know that you just need to synchronize the DLL and your target platform.
I had this problem also, my solution was to add in the Assembly that creates session following code.
private NHibernate.ByteCode.Castle.ProxyFactoryFactory requiredButNeverUsed;