Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TestPythonEngineProperties.SetPythonPath spoils later tests that load modules #1335

Closed
@tminka

Description

@tminka

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.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp