pythonnet / pythonnet/pythonnet

Trouble with pythonnet on docker/linux

Open
#1,346 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
5.5k
Forks
780
PR merge metrics
No merged PRs in 30d

Description

Environment
  • Pythonnet version: 3.0.0-dev1 & 2.5.1
  • Python version: 3.8
  • Operating System: Ubuntu 20.04
  • .NET Runtime: 5
Details
  • Describe what you were trying to get done.

I am trying to run pythonnet via docker. The only requirements is executing a set of pytest tests from C# as well as possibly other python functions directly from C# code.

  • What commands did you run to trigger this issue? If you can provide a

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal-amd64 AS build

ENV DEBIAN_FRONTEND noninteractive

RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        python3 python3-pip python3-dev python3-venv

RUN git clone https://github.com/pythonnet/pythonnet
WORKDIR /pythonnet
RUN python3 setup.py build_dotnet --inplace

RUN python3 -m pip install pytest

WORKDIR /source
COPY ./PyTestSelenium/ /pythonCode/

COPY *.csproj .
RUN dotnet restore

COPY . .
RUN dotnet run 

C# code

PythonEngine.Initialize();
         
var pyLock = PythonEngine.AcquireLock();
PythonEngine.ImportModule("pytest");
PythonEngine.ReleaseLock(pyLock);

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>
  <PropertyGroup>
    <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
  </PropertyGroup>
  <ItemGroup>
    <!--<PackageReference Include="pythonnet_netstandard_py38_linux" Version="2.5.1" />-->

    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
    <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
    <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="87.0.4280.8800" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Python.Runtime">
      <HintPath>/pythonnet/pythonnet/runtime/Python.Runtime.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>
  • If there was a crash, please include the traceback here.
#16 [12/12] RUN dotnet run
#16 sha256:a55eb81183e53ed9529fc7015660b655097ddfaed37913105f34459c0d54941e
#16 4.371 Unhandled exception. Python.Runtime.PythonException: ImportError : /usr/lib/python3.8/lib-dynload/_contextvars.cpython-38-x86_64-linux-gnu.so: undefined symbol: PyContextVar_Type
#16 4.398 ['  File "/usr/local/lib/python3.8/dist-packages/pytest/__init__.py", line 42, in <module>\n    from _pytest.python_api import approx\n', '  File "/usr/local/lib/python3.8/dist-packages/_pytest/python_api.py", line 6, in <module>\n    from decimal import Decimal\n', '  File "/usr/lib/python3.8/decimal.py", line 8, in <module>\n    from _pydecimal import *\n', '  File "/usr/lib/python3.8/_pydecimal.py", line 440, in <module>\n    import contextvars\n', '  File "/usr/lib/python3.8/contextvars.py", line 1, in <module>\n    from _contextvars import Context, ContextVar, Token, copy_context\n']   at Python.Runtime.PythonException.ThrowIfIsNull(IntPtr ob) in /pythonnet/src/runtime/pythonexception.cs:line 261
#16 4.398    at Python.Runtime.PythonEngine.ImportModule(String name) in /pythonnet/src/runtime/pythonengine.cs:line 493
#16 4.398    at PythonNetIntegration.Program.Main(String[] args) in /source/PythonXunitTests.cs:line 119
#16 ERROR: executor failed running [/bin/sh -c dotnet run]: exit code: 134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the provided Dockerfile and C# entry point, then start at PythonEngine.ImportModule in pythonengine.cs:493 and the call site at PythonXunitTests.cs:119. Use the traceback to investigate the Python 3.8 _contextvars import; done means pytest imports successfully without the undefined PyContextVar_Type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, docker, python
Domain
backend, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.