在Windows 7中将包安装到虚拟环境中
我习惯在Mac或者Linux系统上编程,在这些系统中,把软件包安装到虚拟环境里相对简单。不过,在Windows上似乎就没那么顺利了。我一直在尝试在一台64位的Windows 7电脑上,把lxml安装到虚拟环境里。
首先,我用了pip(我最喜欢的工具):
(lxml_tutorial) PS C:\Users\spearsc\Documents\python_projects\lxml_tutorial> pip install lxml
Downloading/unpacking lxml
Running setup.py (path:c:\users\spearsc\appdata\local\temp\pip_build_spearsc\lxml\setup.py) egg_info for package lxml
Building lxml version 3.3.5.
Building without Cython.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
warnings.warn(msg)
warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
Running setup.py install for lxml
Building lxml version 3.3.5.
Building without Cython.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
building 'lxml.etree' extension
gcc -mno-cygwin -mdll -O -Wall -Ic:\users\spearsc\appdata\local\temp\pip_build_spearsc\lxml\src\lxml\includes -IC:\P
ython27\include -IC:\Python27\PC -c src\lxml\lxml.etree.c -o build\temp.win-amd64-2.7\Release\src\lxml\lxml.etree.o -w
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
warnings.warn(msg)
error: command 'gcc' failed: No such file or directory
Complete output from command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\spearsc\\ap
pdata\\local\\temp\\pip_build_spearsc\\lxml\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().rep
lace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\spearsc\appdata\local\temp\pip-c22v0y-record\install-r
ecord.txt --single-version-externally-managed --compile:
Building lxml version 3.3.5.
Building without Cython.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
running install
running build
running build_py
creating build
creating build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\lxml
copying src\lxml\builder.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\cssselect.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\doctestcompare.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\ElementInclude.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\pyclasslookup.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\sax.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\usedoctest.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\_elementpath.py -> build\lib.win-amd64-2.7\lxml
copying src\lxml\__init__.py -> build\lib.win-amd64-2.7\lxml
creating build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\__init__.py -> build\lib.win-amd64-2.7\lxml\includes
creating build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\builder.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\clean.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\defs.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\diff.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\ElementSoup.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\formfill.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\html5parser.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\soupparser.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\usedoctest.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\_diffcommand.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\_html5builder.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\_setmixin.py -> build\lib.win-amd64-2.7\lxml\html
copying src\lxml\html\__init__.py -> build\lib.win-amd64-2.7\lxml\html
creating build\lib.win-amd64-2.7\lxml\isoschematron
copying src\lxml\isoschematron\__init__.py -> build\lib.win-amd64-2.7\lxml\isoschematron
copying src\lxml\lxml.etree.h -> build\lib.win-amd64-2.7\lxml
copying src\lxml\lxml.etree_api.h -> build\lib.win-amd64-2.7\lxml
copying src\lxml\includes\c14n.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\config.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\dtdvalid.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\etreepublic.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\htmlparser.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\relaxng.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\schematron.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\tree.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\uri.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xinclude.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xmlerror.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xmlparser.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xmlschema.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xpath.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\xslt.pxd -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\etree_defs.h -> build\lib.win-amd64-2.7\lxml\includes
copying src\lxml\includes\lxml-version.h -> build\lib.win-amd64-2.7\lxml\includes
creating build\lib.win-amd64-2.7\lxml\isoschematron\resources
creating build\lib.win-amd64-2.7\lxml\isoschematron\resources\rng
copying src\lxml\isoschematron\resources\rng\iso-schematron.rng -> build\lib.win-amd64-2.7\lxml\isoschematron\resources\
rng
creating build\lib.win-amd64-2.7\lxml\isoschematron\resources\xsl
copying src\lxml\isoschematron\resources\xsl\RNG2Schtrn.xsl -> build\lib.win-amd64-2.7\lxml\isoschematron\resources\xsl
copying src\lxml\isoschematron\resources\xsl\XSD2Schtrn.xsl -> build\lib.win-amd64-2.7\lxml\isoschematron\resources\xsl
creating build\lib.win-amd64-2.7\lxml\isoschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_abstract_expand.xsl -> build\lib.win-amd64-2.7\lxm
l\isoschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_dsdl_include.xsl -> build\lib.win-amd64-2.7\lxml\i
soschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_schematron_message.xsl -> build\lib.win-amd64-2.7\
lxml\isoschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_schematron_skeleton_for_xslt1.xsl -> build\lib.win
-amd64-2.7\lxml\isoschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_svrl_for_xslt1.xsl -> build\lib.win-amd64-2.7\lxml
\isoschematron\resources\xsl\iso-schematron-xslt1
copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\readme.txt -> build\lib.win-amd64-2.7\lxml\isoschematr
on\resources\xsl\iso-schematron-xslt1
running build_ext
building 'lxml.etree' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\src
creating build\temp.win-amd64-2.7\Release\src\lxml
gcc -mno-cygwin -mdll -O -Wall -Ic:\users\spearsc\appdata\local\temp\pip_build_spearsc\lxml\src\lxml\includes -IC:\Pytho
n27\include -IC:\Python27\PC -c src\lxml\lxml.etree.c -o build\temp.win-amd64-2.7\Release\src\lxml\lxml.etree.o -w
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
warnings.warn(msg)
error: command 'gcc' failed: No such file or directory
----------------------------------------
Cleaning up...
Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\spearsc\\appdata\\local\\temp\\pip_b
uild_spearsc\\lxml\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __fil
e__, 'exec'))" install --record c:\users\spearsc\appdata\local\temp\pip-c22v0y-record\install-record.txt --single-versio
n-externally-managed --compile failed with error code 1 in c:\users\spearsc\appdata\local\temp\pip_build_spearsc\lxml
Storing debug log for failure in C:\Users\spearsc\pip\pip.log
我之前见过这个错误。我觉得是因为C编译器没有设置好。根据我以前的经验,解决这个问题非常麻烦,所以我决定试试别的方法。
我去了http://www.lfd.uci.edu/~gohlke/pythonlibs/,下载了lxml的exe文件。然后我激活了我的虚拟环境,在里面运行了这个可执行文件。结果这个库被安装到了全局,这不是我想要的,所以我把lxml卸载了。
在我的第三次尝试中,我激活了虚拟环境,使用easy_install来安装lxml。结果又一次,这个包被安装到了全局!我又用pip卸载了lxml。
我真的很失望。我是不是只能把所有的包都安装到全局,忘记使用虚拟环境了?我在网上看到的大部分解决方案似乎都过时了。
2 个回答
在你从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 安装了 lxml 之后,接下来你需要从 "C:/python/lib/site-packages"
这个文件夹里,把以下文件复制到你的虚拟环境的 "/lib/site-packages" 文件夹里。
- lxml(文件夹)
- lxml-3.3.5-py2.7.egg-info(文件夹)
嗯,当我使用我的虚拟环境中的 easy_install
时,它只会在那个虚拟环境里安装,而不会影响全局环境。
不过,除了这个,还有另一种方法可以解决这个问题,那就是从 .exe 文件创建一个 wheel 文件,然后用 pip install
来安装它。(Wheel/.whl 是一种新的标准,取代了 .egg 格式。你可以查看 PEP 427 或 readthedocs 等网站了解更多信息。)
按照以下步骤操作:
pip install wheel
- 下载 .exe 文件
wininst2wheel.exe <下载的 .exe 文件>
pip install <创建的 .whl 文件>