简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM