开发者

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

开发者 https://www.devze.com 2023-01-21 07:06 出处:网络
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 conf

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 cal开发者_运维技巧ling 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号