Title: TestPythonEngineProperties.SetPythonPath spoils later tests that load modules · Issue #1335 · pythonnet/pythonnet · GitHub
Open Graph Title: TestPythonEngineProperties.SetPythonPath spoils later tests that load modules · Issue #1335 · pythonnet/pythonnet
X Title: TestPythonEngineProperties.SetPythonPath spoils later tests that load modules · Issue #1335 · pythonnet/pythonnet
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...
Open Graph 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 i...
X 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 i...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/1335
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TestPythonEngineProperties.SetPythonPath spoils later tests that load modules","articleBody":"### Environment\r\n\r\n- Pythonnet version: [Latest master](https://github.com/pythonnet/pythonnet/commit/d9e15a75a1f2583d2986355ca685140257145c80)\r\n- Python version: 3.7.8\r\n- Operating System: Windows 10\r\n- .NET Runtime: Core 3.1\r\n\r\n### Details\r\n\r\n- Describe what you were trying to get done.\r\n\r\nAdd an embedded test that imports pytest.\r\n\r\n- What commands did you run to trigger this issue? If you can provide a\r\n [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)\r\n this will help us understand the issue.\r\n\r\nAdd the following test to TestTypeManager.cs (so that it runs last):\r\n```csharp\r\n [Test]\r\n public void PathTest()\r\n {\r\n Py.Import(\"pytest\");\r\n }\r\n```\r\nThen run `dotnet test --runtime any-x64 src/embed_tests`\r\n\r\n- If there was a crash, please include the traceback here.\r\n\r\n```\r\n Failed PathTest [20 ms]\r\n Error Message:\r\n Python.Runtime.PythonException : ImportError : No module named pytest\r\n```\r\n\r\nThe 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 adding `Py.Import(\"pytest\");` to the end of the SetPythonPath test, where it will fail.\r\n\r\nAs explained in the documentation for [Py_SetPath](https://docs.python.org/3/c-api/init.html#c.Py_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:\r\n\r\n```csharp\r\n public void SetPythonPath()\r\n {\r\n PythonEngine.Initialize();\r\n\r\n dynamic sys = Py.Import(\"sys\");\r\n Console.WriteLine(sys.executable);\r\n Console.WriteLine(sys.prefix);\r\n Console.WriteLine(sys.exec_prefix);\r\n\r\n string path = PythonEngine.PythonPath;\r\n PythonEngine.Shutdown();\r\n\r\n PythonEngine.PythonPath = path;\r\n PythonEngine.Initialize();\r\n\r\n sys = Py.Import(\"sys\");\r\n Console.WriteLine(sys.executable);\r\n Console.WriteLine(sys.prefix);\r\n Console.WriteLine(sys.exec_prefix);\r\n\r\n Assert.AreEqual(path, PythonEngine.PythonPath);\r\n PythonEngine.Shutdown();\r\n }\r\n```\r\nThe SetPythonPath test should restore the original values of those variables.","author":{"url":"https://github.com/tminka","@type":"Person","name":"tminka"},"datePublished":"2020-12-21T18:30:44.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1335/pythonnet/issues/1335"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c219cad7-6753-bff6-91b6-efd3abee18cf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A7C4:67735:1F381E8:2A1F249:69702DB1 |
| html-safe-nonce | fd1df723042279f16923ba3681614699d4ca5a1b47783c5acb61237adc45964e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBN0M0OjY3NzM1OjFGMzgxRTg6MkExRjI0OTo2OTcwMkRCMSIsInZpc2l0b3JfaWQiOiI4NTYzMzkyMTI1Mzc2NDA5MDA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ba66daa6f041ae8eab4c351d05ec300b465f0a6c542c04917d2a549fb9d8b6f3 |
| hovercard-subject-tag | issue:772377988 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/1335/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a79420840a810cdff1e27dcb17d09d24c1443606083c3fc9427321869737b2fe/pythonnet/pythonnet/issues/1335 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a79420840a810cdff1e27dcb17d09d24c1443606083c3fc9427321869737b2fe/pythonnet/pythonnet/issues/1335 |
| og:image:alt | 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 i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tminka |
| hostname | github.com |
| expected-hostname | github.com |
| None | 01fa379f5de85ef8e791d09724e69709ce9eb9595278316e0a921312dc88e0bc |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | dda91974c069382b0dfa47b2da7e28bd061c8331 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width