IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]《Python入门》Linux 下 Python Web开发环境搭建笔记

    testcs_dn发表于 2016-04-26 20:28:26
    love 0

    之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境。

    我使用的系统是:ubuntu 14.04 server,根据个人经验,CentOS 6.5 下也适用。

    关于Python的版本

    进入Python的网站,鼠标移到导航条上的下载,我们会发现提供两下主版本的下载链接!


    这两个之间存在什么差别呢?

    个人理解,2.7.x的版本为更加稳定的版本,而3.x的版本则是比较前卫的版本,包含了很多新功能新特性之类的;

    但如果想要用Python开发一个新项目,那么该如何选择Python版本呢?大部分Python库都同时支持Python 2.7.x和3.x版本的,所以不论选择哪个版本都是可以的。

    但有一些需要注意的区别,参考:Python 2.7.x 和 3.x 版本的重要区别小结

    下载Python

    由于Bluemix中如果未指定版本,缺省情况下会选择 V2.7.10,所以我决定下载安装 V2.7.10。

    我的操作系统是ubuntu 14.04 server,Linux系统通常都是通过源码进行安装;

    可以直接使用以下命令下载:

    wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz

    点击下图进入Python下载页面,找到下载地址;


    选择你想使用的版本:


    XZ格式的压缩率通常是最高的,所以我这里选择XZ格式的包;



    安装Python

    解压刚刚下载的安装包

    # xz -d Python-2.7.11.tar.xz
    # tar -xvf Python-2.7.11.tar
    进入文件目录:

    # cd Python-2.7.11
    配置

    # ./configure
    安装

    # make
    这里只需要执行 make 就可以了,  不需要执行 makeinstall。

    注意,系统中可能还没有安装 make,会出现如下提示:

    # make
    The program 'make' is currently not installed. You can install it by typing:
    apt-get install make
    然后我们可以根据提示,执行以下命令安装:

    # apt-get install make
    输出如下:

    # apt-get install make
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Suggested packages:
      make-doc
    The following NEW packages will be installed:
      make
    0 upgraded, 1 newly installed, 0 to remove and 48 not upgraded.
    Need to get 119 kB of archives.
    After this operation, 328 kB of additional disk space will be used.
    Get:1 http://hk.archive.ubuntu.com/ubuntu/ trusty/main make amd64 3.81-8.2ubuntu3 [119 kB]
    Fetched 119 kB in 7s (16.0 kB/s)
    Selecting previously unselected package make.
    (Reading database ... 80371 files and directories currently installed.)
    Preparing to unpack .../make_3.81-8.2ubuntu3_amd64.deb ...
    Unpacking make (3.81-8.2ubuntu3) ...
    Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
    Setting up make (3.81-8.2ubuntu3) ...
    安装成功之后再执行 make 命令,这个过程可能有点慢,需要等一会儿。

    以下为最后部分输出:

    Python build finished, but the necessary bits to build these modules were not found:
    _bsddb             _curses            _curses_panel   
    _sqlite3           _ssl               _tkinter        
    bsddb185           bz2                dbm             
    dl                 gdbm               imageop         
    readline           sunaudiodev        zlib            
    To find the necessary bits, look in setup.py in detect_modules() for the module's name.
    
    running build_scripts
    creating build/scripts-2.7
    copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/pydoc -> build/scripts-2.7
    copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/idle -> build/scripts-2.7
    copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/2to3 -> build/scripts-2.7
    copying and adjusting /home/aven/Python-2.7.11/Lib/smtpd.py -> build/scripts-2.7
    changing mode of build/scripts-2.7/pydoc from 644 to 755
    changing mode of build/scripts-2.7/idle from 644 to 755
    changing mode of build/scripts-2.7/2to3 from 644 to 755
    changing mode of build/scripts-2.7/smtpd.py from 644 to 755
    /usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py

    到这里就安装完成了,输入“Python”,然后回车看看吧!


    如何退出 Python 命令行? Ctrl + Z 就可以了。

    关于Pip

    pip是一个安装和管理Python包的工具,是easy_install的一个替换品。

    看到网上一些文章介绍Pip还需要单独安装,已经过时了,经过上面的步骤,Pip已经被安装好了;

    关于Python IDE

    什么是IDE?
    IDE= 集成开发环境= 把开发相关的各种环境(和工具)都集成到一起

    Python IDE= Python的集成开发环境= 把和Python开发相关的各种工具

    有几种可以选择:

    • PyDev
    • Komodo Edit
    • PyCharm
    • Atom with Python plugin

    Hello World

    Python要求严格的缩进:

    [python] view plain copy
    1. #!/usr/bin/python  
    2. # -*- coding: UTF-8 -*-  
    3.   
    4. print 'hello world'  
    5.   
    6. for i in range(1,5):  
    7.     for j in range(1,5):  
    8.         for k in range(1,5):  
    9.             if( i != k ) and (i != j) and (j != k):  
    10.                 print 'hello world', i,j,k  


    保存文件,执行查看结果:


    小结

    到这里Python Web的开发环境就搭建完了,非常的简单!

    有的小伙伴可能怒了,我还没看到Web的影子呢!

    哈哈,我也是刚刚发现,Python和Go语言一样,Web服务器可以自己写;


    下一篇:《Python入门》第一个Python Web程序——简单的Web服务器



沪ICP备19023445号-2号
友情链接