18

I want to use PyYaml in my pip project, but am having trouble using it as a dependency. Mainly the problem is thet PyYaml in pip is not a cross platform install.

How do I install pyyaml using pip so that it works. Note, on a current fresh Ubuntu install I get the following error when running pip install pyyaml

Installing collected packages: pyyaml Running setup.py install for pyyaml checking if libyaml is compilable gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -fPIC -I/usr/include/python3.2mu -c build/temp.linux-x86_64-3.2/check_libyaml.c -o build/temp.linux-x86_64-3.2/check_libyaml.o build/temp.linux-x86_64-3.2/check_libyaml.c:2:18: fatal error: yaml.h: No such file or directory compilation terminated. libyaml is not found or a compiler error: forcing --without-libyaml (if libyaml is installed correctly, you may need to specify the option --include-dirs or uncomment and modify the parameter include_dirs in setup.cfg) Successfully installed pyyaml 

Note that the error says "successfully installed" but it is not. I can not import yaml

I am not looking for answers that say "use apt-get" due to my very first sentence. I need the install to be cross platform and work as a pip dependency I am not simply wondering on how to install it correctly.

If this is not possible, is there any library I can use in replacement?

5
  • Try to install in virtualenv, virtualenv pyenv && . pyenv/bin/activate && pip install pyyaml && python -c 'import yaml' is it working? Commented Jul 16, 2014 at 21:56
  • @spinus works with python3 but not python2.7 Commented Jul 16, 2014 at 22:19
  • For me works also on python2.7. Pretty strange, I have no idea what's wrong. I know you wrote that it's fresh ubuntu, but is it really that fresh? No other ideas what can be wrong. Commented Jul 16, 2014 at 22:47
  • @spinus ok I am not sure. I just spawned a new instance in AWS to test it, and pyyaml is apparently a default library. Not sure why I didn't have it the first time. So the problem appears to be individualized. (might delete this question) Commented Jul 16, 2014 at 23:02
  • How did you install pip? It is not installed on a fresh Ubuntu install AFAIK Commented Jun 10, 2018 at 8:40

3 Answers 3

27

You will need some extra packages to build it.

First of all you need to uninstall pyyaml, or it will complain later that it is already installed

pip uninstall pyyaml 

Then install the following packages:

sudo apt-get install libyaml-dev libpython2.7-dev 

Finally install it again

pip install pyyaml 
Sign up to request clarification or add additional context in comments.

6 Comments

libpython is default for python 2.7 on ubuntu
Nope, libyaml-dev is not required for pyyaml to work. If it's installed pyyaml can use it as more efficient implementation but it's not required. For me pip install pyyaml without external stuff and with the same error working just fine.
I need the install to work as a pip dependency and cross-platform. I am sorry if this wasnt clear, I have edited my question.
@Salem: This was great! Thanks! [[before]] python -m timeit -s 'import yaml; y=open("config.yml", "r").read()' 'yaml.load(y) ==> 100 loops, best of 3: 5.63 msec per loop [[after]] python -m timeit -s 'import yaml; y=open("config.yml", "r").read()' 'yaml.load(y, Loader=yaml.CLoader)' ==> 1000 loops, best of 3: 505 usec per loop
Note also that you might have to use --no-cache-dir when doing that last pip install to force pip to rerun the setup rather than using the cache.
|
0

Using Salem's answer as a guide, I was able to install PyYAML with libyaml bindings in Python 3.8 by doing the following:

sudo apt-get install libyaml-dev libpython3.8-dev pip install pyyaml --global-option=--with-libyaml 

Without specifying the --global-option=--with-libyaml option, it would not pick up the bindings, making the CSafeLoader and CSafeDumper classes inaccessible.

Comments

-3

Virtual Environment Setting

Python version: 3.6.3 Operating system: Windows, 64 Bits 

Keras was not installing because of pyyaml setting not meeting up for python 3.6. I already had Anaconda installed in a different virtualenv with keras working fine (pyyaml module installed). I just used conda install pyyaml instead of pip and it got installed, thereby using pip install keras to install the keras library in my new virtual environment.

if you have anaconda installed already, try using

conda install yaml

and see if it works. It worked for me!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.