简体   繁体   English

将 linux 上的 python 2.7 更新到 3.5

[英]Update python on linux 2.7 to 3.5

So I updated python using these instructions:所以我使用以下说明更新了 python:

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-devlibsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

then然后

cd ~/Downloads 
wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
tar -xvf Python-3.5.0.tgz
cd Python-3.5.0

then然后

./configure
sudo make install



python3.5
Python 3.5.0 (default, Oct  3 2015, 03:16:42) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

and it worked but when I closed terminal, and wrote python -v it is still python 2.7 and still getting errors executing code that use v3+ standard libraries它起作用了,但是当我关闭终端并编写 python -v 时,它仍然是 python 2.7,并且在执行使用 v3+ 标准库的代码时仍然出错

How to make it working as 3.5?如何让它像 3.5 一样工作?

//install python 3.6 //安装python 3.6

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt update
sudo apt install python3.6

//change default python //修改默认python

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python

//view default //查看默认值

 python -V

You still have Python 2 installed, and the python command is still set up to invoke that version by default.您仍然安装了 Python 2,并且默认情况下仍将python命令设置为调用该版本。 Try running your scripts like this:尝试像这样运行你的脚本:

python3 yourscriptname.py

In /usr/bin/, "python" is actually a symlink to python2.7.在 /usr/bin/ 中,“python”实际上是 python2.7 的符号链接。 If you'd rather not have to type the 3 at the end whenever you use python, change that symlink to point to python3 instead.如果您不想在每次使用 python 时都在末尾键入 3,请将该符号链接更改为指向 python3。 Then python will run Python 3.5 and you can use python2.7 or just python2 to run Python 2.7 scripts.然后python将运行 Python 3.5,您可以使用python2.7或仅使用python2来运行 Python 2.7 脚本。

EDIT: Alternatively, you could put alias python=python3 in your ~/.bashrc file to do the same thing, but without needing root access and for your user account only.编辑:或者,您可以将alias python=python3放在您的 ~/.bashrc 文件中以执行相同的操作,但不需要 root 访问权限并且仅适用于您的用户帐户。

You might just have destroyed your system python installation by doing您可能只是通过执行以下操作破坏了您的系统 python 安装

sudo make install

This installs in the default path!这安装在默认路径中! And will possibly overwrite anything your linux distro has there to work correctly.并且可能会覆盖您的 linux 发行版可以正常工作的任何内容。 Never do sudo make installs if you're not sure where the resulting stuff goes.如果您不确定结果的去向,切勿执行 sudo make installs。 In case of Python, you should have done在 Python 的情况下,你应该已经完成

sudo make altinstall

to have it installed next to the default python but the best thing is to install and use the python version via your distro's package manager.将它安装在默认 python 旁边,但最好的是通过发行版的包管理器安装和使用 python 版本。

我读到将全局 python 更改为 3.5 会导致一些问题,所以我在 geany 中为 python3 设置了构建命令,我仍然可以通过键入 python3 xxx.py 在外部运行脚本,现在很舒服

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

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