René's URL Explorer Experiment


Title: pythonnet.load crashes if the Python interpreter is called from PATH in Ubuntu 20.04 · Issue #1388 · pythonnet/pythonnet · GitHub

Open Graph Title: pythonnet.load crashes if the Python interpreter is called from PATH in Ubuntu 20.04 · Issue #1388 · pythonnet/pythonnet

X Title: pythonnet.load crashes if the Python interpreter is called from PATH in Ubuntu 20.04 · Issue #1388 · pythonnet/pythonnet

Description: Environment Pythonnet version: 3.0.0-dev (latest master on 18/2/2021) Python version: 3.8.5 Operating System: Ubuntu 20.04 .NET Runtime: .NET 5.0.2 Details Describe what you were trying to get done. I am trying to use the latest pythonne...

Open Graph Description: Environment Pythonnet version: 3.0.0-dev (latest master on 18/2/2021) Python version: 3.8.5 Operating System: Ubuntu 20.04 .NET Runtime: .NET 5.0.2 Details Describe what you were trying to get done...

X Description: Environment Pythonnet version: 3.0.0-dev (latest master on 18/2/2021) Python version: 3.8.5 Operating System: Ubuntu 20.04 .NET Runtime: .NET 5.0.2 Details Describe what you were trying to get done...

