您的位置:首页 > 编程语言 > Python开发

Windows XP搭建virtualenv

2014-04-05 15:40 363 查看
       反复试验几遍终于把virtualenv弄好了,在此做个记录,希望遇到相同问题的朋友可以参考。

       我的python是2.7.6版,安装在 D:\Python27

      第一种方法,直接解压使用:

        步骤1,下载:

下载virtualenv, https://pypi.python.org/pypi/virtualenv#downloads 或者 https://github.com/pypa/virtualenv/tarball/develop

        步骤2,解压、创建文件夹:

本来virtualenv 应该是可以直接使用的,绿色软件。官方如下解释,注意标红的语句:

To use locally from source:

$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ python virtualenv.py myVE


       于是下载了virtualenv-1.11.4.tar.gz,解压到D盘根目录;在D盘根目录新建一个文件夹暂且命名为 MyENV 吧。

       步骤3,创建virtual environment :

       运行cmd

D:\>cd MyENV

D:\MyENV>D:\Python27\python D:\virtualenv-1.11.4\virtualenv.py TestENV      
注释:我没把D:\Python27加入环境变量,故用绝对路径运行python

       virtualenv-1.11.4\virtualenv_support 中的包自带了pip和setuptools,所以它会自动安装setuptools和pip,但是我运行了出现了如下结果,注意标红的结果:

New python executable in TestENV\Scripts\python.exe

D:\MyENV\TestENVInstalling setuptools, pip...Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools

Cleaning up...

Exception:

Traceback (most recent call last):

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\basecommand.py", line 122, in main

    status = self.run(options, args)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\commands\install.py", line 278, in run

    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\req.py", line 1177, in prepare_files

    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 209, in find_requirement

    file_locations, url_locations = self._sort_locations(locations)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 128, in _sort_locations

    sort_path(os.path.join(path, item))

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 109, in sort_path

    if mimetypes.guess_type(url, strict=False)[0] == 'text/html':

  File "D:\Python27\Lib\mimetypes.py", line 297, in guess_type

    init()

  File "D:\Python27\Lib\mimetypes.py", line 358, in init

    db.read_windows_registry()

  File "D:\Python27\Lib\mimetypes.py", line 258, in read_windows_registry

    for subkeyname in enum_types(hkcr):

  File "D:\Python27\Lib\mimetypes.py", line 249, in enum_types

    ctype = ctype.encode(default_encoding) # omit in 3.x!

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)

Storing debug log for failure in C:\Documents and Settings\Administrator\pip\pip.log

  Complete output from command D:\MyENV\TestENV\Scripts\python.exe -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip:

  Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools

Cleaning up...

Exception:

Traceback (most recent call last):

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\basecommand.py", line 122, in main

    status = self.run(options, args)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\commands\install.py", line 278, in run

    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\req.py", line 1177, in prepare_files

    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 209, in find_requirement

    file_locations, url_locations = self._sort_locations(locations)

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 128, in _sort_locations

    sort_path(os.path.join(path, item))

  File "D:\virtualenv-1.11.4\virtualenv_support\pip-1.5.4-py2.py3-none-any.whl\pip\index.py", line 109, in sort_path

    if mimetypes.guess_type(url, strict=False)[0] == 'text/html':

  File "D:\Python27\Lib\mimetypes.py", line 297, in guess_type

    init()

  File "D:\Python27\Lib\mimetypes.py", line 358, in init

    db.read_windows_registry()

  File "D:\Python27\Lib\mimetypes.py", line 258, in read_windows_registry

    for subkeyname in enum_types(hkcr):

  File "D:\Python27\Lib\mimetypes.py", line 249, in enum_types

    ctype = ctype.encode(default_encoding) # omit in 3.x!

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)

Storing debug log for failure in C:\Documents and Settings\Administrator\pip\pip.log

----------------------------------------

...Installing setuptools, pip...done.

Traceback (most recent call last):

  File "D:\virtualenv-1.11.4\virtualenv.py", line 2338, in <module>

    main()

  File "D:\virtualenv-1.11.4\virtualenv.py", line 824, in main

    symlink=options.symlink)

  File "D:\virtualenv-1.11.4\virtualenv.py", line 992, in create_environment

    install_wheel(to_install, py_executable, search_dirs)

  File "D:\virtualenv-1.11.4\virtualenv.py", line 960, in install_wheel

    'PIP_NO_INDEX': '1'

  File "D:\virtualenv-1.11.4\virtualenv.py", line 902, in call_subprocess

    % (cmd_desc, proc.returncode))

