- Notifications
You must be signed in to change notification settings - Fork749
Description
Environment
- Pythonnet version:Latest master
- Python version: 3.7.8
- Operating System: Windows 10
- .NET Runtime: Core 3.1
Details
- Describe what you were trying to get done.
Add an embedded test that imports pytest.
- What commands did you run to trigger this issue? If you can provide a
Minimal, Complete, and Verifiable example
this will help us understand the issue.
Add the following test to TestTypeManager.cs (so that it runs last):
[Test]publicvoidPathTest(){Py.Import("pytest");}
Then rundotnet test --runtime any-x64 src/embed_tests
- If there was a crash, please include the traceback here.
Failed PathTest [20 ms] Error Message: Python.Runtime.PythonException : ImportError : No module named pytest
The reason for the failure is that an earlier test named TestPythonEngineProperties.SetPythonPath corrupts the Python import mechanism. Removing that test causes PathTest to pass. You can see the causal effect most clearly by addingPy.Import("pytest");
to the end of the SetPythonPath test, where it will fail.
As explained in the documentation forPy_SetPath, setting the Python path has the side effect of changing sys.executable, sys.prefix, and sys.exec_prefix. The import mechanism uses those variables to locate modules. You can confirm that these variables are being altered by making the following change to SetPythonPath:
publicvoidSetPythonPath(){PythonEngine.Initialize();dynamicsys=Py.Import("sys");Console.WriteLine(sys.executable);Console.WriteLine(sys.prefix);Console.WriteLine(sys.exec_prefix);stringpath=PythonEngine.PythonPath;PythonEngine.Shutdown();PythonEngine.PythonPath=path;PythonEngine.Initialize();sys=Py.Import("sys");Console.WriteLine(sys.executable);Console.WriteLine(sys.prefix);Console.WriteLine(sys.exec_prefix);Assert.AreEqual(path,PythonEngine.PythonPath);PythonEngine.Shutdown();}
The SetPythonPath test should restore the original values of those variables.