如何在 Python 中使用 virtualenv? [英] How to use virtualenv with Python?

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

问题描述

我打算为 Python 安装一个虚拟环境,以便将我的 Python 包分开.这样做的动机之一也是在我的机器(Ubuntu 14.04)上有两个版本的 Python 共存.我有以下奇迹:

I am planning to install a virtual environment for Python in order to keep my Python packages separate. One of the motivations for this is also to have two versions of Python on my machine (Ubuntu 14.04) co-existing. I have the following wonders:

  1. Python、PIP 和 virtualenv 应该按什么顺序安装?重要吗?
  2. 完成后,如何在 virtualenv 下保持两个 Python 版本分开?
  3. 假设我正在处理不同的项目,是否建议将每个项目保存在由 virtualenv 创建的单独文件夹中?

我想知道专家的意见,以便以正确的方式和明智地做事.

I would like to know experts opinion in order to do things in the right manner and wisely.

推荐答案

使用 virtualenv 在 Python 程序员中很常见.这些链接比我的回答更有用:

Using virtualenv is common amongst Python programmers. These links will be more useful than my answers:

  1. 是的,这很重要.Pip 使用 Python,但由于 Ubuntu 预装了 Python 版本(在您的情况下,安装了 2 和 3),您不必担心这一点.但顺序是 Python -> PIP -> virtualenv.

  1. Yes, it does matter. Pip uses Python, but since Ubuntu comes pre-installed with a version of Python (In your case both 2 and 3 are installed), you shouldn't have to worry about this. But the order would be Python -> PIP -> virtualenv.

在一个新的空项目文件夹中 cd 后,您可以使用 virtualenv -p/path/to/python/使用您选择的 Python 版本创建 virtualenv版本 venv.您可以使用 which python2which python3 找到路径.

Once you cd in a new, empty project folder, you can create the virtualenv with the Python version of your choice with virtualenv -p /path/to/python/version venv. You can find the path with which python2 or which python3.

如果我正确理解你的问题 - 是的.virtualenv 的重点是将每个项目保存在一个单独的文件夹中,并设置了自己的 virtualenv.即使是一个小项目,您也会更加熟悉 virtualenv 的概念(甚至可能是像 Docker 这样的容器).

If I understand your question correctly - yes. The whole point of virtualenv is to keep each project in a separate folder with its own virtualenv set up. Even with a small project, you will just become more familiar with the concept of virtualenv (and maybe even containers like Docker).

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

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