“No module named fcntl” when py script run from c# but works from windows command line

蹲街弑〆低调 提交于 2019-12-24 13:06:36

问题


I am calling a python script that uses imaplib.py and get the "no module named fcntl" error. From searching I found that this module is only available in unix and so I wonder if the py script is confused about what os it is running under. Again, script works fine under windows run directly from the python directory.

    var engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();
    var ops = engine.Operations;
    var script = engine.CreateScriptSourceFromFile("PyTest.py");
    CompiledCode code = script.Compile();

    //string scode = script.GetCode();
    code.Execute(scope);

and the minimal py script to trigger it. Note that commenting out the import imaplib.py line will stop the error.

import sys
sys.path = ["Python\Lib"]
sys.platform = ["win32"]

import os
import os
import getopt
import getpass
import time
import imaplib

I traced it a bit to the subprocess.py that imaplib.py uses, there I noticed the sys.platform variable and tried setting it to win32 as above but made no difference. Something is different between the ironpython calling environment and the windows command prompt from the cpython folder.


回答1:


First of all you're setting sys.platform to a list, it should be a string. .NET/CLI is a different platform than Win32 though, so just setting sys.platform won't help.

Some Googling suggests IronPython doesn't support the subprocess module (or the module doesn't support IronPython). There is a partial replacement here: http://www.ironpython.info/index.php?title=The_subprocess_module

There's also a bugreport with some discussion here: http://bugs.python.org/issue8110




回答2:


The Microsoft distribution does not load the standard library by default.

You have to set the path in your code in order to access it.

The easiest way to do it is to create an environment variable called "IRONPYTHONPATH" which contains the path to the Lib folder of the IronPython installation.

Once you have created it, you can read the location as follows:

from System import Environment
pythonPath = Environment.GetEnvironmentVariable("IRONPYTHONPATH")
import sys
sys.path.append(pythonPath)

More details here. http://www.ironpython.info/index.php/Using_the_Python_Standard_Library



来源:https://stackoverflow.com/questions/4027157/no-module-named-fcntl-when-py-script-run-from-c-sharp-but-works-from-windows-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!