OSError: Command D:\MyENV\TestENV\Scripts\python.exe -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 2

     

根据UnicodeDecodeError when install in Windows 7
安装setuptools 失败, 使用ez_setup.py安装setuptools报错,这几篇文章,

以及官方
Issue9291,Side by Side Diff: Lib/mimetypes.py显示

是D:\Python27\Lib\minetypes.py  中的错误,的确和我安装的(上面红色文字)错误一样。就是windows中文用户,注册表HEY_CLASSES_ROOT中存在中文编码的文字,

导致minetypes.py错误。

打开 D:\Python27\Lib\minetypes.py  ,

解决办法 1:

查找

default_encoding = sys.getdefaultencoding()


在它前面增加这几行:

if sys.getdefaultencoding() != 'gbk':
reload(sys)
sys.setdefaultencoding('gbk')

default_encoding = sys.getdefaultencoding()


如果 gbk,编码不行可以试试 utf8 或者其他编码,总之应该是系统的编码方式。

解决办法 2:

请按这篇文章,Side by Side Diff: Lib/mimetypes.py显示,将minetypes.py 修改成右侧的样子。

最终结果:

再次执行 D:\MyENV>D:\Python27\python D:\virtualenv-1.11.4\virtualenv.py TestENV

就成功了。这是我在虚拟机里反复测试的结果,不过路径改成了C盘







这种方法的好处是不会在Python27的文件夹和 Lib\site-packages中生成文件,可以保持Python27安装环境的整洁。

而setuptools 和 pip 都安装到了虚拟环境 TestENV 中。

    

第二种方法,setuptools + pip + virtualenv :

      
第一次安装根本没注意看minetypes.py的错误,以下的两种安装方式也应该先按第一种方法修改minetypes.py, 否则将失败。

       参考了这两篇文章,

How To Install Python, pip, and virtualenv on Windows with PowerShell

Windows Python虚拟环境配置(Distribute + pip + virtualenv + virtualenvwrapper-powershell)




注:(不过这两篇文章首先不是在windows xp;第二我没有PowerShell;第三我试过装distribute了,它能够安装成功,

但是再使用vitualenv的时候还是出现第一步中minetypes.py的错误,后来才知道应该先修改minetypes.py。

而且貌似distribute本来是setuptools的分支,用来替代setuptools的,但现在它好像又被合并回setuptools中了)

方法1.通过pip安装

如下是pip的安装方法:

如果setuptools 或者 distribute没安装的话,get-pip.py会自动安装setuptools的。

Install pip

To install or upgrade pip, securely download
get-pip.py.
[1]

Then run the following (which may require administrator access):

python get-pip.py


If
setuptools (or
distribute) is not already installed, get-pip.py willinstallsetuptools for you.[2]

To upgrade an existing
setuptools (or
distribute), run pip
install -U setuptools[3]

直接下载
get-pip.py ,并运行

python get-pip.py


安装完成之后,再通过pip安装virtualenv

pip install virtualenv


方法2.先安装setuptools,再安装pip,再安装virtualenv:

根据setuptools官方文档 说明:

For Windows 7 and
earlier, download
ez_setup.py using your favorite webbrowser or other technique and "run" that file.

下载
ez_setup.py

执行

python ez_setup.py


再按照方法1执行就行了。

参考文献:

Side by Side Diff: Lib/mimetypes.py  : http://bugs.python.org/review/9291/diff/1663/Lib/mimetypes.py

Issue9291  :http://bugs.python.org/issue9291

UnicodeDecodeError when install in Windows 7https://bitbucket.org/pypa/setuptools/issue/127/unicodedecodeerror-when-install-in-windows

安装setuptools 失败http://www.oschina.net/question/1409342_134600

使用ez_setup.py安装setuptools报错http://blog.csdn.net/window_10/article/details/19686007



How To Install Python, pip, and virtualenv on Windows with PowerShell http://www.tylerbutler.com/2012/05/how-to-install-python-pip-and-virtualenv-on-windows-with-powershell/


Windows Python虚拟环境配置(Distribute + pip + virtualenv + virtualenvwrapper-powershell)http://my.phirobot.com/blog/2014-02-python_in_windows.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息