简体   繁体   English

如何在homebrew中将python3符号链接更改为“python”

[英]In homebrew how do I change the python3 symlink to only “python”

I want to install python using homebrew and I noticed there are 2 different formulas for it, one for python 2.x and another for 3.x. 我想用自制软件安装python,我注意到有两个不同的公式,一个用于python 2.x,另一个用于3.x. The first symlinks "python" and the other uses "python3". 第一个符号链接“python”,另一个使用“python3”。 so I ran brew install python3 . 所以我运行了brew install python3

I really only care about using python 3 so I would like the default command to be "python" instead of having to type "python3" every time. 我真的只关心使用python 3所以我希望默认命令是“python”而不是每次都输入“python3”。 Is there a way to do this? 有没有办法做到这一点? I tried brew switch python 3.3 but I get a "python is not found in the Cellar" error. 我尝试了brew switch python 3.3但是我得到了“在地窖中找不到python”的错误。

You definitely do not want to do this! 你肯定希望这样做! You may only care about Python 3, but many people write code that expects python to symlink to Python 2. Changing this can seriously mess your system up. 您可能只关心Python 3,但很多人编写的代码需要python与Python 2进行符号链接。更改此内容会严重影响您的系统。

If you are absolutely sure that you will never want to install / use Python 2, I think you can just create additional symlinks in /usr/local/bin. 如果您完全确定永远不想安装/使用Python 2,我认为您可以在/ usr / local / bin中创建其他符号链接。 Check for everything that links to something in 检查链接到某些内容的所有内容

../Cellar/python3/3.3.0/

and create a link without the 3 at the end, like 并在最后创建一个没有3的链接,比如

python -> ../Cellar/python3/3.3.0/bin/python3

Think twice though, why give up the advantages of having two Pythons side-by-side? 尽管如此,为什么要放弃两个蟒蛇并排的优势呢? Maybe just use the homebrew Python as intended, and create your Python 3 environments with virtualenv. 也许只是按预期使用自制的Python,并使用virtualenv创建Python 3环境。

If you're doing this for personal use, don't change the symlink for python . 如果您这样做是为了个人使用,请不要更改python的符号链接。 Many of your system programs depend on python pointing to Python 2.6, and you'll break them if you change the symlink. 你的许多系统程序都依赖于指向Python 2.6的python ,如果更改了符号链接,你就会破坏它们。

Instead, pick a shorter name like py and write an alias for it in ~/.bashrc , like alias py=python3 . 相反,选择一个较短的名称,如py并在~/.bashrc为它写一个别名,如alias py=python3

For example, with testing: 例如,通过测试:

$ echo "alias py=python3" >> ~/.bashrc
$ bash
$ py
>>> 3+3
6

This will give you the convenience without effecting the system or other users. 这将为您提供方便,而不会影响系统或其他用户。

Yes, far better to use [virtual environments] ( https://docs.python.org/3/library/venv.html ) for python 3 than mess with the system default 是的,使用[虚拟环境]( https://docs.python.org/3/library/venv.html )进行python 3比使用系统默认值更好

pyvenv /path/to/new/virtual/environment

which will setup python 3 as the default python and also isolate pip installs to that environment which is what you want to do on any project. 这将把python 3设置为默认的python并将pip安装隔离到你想在任何项目上做的环境。

As mentioned this is not the best idea. 如上所述,这不是最好的主意。 However, the simplest thing to do when necessary is run python3 in terminal. 但是,必要时最简单的方法是在终端中运行python3。 If you need to run something for python3 then run python3 如果你需要为python3运行一些东西,那么运行python3

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

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