Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load CLoader with pyyaml

Tags:

python

pyyaml

I'm working on a python project using pyyaml. I need to run it in a Docker container based on bitnami/minideb:jessie. Python version is 2.7.9. The original code is using CLoader and I cannot change it currently. Any reason CLoader fails to load but Loader is fine ?

>>> import yaml
>>> yaml.__version__
'3.12'
>>> from yaml import Loader
>>> from yaml import CLoader
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name CLoader
>>>

I cannot figure out what I'm missing here. Any idea ?

Running it from the Docker image python:2.7.9 does not raise any error then:

$ docker run -ti python:2.7.9 bash
#/ python
>>> from yaml import CLoader
>>> from yaml import Loader
>>>
like image 303
Luc Avatar asked Dec 08 '17 13:12

Luc


2 Answers

By default, the setup.py script checks whether LibYAML is installed and if so, builds and installs LibYAML bindings.

This is the minimum to get CLoader compiled and installed.

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
    python3 python3-dev python3-pip gcc libyaml-dev
RUN pip3 install pyyaml
# verify
RUN python3 -c "import yaml; yaml.CLoader"
like image 199
Xuan Avatar answered Nov 15 '22 00:11

Xuan


I ran into the same problem. You need to install the libyaml-dev package, then install libyaml and pyyaml from source. Here's the complete Dockerfile for minideb:jessie:

FROM bitnami/minideb:jessie

RUN apt-get update
RUN apt-get install -y \
        automake \
        autoconf \
        build-essential \
        git-core \
        libtool \
        libyaml-dev \
        make \
        python \
        python-dev \
        python-pip

RUN pip install --upgrade pip
RUN pip install Cython==0.29.10

RUN mkdir /libyaml
WORKDIR /libyaml
RUN git clone https://github.com/yaml/libyaml.git . && \
    git checkout dist-0.2.2 && \
    autoreconf -f -i && \
    ./configure && \
    make && \
    make install

RUN mkdir /pyyaml
WORKDIR /pyyaml
RUN git clone https://github.com/yaml/pyyaml.git . && \
    git checkout 5.1.1 && \
    python setup.py install

RUN python -c "import yaml; from yaml import CLoader; print 'Loaded CLoader!'"
like image 25
mtlynch Avatar answered Nov 14 '22 22:11

mtlynch