Linux和MacOsX下的python环境搭建
在Linux系统和MacOsX系统中,一般都默认安装了python。可以在终端中输入python或python3进行确认,如果已经安装会显示以下内容:
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Python 3.6.5 (default, May 15 2018, 22:51:22)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Windows系统下的python环境搭建
Windows系统大部分应该默认不安装python,同样可以在终端中输入python进行确认。如果系统中没有安装python可以在http://python.org/downloads/中下载对应的安装包进行安装。
运行python程序
环境搭建完成后,就能够进行程序测试了。通过在终端中输入python进入python解释器,输入python语句,例如来一个hello world:
>>> print('hello world!')
hello world!
也可以在文件中编写python代码,然后在终端中使用命令进行运行,例如:
**/*hello.py*/**
#!/usr/bin/python
#-*-coding:utf-8-*-
print('hello world!')
xxx:python xxx$ python hello.py
hello world!
#!/usr/bin/python的作用在于linux系统下在编写.py文件时,只要写上了这行注释,用户就可以直接在命令行用文件名来执行py文件。
#--coding:utf-8--的作用在于python2需要在首行加上这行注释才能支持中文,python3开始默认支持中文了,就可以省去这行注释。
当然,有很多编辑器或集成开发环境也支持运行python程序,例如Sublime Text、Spyder等等,可以根据习惯和偏好进行选择。