Skip to content
build-cython-ext logo

Building Cython Extension Packages

build-cython-ext

Guidance for building and installing Cython extension packages, particularly when resolving compatibility issues with modern Python and NumPy versions. This skill applies when installing legacy Cython packages, fixing NumPy 2.0 deprecation errors, resolving Python 3.x compatibility issues in exte...

SKILL.md

Full skill instructions

Building Cython Extension Packages

This skill provides systematic approaches for building Cython extension packages, with emphasis on resolving compatibility issues that arise with modern Python and NumPy versions.

When to Use This Skill

  • Installing legacy Cython packages that have not been updated for NumPy 2.0 or Python 3.x
  • Encountering compilation errors related to deprecated NumPy types
  • Building packages with .pyx Cython source files
  • Installing extension packages to system or global Python environments
  • Troubleshooting setup.py build failures for Cython projects

Recommended Approach

Phase 1: Pre-Build Analysis (Before Attempting to Build)

Conduct a comprehensive inventory of potential compatibility issues BEFORE attempting to build. This proactive approach prevents the inefficient cycle of build-fail-fix-rebuild.

1. Identify All Deprecated NumPy Types

Search for ALL known NumPy 2.0 deprecated type aliases simultaneously:

# Search in both Python (.py) and Cython (.pyx) files
grep -rn "np\.float[^0-9]" --include="*.py" --include="*.pyx" .
grep -rn "np\.int[^0-9]" --include="*.py" --include="*.pyx" .
grep -rn "np\.complex[^0-9]" --include="*.py" --include="*.pyx" .
grep -rn "np\.bool[^0-9]" --include="*.py" --include="*.pyx" .
grep -rn "np\.object[^0-9]" --include="*.py" --include="*.pyx" .
grep -rn "np\.str[^0-9]" --include="*.py" --include="*.pyx" .

2. Check for Python 3.x Compatibility Issues

# Common Python 2/​3 compatibility issues
grep -rn "from fractions import gcd" --include="*.py" .
grep -rn "print " --include="*.py" .  # Python 2 print statements
grep -rn "xrange" --include="*.py" .
grep -rn "\.iteritems\|\.itervalues\|\.iterkeys" --include="*.py" .

3. Examine Cython Files Specifically

Cython .pyx files may contain C-level type declarations that need updating:

# Look for cdef declarations with deprecated types
grep -rn "cdef.*np\." --include="*.pyx" .

Phase 2: Systematic Fixes

Apply fixes comprehensively before building, not reactively after each error.

NumPy Type Replacements:

DeprecatedReplacement
np.floatnp.float64 or float
np.intnp.int64 or int
np.complexnp.complex128 or complex
np.boolnp.bool_ or bool
np.objectnp.object_ or object
np.strnp.str_ or str

Type Checking Considerations:

When replacing isinstance() checks, consider all relevant subtypes:

# Instead of: isinstance(x, np.complex)
# Use: isinstance(x, (np.complexfloating, complex))
# This covers complex64, complex128, and Python's built-in complex

Python 3.x Fixes:

# gcd import
# Old: from fractions import gcd
# New: from math import gcd

Phase 3: Build and Install

Installation Types:

Understand the difference between installation methods:

  • pip install . - Proper installation, copies package to site-packages
  • pip install -e . - Editable/​development install, links to source directory

When the task specifies "install to system/​global Python environment," use pip install . (without -e).

Clean Build Process:

# Clean any previous build artifacts
python setup.py clean --all
rm -rf build/ dist/ *.egg-info/

# Build and install
pip install .

Phase 4: Verification

1. Verify Installation Location

pip show <package-name>
python -c "import <package>; print(<package>.__file__)"

2. Test All Extension Modules

Run tests that specifically exercise Cython-compiled code paths:

# Run the test suite
python -m pytest tests/

# If specific test files exist for extensions, run those
python -m pytest tests/​test_*.py -v

3. Import Verification

# Verify each compiled extension can be imported
import <package>.<cython_module>

4. Clean Rebuild Verification

After all fixes, perform a clean rebuild to ensure no stale artifacts:

pip uninstall <package> -y
rm -rf build/
pip install .

Common Pitfalls

  1. Reactive vs. Proactive Fixing: Do not fix errors one-by-one as they appear. Search comprehensively first.

  2. Ignoring Cython Files: The .pyx source files often contain the same deprecated types as .py files. Always check both.

  3. Incomplete Type Coverage: When fixing isinstance() checks for complex numbers, account for all NumPy complex types (complex64, complex128) not just complex128.

  4. Editable Install When Global Is Required: Using pip install -e . when the task requires a proper global installation leaves the package tied to the source directory.

  5. Stale Build Artifacts: Failing to clean build directories before rebuilding can cause compiled extensions to use old code.

  6. Missing Extension Tests: Verifying only Python code tests while leaving Cython extension tests unrun.

Reference Materials

For a comprehensive list of NumPy 2.0 migration changes, see references/​numpy2_migration.md.

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View