如何使用 Python virtualenv [英] How to use Python virtualenv

查看:31
本文介绍了如何使用 Python virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景是:我使用的是 Python 2.7 附带的 Ubuntu 11,我想运行支持 Python 2.5/2.6 的 Mozilla JetPack 和仅支持 Python 2.5 的 Google App Engine.

The scenario is: I am on Ubuntu 11 which comes with Python 2.7, I want to run Mozilla JetPack which supports Python 2.5/2.6 and Google App Engine which only supports Python 2.5.

读到删除 Python 2.7 并不是一个好主意,因为 Ubuntu 可能正在使用它.所以正确的方法是使用virtualenv.但我很迷失使用它.我按照 本指南

Read that its not a good idea to remove Python 2.7 as Ubuntu maybe using it. So the correct way is to use virtualenv. But I am quite lost using it. I installed Python 2.5 in /usr/local/python25 following this guide

我试过了

jiewmeng@JM:/usr/local/python25/bin$ ./python --version
Python 2.5.5
jiewmeng@JM:/usr/local/python25/bin$ ./python virtualenv /works/tmp/test
./python: can't open file 'virtualenv': [Errno 2] No such file or directory

然后下面的工作,但我将使用 Python 2.7

then the below works but I will be using Python 2.7

jiewmeng@JM:/usr/local/python25/bin$ virtualenv /works/tmp/test
New python executable in /works/tmp/test/bin/python
Installing distribute.................................................................................................................................................................................done.
jiewmeng@JM:/usr/local/python25/bin$ cd /works/tmp/test/bin
jiewmeng@JM:/works/tmp/test/bin$ ls
activate  activate_this.py  easy_install  easy_install-2.7  pip  python
jiewmeng@JM:/works/tmp/test/bin$ ./python --version
Python 2.7.1+

此外,我如何使用此版本的 Python 运行 Mozilla JetPack 或 Google App Engine?抱歉,我是 Python(和 Linux/Ubuntu)新手

Also, how do I then run Mozilla JetPack or Google App Engine with this version of Python? Sorry I am new to Python (and Linux/Ubuntu)

推荐答案

大纲:

  • 首先cd到/usr/local/python25/bin
  • 下载 Python2.5 的设置工具 (setuptools-0.6c11-py2.5.egg)
  • 安装(sh setuptools-0.6c11-py2.5.egg).
  • 现在安装 pip (easy_install pip).
  • 使用 pip(pip install v... 等)安装 virtualenvvirtualenvwrapper.
  • 配置 WORKON_HOME 以使 virtualenv 包装器工作(export WORKON_HOME = $HOME/.virtualenvs).您可以使用任何其他您想要的目录(不仅仅是 $HOME/.virtualenvs).只需确保使用完整路径即可.
  • 现在创建一个 virtualenv (mkvirtualenv foobar).
  • 切换到新的 virtualenv (workon foobar).
  • 现在使用 pip install blah
  • 安装 GAE、JetPack 和任何你想要的东西
  • First cd to /usr/local/python25/bin
  • Download setuptools for Python2.5 (setuptools-0.6c11-py2.5.egg)
  • Install it (sh setuptools-0.6c11-py2.5.egg).
  • Now install pip (easy_install pip).
  • Install virtualenv and virtualenvwrapper using pip (pip install v... etc.).
  • Configure WORKON_HOME for virtualenv wrapper to work (export WORKON_HOME = $HOME/.virtualenvs). You can use any other directory you want (not just $HOME/.virtualenvs). Just make sure to use the full path.
  • Now create a virtualenv (mkvirtualenv foobar).
  • Switch to the new virtualenv (workon foobar).
  • Now install GAE, JetPack and whatever you want using pip install blah

为什么您的安装不起作用?

看起来你没有为 Python2.5 安装 virtualenv.因此这是行不通的.

Looks like you did not install virtualenv for Python2.5. Hence this will not work.

jiewmeng@JM:/usr/local/python25/bin$ ./python virtualenv /works/tmp/test

您可以通过在该目录中运行 ls 命令来检查.我怀疑你不会在那里找到 virtualenv 文件.

You can check by running ls command in that directory. I suspect you won't find virtualenv file there.

不过这对你有用.

jiewmeng@JM:/usr/local/python25/bin$ virtualenv /works/tmp/test

因为它使用系统默认 Python2.7 的 virtualenv 文件.您可以检查 which virtualenv 并打开 virtualenv 脚本.你会看到#!将指向系统默认python.

Because it is using the virtualenv file for system default Python2.7. You can check which virtualenv and opening the virtualenv script. You'll see that the #! will point to system default python.

因此,您需要先安装适用于 Python 2.5 的 easy_install 和 pip,然后才能为 Python 2.5 创建 virtualenv.只需按照上述步骤操作即可.

So you need to install the easy_install and pip for Python 2.5 before you can create virtualenv for Python 2.5. Just follow the steps outlined above.

这篇关于如何使用 Python virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