bash 安装了 Python 3.7 并尝试将其更改为默认值,但是更改 $PATH 并运行 python --version 仍然显示 2.7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51570306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:59:46  来源:igfitidea点击:

Installed Python 3.7 and tried changing that to the default however, changing $PATH and running python --version still shows 2.7

pythonbashmacos

提问by Ashadyguy

Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.

对不起,如果问题很简单,或者我遗漏了一些明显的东西,但我对控制台命令和 Python 还很陌生。

As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --versionin bash.

正如标题所示,我安装了 Homebrew,随后安装了最新版本的 Python。我想python --version在 bash 中运行时将其设为默认值。

I tried changing the path in my bash_profile with the following line at the bottom:

我尝试使用底部的以下行更改我的 bash_profile 中的路径:

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --versiongoing straight to the default:

然而,似乎当我安装新模块时,它们是用于 2.7 而不是 3。检查pip --version直接进入默认值:

pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)

I also tried running the following commands:

我还尝试运行以下命令:

echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source  ~/.bash_profile

this does edit my bash_profileagain, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):

这确实bash_profile再次编辑了我的,但我想要的行(并手动添加)不是出现的行;而是插入了以下内容(我不知道为什么最后插入应用程序/VMware,它与 Python 或我正在尝试做的事情无关):

export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware

Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?

谁能向我解释如何将 python3 设为默认值。我显然不想摆脱 2.7,因为 mac 的很多脚本仍然依赖于它。是否可以安装 pipenv 并从那里运行 3.7,同时保持 2.7 的默认值?

采纳答案by abarnert

You really don't want to change this.

你真的不想改变这一点。

First:

第一的:

Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?

是否可以安装 pipenv 并从那里运行 3.7,同时保持 2.7 的默认值?

Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.

是的!安装pipenv,并设置环境默认为 3.7,但保留系统默认为 2.7。



More generally:

更普遍:

According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:

根据PEP 394(直到 2020 年才会重新评估),如果您没有激活虚拟环境:

  • python3runs Python 3.x
  • python2runs Python 2.x
  • pythonusually shouldn't be used—but if you do use it, it should run Python 2.x.
  • python3运行 Python 3.x
  • python2运行 Python 2.x
  • python通常不应该使用——但如果你使用它,它应该运行 Python 2.x。

Usually, you'll also have, e.g., python3.7and python2.7to run specific 3.x and 2.x versions, although this isn't covered by any standard.

通常情况下,你也有,例如,python3.7python2.7运行特定3.x和2.x的版本,虽然这不属于任何标准。

Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pipto run the pipfor your 3.x.

pip和由 安装的脚本pip应该使用类似的后缀安装——但理想情况下,如果您有多个版本,则不应运行这些脚本。它们中的大多数被设计为作为模块运行,因此您可以运行python3 -m pip以运行pip3.x。

For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipythonfrom the command line, I usually use an iTerm profile that uses that instead of bashas my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)

对于某些事情,设置快捷方式可能很方便。(例如,python3 -m ipython我通常使用 iTerm 配置文件而不是从命令行运行,而是使用该配置文件而不是bash作为我的启动命令。如果您不使用 IPython 或 iTerm,则可以创建一个运行python3. )

Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.

作为平台包管理器的一部分安装的脚本是一个更复杂的故事,但 macOS 没有官方包管理器,Homebrew 通常希望你安装 Python 库pip,而不是brew,所以你不必担心.

Programs should have an installer—whether they install via pipor something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.

程序应该有一个安装程序——无论是通过安装pip还是类似的方式安装py2app——它会创建一个合适的 shbang 行,这样它们就可以使用它们安装的任何 Python 版本运行,所以你不必担心它们。但是你的操作系统附带的程序可能会使用类似的东西/usr/bin/env python,他们会期望运行你的操作系统附带的 Python 版本。

For development purposes, you usually want to use virtualenvor pipenvor condaso that pythonruns the Python interpreter for whichever environment is currently activated.

出于开发目的,您通常希望使用virtualenvorpipenvcondaso 来python为当前激活的任何环境运行 Python 解释器。

So, don't try to change the fact that pythonruns Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3on your scripts (or, better, use setuptoolsto create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.

所以,不要试图改变python运行 Apple 的 Python 2.7的事实。这就是它应该做的,如果你不这样做,你可能会破坏事情。使用python3python3 -m pip等等; #!/usr/bin/env python3在您的脚本上使用(或者,更好地,用于setuptools自动创建入口点脚本);使用虚拟环境;永远不要运行python,一切都会正常工作。

回答by Snowy

You just need to run command

你只需要运行命令

 $which python 

And then goto the path rename python to python2 Then run command

然后转到路径rename python to python2 然后运行命令

$which python3

To get the path of python3 Just link with command

获取python3的路径只需链接命令

sudo ln -s $python3path /usr/bin

/python You need to instead $python3path with real path Then you can run python3 with python command

/python 你需要用真实路径代替 $python3path 然后你可以用 python 命令运行 python3