[petsc-dev] Problem pip installing petsc4py on existing PETSc installation

Satish Balay balay.anl at fastmail.org
Thu Aug 28 10:07:47 CDT 2025


Please try using 3.0.12 [i.e cython version >=3. and < 3.1]

Likely:

pip install -v cython=3.0.12 petsc4py==3.21.1

I think 3.1 added in some incompatible changes - and it was released after petsc-3.21 - so we can't really modify conf/confpetsc.py for this old release to add in this later change (subsequent petsc releases got a fix).

Ok - I just tried a build via spack - and can reproduce this error with cython-3.1.3 and petsc4py-3.21.1. And using cython-3.0.12 worked.

Satish

On Thu, 28 Aug 2025, Samar Khatiwala via petsc-dev wrote:

> Hi Satish,
> 
> Thanks for the reply.
> 
> I’ve done a bunch more tests and I think the issue is as you suggest the cython version (<3 and >=3 are very different).
> 
> My python installation has cython 0.29.37. However, when doing a pip install, pip insists on downloading cython>=3. The reason is that pyproject.toml and conf/confpetsc.py have this cython version as a requirement.
> 
> On the other hand, I can install petsc4py along with PETSc using --with-petsc4py=1 at configure time. If I understand things correctly, when petsc4py is installed along with PETSc, it builds the extension module using python (and not pip) so the requirements are likely ignored. In fact, if I hack pyproject.toml and confpetsc.py and set the source to the pre-packaged petsc4py in $PETSC_DIR/src/binding/petsc4py I get much further along with pip (not all the way though, but for other reasons).
> 
> From all this I’m fairly certain now that 3.21.1 is not compatible with cython>=3. Is it possible that the cython (and numpy) requirements on PyPI for older versions of PETSc/petsc4py are not correct?
> 
> Thanks,
> 
> Samar
> 
> On Aug 28, 2025, at 11:07 PM, Satish Balay <balay.anl at fastmail.org> wrote:
> 
> On Thu, 28 Aug 2025, Samar Khatiwala via petsc-dev wrote:
> 
> Hello,
> 
> I’m trying to install petsc4py with pip on a machine with an existing PETSc (3.21.1) (installed without pets4py). When I do:
> 
> pip install -v petsc4py==3.21.1
> 
> the installation fails. (If I don’t specify the version pip just downloads the latest version and the installation fails in a different way, but that’s not surprising.)
> 
> I’ve attached the build log.
> 
> If I don’t specify PETSC_DIR etc and just do pip install petsc4py then that works. But I would like to use the existing installed PETSc (yes, I know it is quite old but please bear with me!).
> 
> 
>  Successfully installed cython-3.1.3 numpy-2.0.2 setuptools-80.9.0
> <snip>
>  using Cython 3.1.3
>  cythonizing 'petsc4py/PETSc.pyx' -> 'petsc4py/PETSc.c'
>  warning: petsc4py/PETSc/petscis.pxi:196:4: '__getsegcount__' relates to the old Python 2 buffer protocol and is no longer used.
>  warning: petsc4py/PETSc/petscis.pxi:201:4: '__getreadbuffer__' relates to the old Python 2 buffer protocol and is no longer used.
>  warning: petsc4py/PETSc/petscvec.pxi:585:4: '__getsegcount__' relates to the old Python 2 buffer protocol and is no longer used.
>  warning: petsc4py/PETSc/petscvec.pxi:590:4: '__getreadbuffer__' relates to the old Python 2 buffer protocol and is no longer used.
>  warning: petsc4py/PETSc/petscvec.pxi:595:4: '__getwritebuffer__' relates to the old Python 2 buffer protocol and is no longer used.
> 
>  Error compiling Cython file:
>  ------------------------------------------------------------
>  ...
> 
>      @classmethod
>      def Print(
>          cls,
>          *args: Any,
>          sep: str = ' ',
>                     ^
>  ------------------------------------------------------------
> 
>  petsc4py/PETSc/Sys.pyx:159:19: Compiler crash in ExpressionWriter
> 
> 
>  Compiler crash traceback from this point on:
>    File "Cython/Compiler/Visitor.py", line 174, in Cython.Compiler.Visitor.TreeVisitor._visit
>    File "/private/var/folders/d3/w861tc0j755f19272xfkq56w0000gn/T/pip-install-1e_zq9u1/petsc4py_c977438422fb480b9f145e0099e7cf3e/conf/cyautodoc.py", line 28, in visit_UnicodeNode
>      self.emit_string(node, '')
>  AttributeError: 'ExpressionWriter' object has no attribute 'emit_string'
>  error: Cython failure: 'petsc4py/PETSc.pyx' -> 'petsc4py/PETSc.c'
> <<<
> 
> Perhaps you can try installing/using an older cython version - say 3.0.12 - and see if that works.
> 
> Satish
> 
> 


More information about the petsc-dev mailing list