问题:如何更改默认的python版本?
我已经在Mac中安装了python 3.2。运行/ Applications / Python 3.2 / Update Shell Profile.command之后,令人困惑的是,当我在Terminal中键入python -V时,它说Python 2.6.1,如何更改默认的python版本?
I have installed python 3.2 in my mac. After I run /Applications/Python 3.2/Update Shell Profile.command, it’s confusing that when I type python -V in Terminal it says that Python 2.6.1, how can I change the default python version?
回答 0
这可能是向后兼容所希望的。
Python3破坏了向后兼容性,并且调用“ python”的程序可能希望使用python2。您可能有很多程序和脚本,甚至都不知道哪个期望使用python = python2,更改此设置将破坏这些程序和脚本。
您可能正在寻找的答案是:您不应该更改此设置。
但是,您可以在shell中创建自定义别名。您这样做的方式取决于外壳,但也许您可以这样做alias py=python3
如果您对如何启动最新版本的python感到困惑,则至少在Linux上可以python3
使python2
安装保持完整(由于上述兼容性原因);因此,您可以使用以下python3
命令启动python3 。
This is probably desirable for backwards compatibility.
Python3 breaks backwards compatibility, and programs invoking ‘python’ probably expect python2. You probably have many programs and scripts which you are not even aware of which expect python=python2, and changing this would break those programs and scripts.
The answer you are probably looking for is You should not change this.
You could, however, make a custom alias in your shell. The way you do so depends on the shell, but perhaps you could do alias py=python3
If you are confused about how to start the latest version of python, it is at least the case on Linux that python3
leaves your python2
installation intact (due to the above compatibility reasons); thus you can start python3 with the python3
command.
回答 1
检查python 3的位置
$ which python3
/usr/local/bin/python3
在bash_profile中写别名
vi ~/.bash_profile
alias python='/usr/local/bin/python3'
重新加载bash_profile
source ~/.bash_profile
确认python命令
$ python --version
Python 3.6.5
Check the location of python 3
$ which python3
/usr/local/bin/python3
Write alias in bash_profile
vi ~/.bash_profile
alias python='/usr/local/bin/python3'
Reload bash_profile
source ~/.bash_profile
Confirm python command
$ python --version
Python 3.6.5
回答 2
在Mac OS X上,如您所见,使用python.org安装程序,您需要使用python3
,而不是调用Python 3 python
。目前仅保留用于Python 2版本。您也可以python3.2
用来专门调用该版本。
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 2to3@ -> 2to3-3.2
-rwxrwxr-x 1 root admin 140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x 1 root admin 7 Apr 28 15:51 idle3@ -> idle3.2
-rwxrwxr-x 1 root admin 138 Feb 20 11:14 idle3.2*
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 pydoc3@ -> pydoc3.2
-rwxrwxr-x 1 root admin 123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3*
lrwxr-xr-x 1 root admin 12 Apr 28 15:51 python3-32@ -> python3.2-32
lrwxr-xr-x 1 root admin 16 Apr 28 15:51 python3-config@ -> python3.2-config
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x 1 root admin 17 Apr 28 15:51 python3.2-config@ -> python3.2m-config
-rwxrwxr-x 1 root admin 25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x 1 root admin 1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x 1 root admin 10 Apr 28 15:51 pythonw3@ -> pythonw3.2
lrwxr-xr-x 1 root admin 13 Apr 28 15:51 pythonw3-32@ -> pythonw3.2-32
-rwxrwxr-x 1 root admin 25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 pythonw3.2-32*
如果您还从python.org安装了Python 2,它将有一个类似的框架bin目录,没有重叠的文件名(2to3除外)。
$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x 1 root admin 150 Jul 3 2010 2to3*
lrwxr-x--- 1 root admin 7 Nov 8 23:14 idle@ -> idle2.7
-rwxrwxr-x 1 root admin 138 Jul 3 2010 idle2.7*
lrwxr-x--- 1 root admin 8 Nov 8 23:14 pydoc@ -> pydoc2.7
-rwxrwxr-x 1 root admin 123 Jul 3 2010 pydoc2.7*
lrwxr-x--- 1 root admin 9 Nov 8 23:14 python@ -> python2.7
lrwxr-x--- 1 root admin 16 Nov 8 23:14 python-config@ -> python2.7-config
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 python2.7*
-rwxrwxr-x 1 root admin 1663 Jul 3 2010 python2.7-config*
lrwxr-x--- 1 root admin 10 Nov 8 23:14 pythonw@ -> pythonw2.7
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 pythonw2.7*
lrwxr-x--- 1 root admin 11 Nov 8 23:14 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x 1 root admin 18272 Jul 3 2010 smtpd2.7.py*
On Mac OS X using the python.org installer as you apparently have, you need to invoke Python 3 with python3
, not python
. That is currently reserved for Python 2 versions. You could also use python3.2
to specifically invoke that version.
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 2to3@ -> 2to3-3.2
-rwxrwxr-x 1 root admin 140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x 1 root admin 7 Apr 28 15:51 idle3@ -> idle3.2
-rwxrwxr-x 1 root admin 138 Feb 20 11:14 idle3.2*
lrwxr-xr-x 1 root admin 8 Apr 28 15:51 pydoc3@ -> pydoc3.2
-rwxrwxr-x 1 root admin 123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3*
lrwxr-xr-x 1 root admin 12 Apr 28 15:51 python3-32@ -> python3.2-32
lrwxr-xr-x 1 root admin 16 Apr 28 15:51 python3-config@ -> python3.2-config
-rwxrwxr-x 2 root admin 25624 Feb 20 11:14 python3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x 1 root admin 17 Apr 28 15:51 python3.2-config@ -> python3.2m-config
-rwxrwxr-x 1 root admin 25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x 1 root admin 1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x 1 root admin 10 Apr 28 15:51 pythonw3@ -> pythonw3.2
lrwxr-xr-x 1 root admin 13 Apr 28 15:51 pythonw3-32@ -> pythonw3.2-32
-rwxrwxr-x 1 root admin 25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x 1 root admin 13964 Feb 20 11:14 pythonw3.2-32*
If you also installed a Python 2 from python.org, it would have a similar framework bin directory with no overlapping file names (except for 2to3).
$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x 1 root admin 150 Jul 3 2010 2to3*
lrwxr-x--- 1 root admin 7 Nov 8 23:14 idle@ -> idle2.7
-rwxrwxr-x 1 root admin 138 Jul 3 2010 idle2.7*
lrwxr-x--- 1 root admin 8 Nov 8 23:14 pydoc@ -> pydoc2.7
-rwxrwxr-x 1 root admin 123 Jul 3 2010 pydoc2.7*
lrwxr-x--- 1 root admin 9 Nov 8 23:14 python@ -> python2.7
lrwxr-x--- 1 root admin 16 Nov 8 23:14 python-config@ -> python2.7-config
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 python2.7*
-rwxrwxr-x 1 root admin 1663 Jul 3 2010 python2.7-config*
lrwxr-x--- 1 root admin 10 Nov 8 23:14 pythonw@ -> pythonw2.7
-rwxrwxr-x 1 root admin 33764 Jul 3 2010 pythonw2.7*
lrwxr-x--- 1 root admin 11 Nov 8 23:14 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x 1 root admin 18272 Jul 3 2010 smtpd2.7.py*
回答 3
老问题了,但是或者:
virtualenv --python=python3.5 .venv
source .venv/bin/activate
Old question, but alternatively:
virtualenv --python=python3.5 .venv
source .venv/bin/activate
回答 4
做正确的事,做正确的事!
—>零打开您的终端,
-首先输入python -V
,它可能显示:
Python 2.7.10
-其次输入python3 -V
,它可能显示:
Python 3.7.2
-三次输入where python
或which python
,它可能显示:
/usr/bin/python
—第四个输入where python3
或which python3
,它可能显示:
/usr/local/bin/python3
-第五,~/.profile file or ~/.bash_profile
在Bash或~/.zshrc
zsh 下的PATH环境变量文件的底部添加以下行。
alias python='/usr/local/bin/python3'
要么
alias python=python3
– source ~/.bash_profile
在Bash或source ~/.zshrc
zsh 下第六次输入。
-第七步退出终端。
—第八次打开终端,输入python -V
,可能显示:
Python 3.7.2
我已经成功地尝试过了。
其他的,~/.bash_profile
不是zsh下的那个~/.bash_profile
。
zsh下的PATH环境变量~/.profile
(或~/.bash_file
通过)~/.zshrc
。
帮助你们!
Do right thing, do thing right!
—>Zero Open your terminal,
–Firstly input python -V
, It likely shows:
Python 2.7.10
-Secondly input python3 -V
, It likely shows:
Python 3.7.2
–Thirdly input where python
or which python
, It likely shows:
/usr/bin/python
—Fourthly input where python3
or which python3
, It likely shows:
/usr/local/bin/python3
–Fifthly add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile
under Bash or ~/.zshrc
under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
-Sixthly input source ~/.bash_profile
under Bash or source ~/.zshrc
under zsh.
–Seventhly Quit the terminal.
—Eighthly Open your terminal, and input python -V
, It likely shows:
Python 3.7.2
I had done successfully try it.
Others, the ~/.bash_profile
under zsh is not that ~/.bash_profile
.
The PATH environment variable under zsh instead ~/.profile
(or ~/.bash_file
) via ~/.zshrc
.
Help you guys!
回答 5
通过将“默认” Python放在您路径上的系统Python之前来进行更改,例如:
export PATH=/usr/local/bin:$PATH
Change the “default” Python by putting it ahead of the system Python on your path, for instance:
export PATH=/usr/local/bin:$PATH
回答 6
根据Google的快速搜索,此更新仅适用于您打开的当前Shell。可以通过键入python3来修复它,因为mac和linux足够相似,可以使这种情况重合。链接到Google搜索的结果。
而且,正如ninjagecko所说,大多数程序尚未更新到3.x,因此将默认python设置为3.x会破坏应用程序中使用的许多python脚本。
According to a quick google search, this update only applies to the current shell you have open. It can probably be fixed by typing python3, as mac and linux are similar enough for things like this to coincide. Link to the result of google search.
Also, as ninjagecko stated, most programs have not been updated to 3.x yet, so having the default python as 3.x would break many python scripts used in applications.
回答 7
我正在使用OS X 10.7.5和Python 3.4.2。如果键入python3
并要运行什么,它将使用python 3运行它。例如
pyhton3 test001.py
。那运行了我制作的名为test001的测试程序。我希望这有帮助。
I am using OS X 10.7.5 and Python 3.4.2. If you type python3
and what you want to run it will run it using python 3. For example
pyhton3 test001.py
. That ran a test program I made called test001. I hope this helps.
回答 8
导航:
我的电脑->属性->高级->环境变量->系统变量
假设您已经在路径变量中添加了python 2.7,并且您想将默认路径更改为python 3.x
然后在python2.7路径之前添加python3.5.x文件夹的路径。
打开cmd:键入“ python –version”
python版本将更改为python 3.5.x
Navigate to:
My Computer -> Properties -> Advanced -> Environment Variables -> System Variables
Suppose you had already having python 2.7 added in path variable and you want to change default path to python 3.x
then add path of python3.5.x folder before python2.7 path.
open cmd: type “python –version”
python version will be changed to python 3.5.x
回答 9
sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python
这将破坏脚本,但这正是更改python的方法。您还应该重写脚本以不假定python
为2.x。无论您打电话system
到哪个地方,它都将起作用exec
。
sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python
This will break scripts, but is exactly the way to change python. You should also rewrite the scripts to not assume python
is 2.x. This will work regardless of the place where you call system
or exec
.
回答 10
简而言之:更改环境变量中的路径!
对于Windows:
In short: change the path in Environment Variables!
For Windows:
Advanced System Settings > Advance (tab)
. On bottom you’ll find 'Environment Variables'
Double-click on the Path
. You’ll see path to one of the python installations, change that to path of your desired version.
回答 11
检查python3具有库的执行路径
$ which python3
/usr/local/bin/python3 some OS might have /usr/bin/python3
打开bash_profile文件并添加别名
vi ~/.bash_profile
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'
重新加载bash_profile以使修改生效
source ~/.bash_profile
运行python命令并检查是否正在使用python3加载
$ python --version
Python 3.6.5
Check the execution path of python3 where it has libraries
$ which python3
/usr/local/bin/python3 some OS might have /usr/bin/python3
open bash_profile file and add an alias
vi ~/.bash_profile
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'
Reload bash_profile to take effect of modifications
source ~/.bash_profile
Run python command and check whether it’s getting loading with python3
$ python --version
Python 3.6.5