简体   繁体   English

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

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

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. 顾名思义,我安装了Homebrew,然后安装了最新版本的Python。 I want to make this the default when running python --version in bash. 我想在bash中运行python --version时将其设置为默认值。

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 --version going straight to the default: 看来,当我安装新模块时,它们适用于2.7而不是3。检查pip --version直接进入默认值:

 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_profile again, but the line I wanted (and added manually) isn't the one that appears; 这确实会再次编辑我的bash_profile ,但是我想要的行(并手动添加)不是出现的那行; 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): 而是插入以下内容(我不知道为什么在最后插入Applications / 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. 谁能向我解释如何将python3设置为默认值。 I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. 我不想摆脱2.7,因为许多Mac脚本仍然依赖它。 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保留为默认值?

You just need to run command 您只需要运行命令

 $which python 

And then goto the path rename python to python2 Then run command 然后转到路径将python重命名为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

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年才能重新评估:

  • python3 runs Python 3.x python3运行Python 3.x
  • python2 runs Python 2.x python2运行Python 2.x
  • python usually shouldn't be used—but if you do use it, it should run Python 2.x. 通常不应该使用python但如果您确实使用python,则应运行python2.x。

Usually, you'll also have, eg, python3.7 and python2.7 to 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. pip脚本以及由pip安装的脚本,应使用类似的后缀安装,但是理想情况下,如果您有多个版本,则不应运行这些脚本。 Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x. 它们中的大多数被设计为作为模块运行,因此您可以运行python3 -m pip来运行3.x的pip

For a few things, it may be handy to set up shortcuts. 对于某些事情,设置快捷方式可能会很方便。 (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as 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,则可以创建一个Terminal。运行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通常希望您使用pip而不是brew安装Python库,因此您不必担心关于那个。

Programs should have an installer—whether they install via pip or 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. 程序应具有安装程序(无论是通过pip还是py2app类的程序进行安装),都可以创建适当的shbang行,因此它们可以与安装的Python版本一起运行,因此您不必担心它们。 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. 但是您的操作系统随附的程序可能会使用/usr/bin/env python ,并且他们希望它们能够运行您的操作系统随附的Python版本。

For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated. 对于开发目的,通常要使用virtualenvpipenvconda ,使python运行Python解释器无论哪个环境是当前激活。

So, don't try to change the fact that python runs Apple's Python 2.7. 因此,请勿尝试更改python运行Apple的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.; 使用python3python3 -m pip等; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); 在脚本上使用#!/usr/bin/env python3 (或者更好的是,使用setuptools自动创建入口点脚本); use virtual environments; 使用虚拟环境; just never run python , and everything will work properly. 只是永远不要运行python ,一切都会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM