I have written the following code and I am getting no module named fcntl when i am running the python script using C#
print "hi how are you"
import nltk
a="hi how are you"
tokens=nltk.word_tokenize(a)
print tokens
Possibly NLTK depends on subprocess module, which is not supported in embedded IronPython. There're already some answers on the subject:
"No module named fcntl" when py script run from c# but works from windows command line
No module named fcntl
sys.path
Also check your sys.path. Probably it is not set automatically for embedded engine. You can set library paths like this:
engine.SetSearchPaths(
new string[]
{
"C:\\Program Files\\IronPython 2.7",
"C:\\Program Files\\IronPython 2.7\\Lib",
"C:\\Program Files\\IronPython 2.7\\Lib\\site-packages",
}
);
sys.platform
Any module, dependent on fcntl, decides to import it only if sys.platform == 'posix'. Debug it's actual value inside your python code and try to force it to "win32" (before any other imports)
import sys
sys.platform = "win32"
Related
I am attempting to run a simple python script within my .net application using IronPython. I have installed the nuget package into the project (I have NOT installed the ironpython cli on my machine) and have authored this code to handle setting paths, reading output, and setting input.
this.engine = Python.CreateEngine();
this.scope = this.engine.CreateScope();
this.streamOut = new MemoryStream();
this.streamErr = new MemoryStream();
this.engine.Runtime.IO.SetOutput(this.streamOut, Encoding.Default);
this.engine.Runtime.IO.SetErrorOutput(this.streamErr, Encoding.Default);
// this is a locally set environment variable
string pythonRootPath = Environment.GetEnvironmentVariable("PythonPath");
ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add($#"{pythonRootPath}\Lib\site-packages\");
searchPaths.Add($#"{pythonRootPath}\Lib");
engine.SetSearchPaths(searchPaths);
this.engine.Execute(#"import jinja2");
Executing the script results in the following exception
IronPython.Runtime.Exceptions.ImportException: 'No module named errno'
My C# project has references to the following relevant (to IronPython) assemblies:
IronPython (v2.7.9)
IronPython.Modules
IronPython.SQLite
IronPython.Wpf
Microsoft.Dynamic
Microsoft.Scripting
Microsoft.Scripting.Metadata
My paths appear to be well enough set for my purposes as it's not the import of jinja2 itself that causes the error and I am able to import some standard library modules. However, I've seen that the os package fails to import for the same reason as jinja2. I also cannot import errno directly as expected, but I can import it directly if I run it via the python 2.7 cli so the package is installed.
I'm very much out of ideas here if anyone has any suggestions.
I am trying to use numpy module functions in my Python code that i run inside my c# application in VS13.
I'm doing this to import the module:
ScriptEngine pyEngine = IronPython.Hosting.Python.CreateEngine();
ScriptScope pyScope = pyEngine.CreateScope();
pyEngine.Execute("import numpy", pyScope);
however it says "No module named numpy".
I was looking for the solution and found this:
How to install numpy and scipy for Ironpython27? Old method doens't work
Using Nilsters answer I managed to install numpy and im able to use it when i run ipy from cmd. However i dont know how to use it in my c# app in VS.
I've been looking through the files and found
\IronPython 2.7\DLLs\NumpyDotNet.dll
and also:
\IronPython 2.7\Lib\site-packages\numpy\
How do i make it possible for my python code to import numpy?
First of all, the python code is perfectly working in PyCharm and in command prompt. So, Cv2 module is installed well on my Windows machine.
But when I run by IronPython Script engine, it failed as below.
IronPython.Runtime.Exceptions.ImportException: No module named cv2
I setup IronPython engine as below. Note that site-packages has cv2.pyd file.
var engine = Python.CreateEngine();
List<string> pathes = engine.GetSearchPaths().ToList();
pathes.AddRange(new[]
{
#"C:\Python27\Lib\", #"C:\Python27\Lib\site-packages\"
});
engine.SetSearchPaths(pathes);
dynamic py = engine.ExecuteFile("sample.py"); // <- Exception occurred here.
I guess engine.Setup.FileExtensions has only .py file, so that cv2.pyd is not recognized. But, I hardly figure out how to add .pyd to the setup.
Or, is there anything that I missed?
I think you did nothing wrong, but *.pyd files are just not working for IronPython by default. Just checkout IronClad or this so article: https://stackoverflow.com/a/1231131/2630261
I am trying to import scikit-learn in a C# (console) application. I am using Python Tools for Visual Studio and IronPython 2.7.3.
I managed to run an external python script and I also managed to import numpy by declaring the python path: "C:\Python27\Lib\site-packages\"
However, when it comes to scikit-learn I get an error message:
Oops! We couldn't execute the script because of an exception: No module named _c
heck_build
___________________________________________________________________________
Contents of C:\Python27\Lib\site-packages\sklearn\__check_build:
setup.py setup.pyc setup.pyo
_check_build.pyd __init__.py __init__.pyc
__init__.pyo
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
The file "_check_build.pyd" exists in "C:\Python27\Lib\site-packages\sklearn__check_build\".
My code is based on this article: http://devleader.ca/2013/09/23/visual-studio-c-python-sweet/
The file I am using has only the following code:
from sklearn.svm import SVC
print('Hello Python in C#')
Is it possible to add and use scikit in C#? If yes, could you please provide a workaround?
Looks like scikit-learn requires a C extension, which means it won't run under IronPython.
I am trying to run a Python class through C# .NET using IronPython, a couple of the Modules imported by the Python class are:
import collections
import nltk.classify.util
In order to import these when running IronPython, I am using the GetSearchPath collection of the ScriptEngine to add the path to the location of the Python library, as such:
ICollection<string> paths = pyEngine.GetSearchPaths();
string dir = #"C:\Python27\Lib\";
paths.Add(dir);
string dir2 = #"C:\Python27\Lib\site-packages\nltk\classify\";
paths.Add(dir2);
pyEngine.SetSearchPaths(paths);
This seems to run fine for the collections Module, but not the nltk.classify.util, and I get the following error when calling the Execute method of the ScriptEngine:
No module named nltk.classify.util
Even tho the util module lives in the path specified above. I take it the issue has to do with the way the import is specified in the Python class ('.' delimited), just not sure how to solve it. Any ideas where am going wrong?
Python uses the structure of a package name to search for a module, so if you ask for nltk.classify.util it will look for nltk\classify\util.py starting from each directory in the search path.
So in your example, you want to change dir2 as follows:
string dir2 = #"C:\Python27\Lib\site-packages";