1分钟,让虚拟机跑起Python

转载来源:https://blog.csdn.net/weixin_42313749/article/details/117262264?spm=1001.2014.3001.5501

Python 入门第一天

在这里插入图片描述


Python 简介

Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。

Python 的设计具有很强的可读性,语法简洁明晰
python具有丰富和强大的库,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。

Python 由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。

  • Python 的发展历史

    Python 是由 Guido van Rossum 在八十年代末和九十年代初,在荷兰国家数学和计算机科学研究所设计出来的。
    

Python 本身也是由诸多其他语言发展而来的,这包括 ABC、Modula-3、C、C++、Algol-68、SmallTalk、Unix shell 和其他的脚本语言等等。

像 Perl 语言一样,Python 源代码同样遵循 GPL(GNU General Public License)协议。

现在 Python 是由一个核心开发团队在维护,Guido van Rossum 仍然占据着至关重要的作用,指导其进展。

Python 2.7 被确定为最后一个 Python 2.x 版本,它除了支持 Python 2.x 语法外,还支持部分 Python 3.1 语法。

    在这里插入图片描述

    • Python 的特点

      优点:
      	1、简单易学。很容易上手
      	2、免费开源。 Python 是 FLOSS(自由/开放源码软件)之一.软件可以改动
      	3、高层语言。开发时,不许考虑底层细节
      	4、可移植。可以跨平台
      	5、可扩展。某些算法不公开。你可以编写,调用
      缺点:
      	1、速度慢
      	2、代码不能加密
      	3、线程不能利用多CPU问题。GIL(全局解释器锁)
      
        
        
      • Python 环境搭建

        [root@localhost ~]# rpm -ivh http://mirror.centos.org/centos/7/os/x86_64/Packages/wget-1.14-18.el7.x86_64.rpm
        [root@localhost ~]# cd /etc/yum.repos.d/
        [root@localhost yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
        [root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
        [root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
        [root@localhost src]# yum clean all
        [root@localhost ~]# yum -y install epel-release
        
         
         
          • 下载Python程序源码包
          [root@localhost ~]# cd /usr/src
          [root@localhost ~]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
          [root@localhost src]# ls
          debug  kernels  Python-3.7.3.tar.xz
          
           
           
            • 安装编译器与依赖包

              [root@localhost src]# yum -y install gcc gcc-c++ make zlib-devel libffi-devel openssl openssl-devel bzip2 bzip2-devel vim readline-devel ncurses-devel sqlite-devel gdbm-devel xz-devel tk-devel
              
                 
                 
              • 编译安装

                [root@localhost src]# tar xf Python-3.7.3.tar.xz
                [root@localhost src]# cd Python-3.7.3
                [root@localhost Python-3.7.3]# ls
                aclocal.m4           configure     Include     m4               Modules  PCbuild        README.rst
                CODE_OF_CONDUCT.rst  configure.ac  install-sh  Mac              Objects  Programs       setup.py
                config.guess         Doc           Lib         Makefile.pre.in  Parser   pyconfig.h.in  Tools
                config.sub           Grammar       LICENSE     Misc             PC       Python
                [root@localhost Python-3.7.3]# ./configure --prefix=/usr/local/python37 --enable-optimization
                [root@localhost Python-3.7.3]# make -j2
                [root@localhost Python-3.7.3]# make install -j2
                
                   
                   
                • 安装后配置

                  [root@localhost ~]# ls /usr/local/python37/
                  bin  include  lib  share
                  [root@localhost ~]# echo 'export PATH=/usr/local/python37/bin:$PATH' > /etc/profile.d/py3.sh
                  [root@localhost ~]# source /etc/profile.d/py3.sh
                  [root@localhost ~]# echo $PATH
                  /usr/local/python37/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
                  [root@localhost ~]# which python3
                  /usr/local/python37/bin/python3
                  [root@localhost ~]# pip3.7 install --upgrade pip
                  
                     
                     
                  • 测试

                    [root@localhost ~]# python3.7 
                    Python 3.7.3 (default, May 25 2021, 17:59:49) 
                    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
                    Type "help", "copyright", "credits" or "license" for more information.
                    >>> 1+2
                    3
                    >>> exit()
                    [root@localhost ~]# 
                    
                       
                       

                    在这里插入图片描述

                    Python 解释器

                    • 工作原理

                    ​ Python程序在第一次执行时,自动通过Compiler模块将源代码编译成.pyc后缀的bytecode(字节码)文件,之 后由python解释器(PVM,Interpreter)读取bytecode文件然后在处理器(processor)中运行。

                    • 代码执行方式:

                      可以通过两种方式来执行Python代码:
                      1. 交互式解释器
                      2. Python程序文件
                      
                         
                         

                      第一个Python 程序

                      • hello,world

                        [root@localhost ~]# vim hello.py
                        [root@localhost ~]# touch hello.py
                        [root@localhost ~]# cat hello.py 
                        #!/usr/bin/env python3.7
                        print("Hello, World!")
                        [root@localhost ~]# chmod o+x hello.py   #设定执行权限
                        [root@localhost ~]# ./hello.py          
                        Hello, World!
                        [root@localhost ~]# python3.7 hello.py   
                        Hello, World!
                        [root@localhost ~]# 
                        
                           
                           

                        总结

                        • 关于解释器

                          #!/usr/bin/python 是告诉操作系统执行这个脚本的时候,调用 /usr/bin 下的 python 解释器。
                          

                        #!/usr/bin/env python 这种用法是为了防止操作系统用户没有将 python 装在默认的 /usr/bin 路径里。当系统看到这一行的时候,首先会到 env 设置里查找 python 的安装路径,再调用对应路径下的解释器程序完成操作。

                        #!/usr/bin/python 相当于写死了 python 路径。
                        #!/usr/bin/env python 会去环境设置寻找 python 目录,可以增强代码的可移植性,推荐这种写法。

                          在这里插入图片描述

                        • 4
                          点赞
                        • 3
                          收藏
                          觉得还不错? 一键收藏
                        • 0
                          评论

                        “相关推荐”对你有帮助么?

                        • 非常没帮助
                        • 没帮助
                        • 一般
                        • 有帮助
                        • 非常有帮助
                        提交
                        评论
                        添加红包

                        请填写红包祝福语或标题

                        红包个数最小为10个

                        红包金额最低5元

                        当前余额3.43前往充值 >
                        需支付:10.00
                        成就一亿技术人!
                        领取后你会自动成为博主和红包主的粉丝 规则
                        hope_wisdom
                        发出的红包
                        实付
                        使用余额支付
                        点击重新获取
                        扫码支付
                        钱包余额 0

                        抵扣说明:

                        1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
                        2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

                        余额充值