2011-01-17 3 views
7

.NET 4.0에 파이썬 2.6을 포함하려고합니다. 다음과 같이 ".NET을위한 파이썬"의 아주 최소한의 문서에 따라Python for .NET : ImportError : 경고라는 모듈이 없습니다.

, 나는 매우 간단한 코드를 작성 :

test.py 내가 없음 모듈 이름이 없습니다 "라는 가져 오기 오류가 비어있는 경우에도
const string pythonModulePath = @"C:\Projects\PythonImport\PythonImport\test.py"; 
Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(python 
ModulePath)); 

PythonEngine.Initialize(); 

var oldWorkingDirectory = Directory.GetCurrentDirectory(); 
var currWorkingDirectory = Path.GetDirectoryName(pythonModulePath); 

Directory.SetCurrentDirectory(currWorkingDirectory); 

var pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(python 
ModulePath)); 
if (pyPlugin == null) 
{ 
     throw new PythonException(); 
} 

Directory.SetCurrentDirectory(oldWorkingDirectory); 

경고 ".

'import site' failed; use -v for traceback 

Unhandled Exception: Python.Runtime.PythonException: ImportError : No module nam 
ed warnings 

"python -v"(자세한 정보 표시 모드)를 사용하여 test.py를 실행하면 그 이유가 분명해집니다. python은 #cleanup [2] 경고를 호출합니다.

왜 파이썬 .NET은 경고를 해결할 수 없습니까? 모듈이 Lib 디렉토리에 있습니다.

아이디어가 있으십니까?

답변

2

기본적으로 파이썬 엔진은 자동으로 필요한 경로를 설정하지 않는다고 생각합니다.

http://www.voidspace.org.uk/ironpython/custom_executable.shtml에는 ironpython이 포함되어 있습니다. 내가 어디 있는지와 IronPython의가 설치되어있는 경우, 내가, 내가 필요로하는 표준 모듈을 모두 찾아내는 IPyStdLibDLL로 컴파일하고 내 코드

에서 다음 번째 수행하는 것을 선호하지 않는 한 당신이

PythonEngine engine = new PythonEngine(); 
engine.AddToPath(Path.GetDirectoryName(Application.ExecutablePath)); 
engine.AddToPath(MyPathToStdLib); 

같은 누락 것 같은데

1

@ SilentGhost 당신이 알아 낸 것인지 모르겠지만 PYTHONHOME 변수 대신 PYTHONPATH 환경 변수를 설정해야합니다. 나도 문제가 있었고, 환경 변수를 ol 'Environment.SetVariable ("PYTHONPATH", ...)로 설정했다. 결국 모든 것이 잘되었다.

Python.NET에서 행운을 빈다.

관련 문제