38

我正在关注一个名为“以正确的方式启动 Django 1.4 项目”的教程,该教程提供了有关如何使用 virtualenv 和 virtualenvwrapper 等的指导。

有一段是这样写的:

如果您使用 pip 安装软件包(我不明白您为什么不这样做),您可以通过简单地安装 virtualenv 和 virtualenvwrapper 来获得后者。

   $ pip install virtualenvwrapper

安装后,将以下行添加到 shell 的启动文件(.zshrc、.bashrc、.profile 等)。

   export WORKON_HOME=$HOME/.virtualenvs
   export PROJECT_HOME=$HOME/directory-you-do-development-in
   source /usr/local/bin/virtualenvwrapper.sh

重新加载您的启动文件(例如源文件 .zshrc),您就可以开始了。

我正在运行 Mac OSX,并且不太了解终端的方式。作者到底是什么意思shell's start-up file (.zshrc, .bashrc, .profile, etc)?我在哪里可以找到这个文件,以便我可以添加这三行?

还有,他是什么意思reload your start up file (e.g. source .zshrc)

我将不胜感激具体针对 OSX 的详细回复。

4

4 回答 4

45

您可能正在使用bash,因此只需将以下 3 行添加到~/.bash_profile

$ cat >> ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/directory-you-do-development-in
source /usr/local/bin/virtualenvwrapper.sh
^D

其中^D表示您键入Control+ D(EOF)。

然后关闭您的终端窗口并打开一个新窗口,或者您可以.bash_profile像这样“重新加载”您的:

$ source ~/.bash_profile
于 2013-02-26T23:40:36.997 回答
6

如果你使用 bash,它通常意味着~/.bash_profile.

在终端和 iTerm 中,默认情况下新的 shell 是登录 shell,因此~/.bashrc根本不会读取。如果为其他平台编写的说明告诉您添加一些东西到.bashrc,您通常必须将其添加到.bash_profile

如果两者都~/.profile存在~/.bash_profile.bash_profile则只读。.profile也可以被其他 shell 读取,但是您添加的许多内容.bash_profile不适用于它们。

从 /usr/share/doc/bash/bash.html:

当 bash 作为交互式登录 shell 或作为带有 --login 选项的非交互式 shell 调用时,它首先从文件/etc/profile中读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找~/.bash_profile~/.bash_login~/.profile,并从第一个存在且可读的文件中读取并执行命令。

[...]

当一个不是登录 shell 的交互式 shell 启动时,bash 读取并执行来自 的命令~/.bashrc(如果该文件存在)。

于 2013-02-27T05:16:20.240 回答
2

我安装了 Anaconda,所以我将这 3 行添加到 ~/.bash_profile

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Documents/Python
source /Users/Username/anaconda3/bin/virtualenvwrapper.sh 

然后通过以下方式重新加载配置文件:

$ source ~/.bash_profile
于 2018-01-16T20:06:08.577 回答
0

我使用一种我认为易于维护的方法。如果您有时使用 Ubuntu 系统,它也可以很好地工作,但是我一定会在我的回答中解决 OP 的 OSX 要求。

  1. .aliases在您的主目录中使用您的别名创建一个文件,例如~/.aliases

  2. 使用. .bashrc_ source ~/.aliases顺便说一句,这就是您实际上需要为 Ubuntu 做的所有事情。

  3. .bashrc从您的文件中调用 OSX ~/.profile,即~/.bash_profile包含:source ~/.bashrc

于 2016-10-06T00:51:36.517 回答