【问题标题】:How to switch between Python paths?如何在 Python 路径之间切换?
【发布时间】:2019-01-30 19:46:13
【问题描述】:

我最近下载了 anaconda,它将我的默认 python 路径更改为

 $ which python
 /anaconda3/bin/python

但是,我想在它和默认值之间切换,

/usr/bin/python

这样做的最佳方法是什么?

【问题讨论】:

标签: python macos anaconda


【解决方案1】:

如果你安装了最新版本(2018.12)并让它自动修改你的.bashrc文件,那么你只需要停用conda即可使用系统python。然后使用 conda activate 切换回 conda 环境。

[builder@3abd754f9aeb ~]$ which python
~/anaconda2/bin/python
[builder@3abd754f9aeb ~]$ conda deactivate
[builder@3abd754f9aeb ~]$ which python
/usr/bin/python
[builder@3abd754f9aeb ~]$ conda activate
(base) [builder@3abd754f9aeb ~]$ which python
~/anaconda2/bin/python
(base) [builder@3abd754f9aeb ~]$

【讨论】:

    【解决方案2】:

    查看~/.profile~/.bash_profile~/.bash_login

    来自我~/.profile的标题:

    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    # see /usr/share/doc/bash/examples/startup-files for examples.
    # the files are located in the bash-doc package.
    

    如果不存在,它将在~/.bashrc

    如果您只是想暂时这样做,则必须修改您的PATH。您可以使用

    查看当前路径
    echo $PATH
    

    【讨论】:

      【解决方案3】:

      文档第 7 步说要编辑您的点文件以配置路径:

      https://docs.anaconda.com/anaconda/install/mac-os/

      所以编辑您的~/.bashrc~/.bash_profile 以更改python 路径。

      【讨论】:

        【解决方案4】:

        我刚刚编写了一个小别名脚本,并将其发布在我的 .bashrc 中(隐藏在您的主目录中)。它使您可以轻松地在系统 python(在 /usr 中的某个位置)和不同的本地 python 之间切换。我使用别名而不是自定义命令,因此它可以轻松影响本地终端 PATH。

        我打算使用它,以免在开发过程中污染我的系统 python。

        alias quar='if [ "${PATH#/home/luke/bin/quarantine:}" == $PATH ]; then PATH="/home/luke/bin/quarantine:$PATH"; else echo "already quarantined"; fi'
        
        alias unquar='if [ "${PATH#/home/luke/bin/quarantine:}" != $PATH ]; then PATH=${PATH#/home/luke/bin/quarantine:}; else echo "already unquarantined"; fi'
        

        请注意,我指向自己的文件夹是“/home/luke/bin/quarantine”。 quar / unquar 的目的是将该文件夹切换到 PATH 的前面和外面。

        【讨论】:

          【解决方案5】:

          如果您为两个版本的 python 创建虚拟环境,那么无论您想使用哪个版本的 python,都非常容易使用。 Anaconda 很容易做到这一点。互联网上有说明,here for example,可以帮助您启动和运行环境。

          【讨论】:

            猜你喜欢
            • 2018-03-08
            • 1970-01-01
            • 1970-01-01
            • 2017-05-10
            • 1970-01-01
            • 1970-01-01
            • 2019-12-07
            • 2014-05-21
            • 2011-06-11
            相关资源
            最近更新 更多