如何将 PyYAML 安装到 Python 的本地安装中?

如何将 PyYAML 安装到 Python 的本地安装中?

我已将 Python 2.6.4 安装到预装了 Python 2.3.4 的 Linux 计算机上的主目录(子目录中),因为我需要运行一些代码,但我认为这些代码在 2.3.4 上运行需要做大量工作。(我不在该计算机的 sudoers 列表中。)

我希望我可以运行~/Python-2.6.4/python setup.py install (从我主目录中的 PyYAML 目录,我在那里解压了 PyYAML 源),并且它会足够智能地将其安装到我本地的 Python 2.6.4 安装中。但事实并非如此。(参见 PS)

是否可以将 PyYAML 安装到我的本地 Python 安装中,以便调用该 Python 时“import yaml”可以正常工作?如果可以,我该怎么做?

PS 这是我运行时的输出~/Python-2.6.4/python setup.py install

running install
running build
running build_py
creating build/lib.linux-ppc64-2.6
creating build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/composer.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/nodes.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/dumper.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/resolver.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/events.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/emitter.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/error.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/loader.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/cyaml.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/scanner.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/__init__.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/serializer.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/reader.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/representer.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/constructor.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/tokens.py -> build/lib.linux-ppc64-2.6/yaml
copying lib/yaml/parser.py -> build/lib.linux-ppc64-2.6/yaml
running build_ext
creating build/temp.linux-ppc64-2.6
checking if libyaml is compilable
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall
-Wstrict-prototypes -fPIC -I/home/dspitzer/Python-2.6.4/Include
-I/home/dspitzer/Python-2.6.4 -c
build/temp.linux-ppc64-2.6/check_libyaml.c -o
build/temp.linux-ppc64-2.6/check_libyaml.o
build/temp.linux-ppc64-2.6/check_libyaml.c:2:18: yaml.h: No such file
or directory
build/temp.linux-ppc64-2.6/check_libyaml.c: In function `main':
build/temp.linux-ppc64-2.6/check_libyaml.c:5: error: `yaml_parser_t'
undeclared (first use in this function)
build/temp.linux-ppc64-2.6/check_libyaml.c:5: error: (Each undeclared
identifier is reported only once
build/temp.linux-ppc64-2.6/check_libyaml.c:5: error: for each function
it appears in.)
build/temp.linux-ppc64-2.6/check_libyaml.c:5: error: syntax error
before "parser"
build/temp.linux-ppc64-2.6/check_libyaml.c:6: error: `yaml_emitter_t'
undeclared (first use in this function)
build/temp.linux-ppc64-2.6/check_libyaml.c:8: warning: implicit
declaration of function `yaml_parser_initialize'
build/temp.linux-ppc64-2.6/check_libyaml.c:8: error: `parser'
undeclared (first use in this function)
build/temp.linux-ppc64-2.6/check_libyaml.c:9: warning: implicit
declaration of function `yaml_parser_delete'
build/temp.linux-ppc64-2.6/check_libyaml.c:11: warning: implicit
declaration of function `yaml_emitter_initialize'
build/temp.linux-ppc64-2.6/check_libyaml.c:11: error: `emitter'
undeclared (first use in this function)
build/temp.linux-ppc64-2.6/check_libyaml.c:12: warning: implicit
declaration of function `yaml_emitter_delete'

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)
running install_lib
creating /usr/local/lib/python2.6
error: could not create '/usr/local/lib/python2.6': Permission denied

答案1

查看setup.py 的--home--user--prefix开关。这些控制文件的安装位置。

不过,从您的构建日志来看,我不确定这是否是问题所在。它似乎无法找到 yaml.h。它是 PyYAML 源的一部分吗?还是它是 PyYAML 需要但您忘记安装的 libyaml 的一部分?

答案2

我没有正确安装 Python。当我按照克拉斯特的指示https://stackoverflow.com/questions/2278028/how-do-i-work-around-this-problem-creating-a-virtualenv-environment-with-a-custom/2278059#2278059之后我就可以毫无问题地安装 PyYAML 了。(不需要 virtualenv。)

相关内容