[Fixed] AttributeError: cython_sources – Python

by
Liam Thompson
catboost python python-3.12 python-3.x

Quick Fix: To solve the "AttributeError: cython_sources" issue while installing catboost==1.2.2, use one of these workarounds:

  1. Preinstall cython<3, then install pyyaml without build isolation, followed by installing other dependencies.

  2. Alternatively, create a constraints file to force pip to use cython<3 at build time.

Refer to the GitHub issues (links provided) for more details and updates.

The Problem:

I’m trying to install ‘catboost==1.2.2’ using pip, but the installation fails with errors related to missing attributes during wheel building. The build process seems to be unable to find files in the ‘cython_sources’ attribute, resulting in an ‘AttributeError: cython_sources’. The issue persists despite trying various methods to install the package.

The Solutions:

Solution 1: Preinstall Cython

One way to resolve this issue is by pre-installing Cython with a version less than 3.0.0. Here’s how you can do it:

  1. Open your terminal or command prompt.

  2. Run the following command to install Cython version 2.x:

    pip install "cython<3"
    
  3. After Cython is installed, try installing catboost again:

    pip install catboost==1.2.2
    

This should bypass the error related to cython_sources and allow catboost to be installed successfully.

Solution 2: Use a Constraints File

Another approach is to use a constraints file to force pip to use a specific version of Cython during the installation of catboost. Here’s how to do it:

  1. Create a new file named constraints.txt in a convenient location on your computer.

  2. Open the constraints.txt file and add the following line:

    cython<3
    
  3. Save and close the constraints.txt file.

  4. Now, install catboost using the following command:

    PIP_CONSTRAINT=/path/to/constraints.txt pip install catboost==1.2.2
    
  5. Replace /path/to/constraints.txt with the actual path to the constraints.txt file you created in Step 2.

By using the constraints file, you’re instructing pip to use a version of Cython less than 3.0.0 during the installation of catboost. This should resolve the cython_sources error and allow catboost to be installed successfully.

Q&A

I am facing error in installing catboost==1.2.2. The error is AttributeError: cython_sources

The error is likely related to PyYAML and Cython.

What is the possible solution to the error AttributeError: cython_sources

There are two potential workarounds:

  1. Preinstall cython&lt;3.0.0, install pyyaml without build isolation, then install remaining dependencies.
  2. Use a constrains file to force pip to use cython<3 at build time.

Is there a specific GitHub thread I can refer to for more details?

Yes, you can refer to the following GitHub threads for more details about the error:
https://github.com/catboost/catboost/issues/2520
https://github.com/catboost/catboost/issues/2469
https://github.com/yaml/pyyaml/issues/601

Video Explanation:

The following video, titled "pip install attributeerror cython sources - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... attribute 'cython_sources' error during the installation of a Python package. Remember to keep your dependencies up-to-date and report any ...