Ubuntu18.04中安装Python3.7教程——遇到的问题以及解决方法

系统:Ubuntu18.04,安装Python版本Python3.7

step1:下载想要的Python版本,本次安装的版本为Python-3.7.0,下载网址:https://www.python.org/downloads/source/

step2:解压,放在指定的目录当中,本次的安装目录为 /usr/local/python,将Python-3.7.0放在上述的目录当中,可以用cp命令复制Python-3.7.0到该文件夹中sudo cp -r /usr/local/Python-3.7.0 /usr/local/python

其中:/usr/local/Python-3.7.0为Python的原文件夹目录,/usr/local/python为新文件夹目录

step3:进入目录cd /usr/local/python/Python-3.7.0

运行configure文件 sudo ./configure

[email protected]:/usr/local/python/Python-3.7.0$ sudo ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/python/Python-3.7.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

出现没有c编译器的错误,解决方法为安装gcc,具体方法如下:

1、运行以下命令才能找到gcc: sudo apt-get update

2、安装gcc: sudo apt-get install gcc

step4:此时运行Python可以看到系统安装了默认的Python版本:python

[email protected]:/usr/local/python/Python-3.7.0# python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

step5:安装我们想要的Python版本

运行configure文件: sudo ./configure

安装完成以后输入: python 显示现在版本为Python3.7

[email protected]:/usr/local/python/Python-3.7.0# python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

到这里为止,Python3.7已经安装完毕!接下来,我们测试基本的hello world程序~

Ubuntu中没有安装默认的Python集成开发工具,可以安装vim编写

sudo apt install vim-gtk
安装好vim以后可以选择在终端输入vim打开,或者在Ubuntu的软件列表中打开,

1
 

 

 

 

 

 

 

 

可以使用上图中的vim,也可以使用GVim,区别在于vim比较像终端界面,GVim是一个有界面的比较友好的编辑器。

写一个简单的hello.py 函数

1

 

 

 

 

 

 

 

 

接下来,运行hello.py文件,直接运行python hello.py会出现以下的错误:

[email protected]:~/文档/python$ python hello.py
  File "hello.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file hello.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

需要在开头加入                      # -*- coding: utf-8 -*

1

 

 

 

 

 

 

 

 

再次运行python hello.py 成功运行~

[email protected]:~/文档/python$ python hello.py
hello world

真心感觉安装Python其实挺费劲的,不过装完以后感觉还是挺好的,大家如果有问题可以留言,有错误也欢迎指出~