1

I'm writing a script in python to update some YAML config files containing jinja2. I found this answer showing how to do it using ruamel.yaml and ruamel.yaml.jinja2 packages and it works absolutely fine on windows10 using this configuration:

$ python -V
Python 2.7.5
$ python -m pip list
[...]
ruamel.ordereddict            0.4.13
ruamel.yaml                   0.15.94
ruamel.yaml.jinja2            0.2.2
[...]

and this code:

from ruamel.yaml import YAML

yamlLoader = YAML(typ='jinja2')

But when I try to use it on a CentOS virtual machine, which is the target environment for this script, I get this error:

  File "/opt/salt/mig/cnamts_migrate.py", line 17, in <module>
    yamlLoader = YAML(typ='jinja2')
  File "/usr/lib64/python2.7/site-packages/ruamel/yaml/main.py", line 138, in __init__
    'typ "{}"not recognised (need to install plug-in?)'.format(self.typ)
NotImplementedError: typ "jinja2"not recognised (need to install plug-in?)

I can't find any difference in neither Python configuration nor packages' version.

On CentOS VM:


$ python -V
Python 2.7.5
$ pip list
Package                      Version
---------------------------- -----------
pip                          19.1
ruamel.ordereddict           0.4.13
ruamel.yaml                  0.15.94
ruamel.yaml.jinja2           0.2.2
setuptools                   41.0.1
num3ri
  • 822
  • 16
  • 20

2 Answers2

0

This path

/usr/lib64/python2.7/site-packages/ruamel/yaml/main.py

in the error message gives me the impression that you are using a system wide Python installation. You should (IMHO even in a VM) use python virtualenv for any utility (and preferaby one per non-related utility). You don't show the output of pip for your CentOS machine, which is way more interesting than the one on your Windows box.

$ more /etc/centos-release
CentOS Linux release 7.3.1611 (Core) 
$ /opt/python/3.7/bin/python -m venv /tmp/so-55900745
$ source /tmp/so-55900745/bin/activate
(so-55900745) $ pip install ruamel.yaml.jinja2
Collecting ruamel.yaml.jinja2
  Downloading https://files.pythonhosted.org/packages/4f/b4/9676d4fa53d921f98f40dcda2ecfdb9fba2b68fbdccec3d9d4d2c87d96a7/ruamel.yaml.jinja2-0.2.2-py2.py3-none-any.whl
Collecting ruamel.yaml>=0.15.10 (from ruamel.yaml.jinja2)
  Downloading https://files.pythonhosted.org/packages/bb/e3/8c06f90dab796bd5baf5da2482cf919bab3145389196814ec3180d4c7bd5/ruamel.yaml-0.15.94-cp37-cp37m-manylinux1_x86_64.whl (647kB)
    100% |████████████████████████████████| 655kB 9.3MB/s 
Installing collected packages: ruamel.yaml, ruamel.yaml.jinja2
Successfully installed ruamel.yaml-0.15.94 ruamel.yaml.jinja2-0.2.2
(so-55900745) $ pip list
Package            Version
------------------ -------
pip                19.1   
ruamel.yaml        0.15.94
ruamel.yaml.jinja2 0.2.2  
setuptools         40.8.0 
(so-55900745) $ python
Python 3.7.3 (default, Apr  3 2019, 11:33:06) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> exit()
(so-55900745) $ deactivate
$ rm -rf /tmp/so-55900745/
$ virtualenv -p /opt/python/2.7/bin/python /tmp/so-55900745
Running virtualenv with interpreter /opt/python/2.7/bin/python
New python executable in /tmp/so-55900745/bin/python
Installing setuptools, pip, wheel...
done.
$ source /tmp/so-55900745/bin/activate
(so-55900745) $ pip install ruamel.yaml.jinja2
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting ruamel.yaml.jinja2
  Using cached https://files.pythonhosted.org/packages/4f/b4/9676d4fa53d921f98f40dcda2ecfdb9fba2b68fbdccec3d9d4d2c87d96a7/ruamel.yaml.jinja2-0.2.2-py2.py3-none-any.whl
Collecting ruamel.yaml>=0.15.10 (from ruamel.yaml.jinja2)
  Downloading https://files.pythonhosted.org/packages/a2/59/e8cb144511e47e068efdb71a85f35d00b32fc5f05a9e9a17df265ec252b5/ruamel.yaml-0.15.94-cp27-cp27mu-manylinux1_x86_64.whl (600kB)
     |████████████████████████████████| 604kB 2.0MB/s 
Collecting ruamel.ordereddict; platform_python_implementation == "CPython" and python_version <= "2.7" (from ruamel.yaml>=0.15.10->ruamel.yaml.jinja2)
  Downloading https://files.pythonhosted.org/packages/f3/2c/fa6d75dc459b371ed3b88fdbf8042785ce1655073c884fd97bdbb9f48e01/ruamel.ordereddict-0.4.13-cp27-cp27mu-manylinux1_x86_64.whl (99kB)
     |████████████████████████████████| 102kB 12.7MB/s 
Installing collected packages: ruamel.ordereddict, ruamel.yaml, ruamel.yaml.jinja2
Successfully installed ruamel.ordereddict-0.4.13 ruamel.yaml-0.15.94 ruamel.yaml.jinja2-0.2.2
(so-55900745) $ python
Python 2.7.15 (default, Aug 10 2018, 11:41:46) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> exit()
(so-55900745) $ deactivate
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Hi ! Thanks for your answer. I am effectively using a system wide python installation. I didn't show the output of the pip list on CentOS because it was exactly the same than the one on Windows. After trying in a virtualenv, it works on the VM. I wonder why this happens, shouldn't python be OS independent ? – Evrard-Nil Daillet Apr 29 '19 at 12:00
  • Python should be system independent, but there might be a difference in that your system wide site-packages is not clean (e.g. because you had originally installed ruamel.yaml using the CentOS package manager). That is one of the reasons to always use a virtualenv (the other being that you can break the python your system is using for its utilities). – Anthon Apr 29 '19 at 13:08
0

I ran into the same error message. but with python3.6.

The fix for me was to pip instead of easy_install.

Here's the easy_install related output:

(env) $ easy_install -q ruamel.yaml.jinja2
(env) $ easy_install -q ruamel.yaml
(env) $ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "REDACTED/env/lib/python3.6/site-packages/ruamel.yaml-0.15.96-py3.6-linux-x86_64.egg/ruamel/yaml/main.py", line 138, in __init__
    'typ "{}"not recognised (need to install plug-in?)'.format(self.typ)
NotImplementedError: typ "jinja2"not recognised (need to install plug-in?)

Here's the same thing installing with pip:

(env) $ pip -q install ruamel.yaml.jinja2
(env) $ pip -q install ruamel.yaml
(env) $ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> yamlLoader = YAML(typ='jinja2')
>>> print("¯\_(ツ)_/¯")
¯\_(ツ)_/¯

If you are using a setup.py in your project, check out Can I use `pip` instead of `easy_install` for `python setup.py install` dependency resolution?

pwan
  • 2,894
  • 2
  • 24
  • 38