在MacOS上安装virtualenv和virtualenvwrapper [英] Install virtualenv and virtualenvwrapper on MacOS

查看:52
本文介绍了在MacOS上安装virtualenv和virtualenvwrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 OSX 上为 Python 安装和配置 virtualenv virtualenvwrapper ?

How to install and configure virtualenv and virtualenvwrapper for Python on OSX?

推荐答案

要重复安装 virtualenv virtualenvwrapper ,您需要正确配置的 Python (此示例使用 Python 3.x ,但处理过程与 Python 2.x 相同).

To install virtualenv and virtualenvwrapper for repetitive use you need a correctly configured Python (this example uses Python 3.x but process is identical for Python 2.x).

尽管您可以从Python网站获得 python 安装程序,但我还是强烈建议反对.在 MacOS 上安装 Python 的最方便且面向未来的方法是 brew .

Although you can get python installer from Python website I strongly advice against it. The most convenient and future-proof method to install Python on MacOS is brew.

Python网站的安装程序与
brew 的主要区别在于,安装程序将 python 软件包放入:

Main difference between installer from Python website and brew is that installer puts python packages to:

/Library/Frameworks/Python.framework/Versions/3.x

另一方面,

Brew 安装 Python Pip & Setuptools 并将所有内容放入:

Brew on the other hand installs Python, Pip & Setuptools and puts everything to:

/usr/local/bin/python3.x/site-packages

尽管它现在对您可能没有任何影响–稍后会出现.

And though it may not make any difference to you now – it will later on.

  1. 安装酿造


查看 brew 安装页面或直接在您的终端中运行:


Check out brew installation page or simply run this in your terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  1. 安装Python


要使用 brew 安装 python ,请运行:

brew install python3

现在,您的系统需要知道在哪里寻找新安装的 Python 软件包.将此行添加到您的〜/.zshrc (或〜/.bash_profile (如果您使用的是 bash )):

Now your system needs to know where to look for freshly installed Python packages. Add this line to youre ~/.zshrc (or ~/.bash_profile if you're using bash):

export PATH=/usr/local/share/python:$PATH

重新启动您的终端.为了确保您已正确完成所有操作,请运行哪个python3 ,作为回报,您应该收到/usr/local/bin/python .

Restart your terminal. To make sure you've done everything correctly run which python3 and in return you should receive /usr/local/bin/python.

  1. 安装 virtualenv & virtualenvwrapper
  1. Install virtualenv & virtualenvwrapper


现在是时候安装 virtualenv virtualenvwrapper 以便能够使用 workon 命令并在虚拟环境之间进行切换了.这是通过 pip :


Now it's time to install virtualenv and virtualenvwrapper to be able to use workon command and switch between virtual environments. This is done using pip:

pip3 install virtualenv virtualenvwrapper

  1. 设置 virtualenv 变量


为您的虚拟环境定义默认路径.例如,您可以在中创建一个隐藏目录,并使用 mkdir〜/.virtualenvs 将其命名为 .virtualenvs .将 virtualenv 变量添加到 .zshrc (或 .bash_profile )中.


Define a default path for your virtual environments. For example you can create a hidden directory inside ~ and called it .virtualenvs with mkdir ~/.virtualenvs. Add virtualenv variables to .zshrc (or .bash_profile).

您的 .zshrc (或 .bash_profile )的最终版本应包含以下信息,以与已安装的软件包一起正常工作:

Final version of your .zshrc (or .bash_profile) should contain this information to work properly with installed packages:

# Setting PATH for Python 3 installed by brew
export PATH=/usr/local/share/python:$PATH

# Configuration for virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

重启终端.您应该能够使用 mkvirtualenv workon 命令,包括自动补全.

Restart your terminal. You should be able to use mkvirtualenv and workon commands including autocompletion.

有关如何创建具有特定功能的virtualenv的小提示版本的Python.

Here's a little tip on how to create virtualenv with specific version of Python.

如果您使用的是 MacOS Mojave ,并且要安装的是 Python3.6 从冲泡瓶中,您可能有 pip 这里是可能有帮助的解决方案.

In case you are using MacOS Mojave and you are installing Python3.6 from brew bottle you might have a problem with pip, here's a solution that might help.


随着时间的推移,你们中的一些人可能想要安装多个 Python 版本,每个版本具有多个虚拟环境.这就是 pyenv pyenv-virtualenv 发挥作用.


With time some of you may want to install multiple Python versions with multiple virtual environments per version. That's where pyenv and pyenv-virtualenv come into play.

这篇关于在MacOS上安装virtualenv和virtualenvwrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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