Opengraph URL: https://github.com/pythonnet/pythonnet/issues/1388

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"pythonnet.load crashes if the Python interpreter is called from PATH in Ubuntu 20.04","articleBody":"### Environment\r\n\r\n-   Pythonnet version: 3.0.0-dev (latest master on 18/2/2021)\r\n-   Python version: 3.8.5\r\n-   Operating System: Ubuntu 20.04\r\n-   .NET Runtime: .NET 5.0.2\r\n\r\n### Details\r\n\r\n-   Describe what you were trying to get done.\r\nI am trying to use the latest `pythonnet` in a virtual environment. If I call my simple demo script using `python` command in PATH, pythonnet.load() crashes. It is the same with any other PATH-based `pythonX` command. Using a full path works fine. See the details and the analysis below.\r\n\r\nPreparation\r\n```\r\n# Create virtual env.\r\npython3 -m virtualenv venv\r\n. venv/bin/activate\r\n# Install the latest pythonnet\r\ngit clone https://github.com/pythonnet/pythonnet.git\r\npip install ./pythonnet/\r\n# Get .NET 5 runtime\r\nwget https://download.visualstudio.microsoft.com/download/pr/824175f2-5577-46ec-a675-95d2f652575f/07dafc2398e669afa7d25f2d47398c36/dotnet-runtime-5.0.2-linux-x64.tar.gz\r\nmkdir dotnet_5.0.2\r\ntar xfzv dotnet-runtime-5.0.2-linux-x64.tar.gz -C dotnet_5.0.2/\r\n```\r\n\r\nRuntime config (`cat runtimeconfig.json`)\r\n``` \r\n{\r\n  \"runtimeOptions\": {\r\n    \"tfm\": \"net5.0\",\r\n    \"framework\": {\r\n      \"name\": \"Microsoft.NETCore.App\",\r\n      \"version\": \"5.0.2\"\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nPython script (`cat bug.py`):\r\n```python\r\nimport os\r\n\r\nimport clr_loader\r\nimport pythonnet\r\n\r\nruntime_config = os.path.join(os.path.dirname(__file__), \"runtimeconfig.json\")\r\ndotnet_root = os.path.join(os.path.dirname(__file__), \"dotnet_5.0.2\")\r\n\r\nruntime = clr_loader.get_coreclr(runtime_config=runtime_config, dotnet_root=dotnet_root)\r\n\r\npythonnet.set_runtime(runtime)\r\npythonnet.load()\r\n\r\nimport System\r\nfrom System import String\r\n\r\ns = String(\"Hello World from Python.NET for .NET 5!\")\r\nSystem.Console.WriteLine(s)\r\n```\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\n```\r\n\u003e which python\r\n/home/takacs/src/pythonnet/venv/bin/python\r\n\r\n\u003e`which python` bug.py \r\nHello World from Python.NET for .NET 5!\r\n \r\n\u003e python bug.py \r\nTraceback (most recent call last):\r\n  File \"bug.py\", line 12, in \u003cmodule\u003e\r\n    pythonnet.load()\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/pythonnet/__init__.py\", line 50, in load\r\n    _FFI.dlopen(libpython, posix.RTLD_NODELETE | posix.RTLD_LOCAL)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 150, in dlopen\r\n    lib, function_cache = _make_ffi_library(self, name, flags)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 832, in _make_ffi_library\r\n    backendlib = _load_backend_lib(backend, libname, flags)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 827, in _load_backend_lib\r\n    raise OSError(msg)\r\nOSError: cannot load library '/home/takacs/src/pythonnet/python': /home/takacs/src/pythonnet/python: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/home/takacs/src/pythonnet/python'\r\n```\r\n\r\n-   If there was a crash, please include the traceback here.\r\n```\r\nTraceback (most recent call last):\r\n  File \"bug.py\", line 12, in \u003cmodule\u003e\r\n    pythonnet.load()\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/pythonnet/__init__.py\", line 50, in load\r\n    _FFI.dlopen(libpython, posix.RTLD_NODELETE | posix.RTLD_LOCAL)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 150, in dlopen\r\n    lib, function_cache = _make_ffi_library(self, name, flags)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 832, in _make_ffi_library\r\n    backendlib = _load_backend_lib(backend, libname, flags)\r\n  File \"/home/takacs/src/pythonnet/venv/lib/python3.8/site-packages/cffi/api.py\", line 827, in _load_backend_lib\r\n    raise OSError(msg)\r\nOSError: cannot load library '/home/takacs/src/pythonnet/python': /home/takacs/src/pythonnet/python: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called \r\n```\r\n\r\n### Analysis\r\n\r\nThis issue is in the `pythonnet.find_libpython` module. The `dlinfo.dli_fname` string contains the actual command that was used to start the Python interpreter. In the case of pure `python` command, `os.path.realpath` fails the properly translate this back to the real executable.\r\n\r\nhttps://github.com/pythonnet/pythonnet/blob/0f5e7814c68b846cd89019f2e23fed69eaa59eca/pythonnet/find_libpython/__init__.py#L90)\r\n\r\nAdding a few debug lines properly demonstrate this:\r\n```Python\r\ndef _linked_libpython_unix():\r\n    libdl = ctypes.CDLL(ctypes.util.find_library(\"dl\"))\r\n    libdl.dladdr.argtypes = [ctypes.c_void_p, ctypes.POINTER(Dl_info)]\r\n    libdl.dladdr.restype = ctypes.c_int\r\n\r\n    dlinfo = Dl_info()\r\n    retcode = libdl.dladdr(\r\n        ctypes.cast(ctypes.pythonapi.Py_GetVersion, ctypes.c_void_p),\r\n        ctypes.pointer(dlinfo))\r\n    if retcode == 0:  # means error\r\n        return None\r\n\r\n    print(f\"DEBUG dlinfo.dli_fname.decode(): {dlinfo.dli_fname.decode()}\")\r\n    path = os.path.realpath(dlinfo.dli_fname.decode())\r\n    print(f\"DEBUG path: {path}\")\r\n    \r\n    if path == os.path.realpath(sys.executable):\r\n        return None\r\n    return path\r\n```\r\n```\r\n\u003e  ./venv/bin/python bug.py \r\nDEBUG dlinfo.dli_fname.decode(): ./venv/bin/python\r\nDEBUG path: /usr/bin/python3.8\r\nHello World from Python.NET for .NET 5!\r\n\r\n\u003e ../pythonnet/venv/bin/python bug.py \r\nDEBUG dlinfo.dli_fname.decode(): ../pythonnet/venv/bin/python\r\nDEBUG path: /usr/bin/python3.8\r\nHello World from Python.NET for .NET 5!\r\n\r\n\u003epython bug.py \r\nDEBUG dlinfo.dli_fname.decode(): python\r\nDEBUG path: /home/takacs/src/pythonnet/python\r\nTraceback (most recent call last):\r\n...\r\n```\r\nThe last case demonstrates that `os.path.realpath` just extends `python` with the current path which is obviously incorrect.\r\n\r\nAs a result, `pythonnet.load()` method's `libpython` variable will point to a library which cannot be loaded (https://github.com/pythonnet/pythonnet/blob/0f5e7814c68b846cd89019f2e23fed69eaa59eca/pythonnet/__init__.py#L50).\r\n However, if this part is disabled, the application will crash later in the .NET `Python.Runtime` for the same reason.\r\n","author":{"url":"https://github.com/tibortakacs","@type":"Person","name":"tibortakacs"},"datePublished":"2021-02-18T19:44:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/1388/pythonnet/issues/1388"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:536cf2f0-f9d5-fb51-bcd3-7ef5610508aa
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAE66:EC9DB:426AE2E:5A46689:69720859
html-safe-noncef11d51fbeff8be6127e444c3468ac17fd4fdd5e8656ebd0824ab8f1ff9622e83
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTY2OkVDOURCOjQyNkFFMkU6NUE0NjY4OTo2OTcyMDg1OSIsInZpc2l0b3JfaWQiOiIyNjkxNzEzNDY0MjQ0NzY2ODA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac16b06017049b14355e514914ca53131cb06282e49adee8905d132f91c5c46b46
hovercard-subject-tagissue:811404249
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/1388/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8ee32df851127a73f4f9ca630f76b74ec30b5c37a42cc510d0f494baf563e964/pythonnet/pythonnet/issues/1388
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8ee32df851127a73f4f9ca630f76b74ec30b5c37a42cc510d0f494baf563e964/pythonnet/pythonnet/issues/1388
og:image:altEnvironment Pythonnet version: 3.0.0-dev (latest master on 18/2/2021) Python version: 3.8.5 Operating System: Ubuntu 20.04 .NET Runtime: .NET 5.0.2 Details Describe what you were trying to get done...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametibortakacs
hostnamegithub.com
expected-hostnamegithub.com
None5d01a3551afdd1a0e12e78acaf3ba43a9a1f7dcb518695d6ed59db76a9290f13
turbo-cache-controlno-preview
go-importgithub.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git
octolytics-dimension-user_id6050430
octolytics-dimension-user_loginpythonnet
octolytics-dimension-repository_id14748123
octolytics-dimension-repository_nwopythonnet/pythonnet
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id14748123
octolytics-dimension-repository_network_root_nwopythonnet/pythonnet
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release062b05dcdf64ee7a3dad7acabb746494ae0982cc
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/pythonnet/pythonnet/issues/1388#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F1388
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F1388
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=pythonnet%2Fpythonnet
Reloadhttps://github.com/pythonnet/pythonnet/issues/1388
Reloadhttps://github.com/pythonnet/pythonnet/issues/1388
Reloadhttps://github.com/pythonnet/pythonnet/issues/1388
pythonnet https://github.com/pythonnet
pythonnethttps://github.com/pythonnet/pythonnet
Notifications https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Fork 770 https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Star 5.4k https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Code https://github.com/pythonnet/pythonnet
Issues 155 https://github.com/pythonnet/pythonnet/issues
Pull requests 18 https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects 0 https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/pythonnet/pythonnet/security
Please reload this pagehttps://github.com/pythonnet/pythonnet/issues/1388
Insights https://github.com/pythonnet/pythonnet/pulse
Code https://github.com/pythonnet/pythonnet
Issues https://github.com/pythonnet/pythonnet/issues
Pull requests https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security https://github.com/pythonnet/pythonnet/security
Insights https://github.com/pythonnet/pythonnet/pulse
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/1388
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/1388
pythonnet.load crashes if the Python interpreter is called from PATH in Ubuntu 20.04https://github.com/pythonnet/pythonnet/issues/1388#top
https://github.com/tibortakacs
https://github.com/tibortakacs
tibortakacshttps://github.com/tibortakacs
on Feb 18, 2021https://github.com/pythonnet/pythonnet/issues/1388#issue-811404249
Minimal, Complete, and Verifiable examplehttp://stackoverflow.com/help/mcve
pythonnet/pythonnet/find_libpython/__init__.pyhttps://github.com/pythonnet/pythonnet/blob/0f5e7814c68b846cd89019f2e23fed69eaa59eca/pythonnet/find_libpython/__init__.py#L90
0f5e781https://github.com/pythonnet/pythonnet/commit/0f5e7814c68b846cd89019f2e23fed69eaa59eca
pythonnet/pythonnet/__init__.pyhttps://github.com/pythonnet/pythonnet/blob/0f5e7814c68b846cd89019f2e23fed69eaa59eca/pythonnet/__init__.py#L50
0f5e781https://github.com/pythonnet/pythonnet/commit/0f5e7814c68b846cd89019f2e23fed69eaa59eca
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.