【问题标题】:Terminal: Where is the shell start-up file?终端:shell 启动文件在哪里?
【发布时间】:2013-02-12 15:19:19
【问题描述】:

我正在学习一个名为 Starting a Django 1.4 Project the Right Way 的教程,其中提供了有关如何使用 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 的详细回复。

【问题讨论】:

    标签: macos shell terminal virtualenvwrapper


    【解决方案1】:

    您可能正在使用 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
    

    【讨论】:

    • 一个小问题:.profile 可以被许多不同的 shell 读取,其中一些可能无法将source 识别为. 的更易读的同义词。使用. /usr/local/bin/virtualenwrapper.sh 代替第三行。或者,将这三行放在.bash_profile 中。
    • 如果 .bash_profile 已经存在,则根本不会读取 .profile。
    • 感谢 cmets - 我已将答案更新为明确使用 .bash_profile 而不是 .profile
    • 我正在运行 windows 7 并且我有 cmd.exe 那么我在哪里添加这些行?
    • @UFM:您可能应该安装cygwin 以便可以在 Windows 上使用 bash shell。
    【解决方案2】:

    如果你使用 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 的命令(如果该文件存在)。

    【讨论】:

      【解决方案3】:

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

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

      2. 使用source ~/.aliases 从您的.bashrc 文件(每次针对新的shell 进程执行)执行此文件。顺便说一句,这就是您实际上需要为 Ubuntu 做的所有事情。

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

      【讨论】:

        【解决方案4】:

        我安装了 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
        

        【讨论】:

          猜你喜欢
          • 2011-06-24
          • 1970-01-01
          • 2015-08-26
          • 1970-01-01
          • 1970-01-01
          • 2018-08-20
          • 1970-01-01
          • 2020-11-15
          • 2017-05-14
          相关资源
          最近更新 更多