zoukankan      html  css  js  c++  java
  • (python learn) 1 python environment

    By default linux have python installed. There are two kind of enviroment in python. One is interactive enviroment. Like shell.  The other one is file enviroment. Like other programming languages, you put the code in a file and run it.

    The interactive environment

    input python you will get into the python interactive enviroment

    [root@racnode1 test]# python
    Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 8
    8
    >>> 1+1
    2
    >>> exit()
    

      

    The file evironment

    There are three kind of python files.

    source code  -- with extension name ".py".  You can run this file without comiple it. Because the python interpreter will interpret and run it.

    compiled code -- with extension name ".pyc". You can compile a source code to generate a ".pyc" file. The compile methold is like below

    import py_compile
    py_compile.compile("1.py")
    

    optimized code -- with extension name ".pyo".  This is the code being optimized by python. You can generate the ".pyo" file in this way. 

    pyton -O -m py_compile example.py 
    

      

    let`s create a source code and run it. to create a souce code python file you can use vi.

    #!/usr/bin/python
    print "hello world"

    without the line # !/usr/bin/python, you will have to run the file in below way. 

    [root@racnode1 test]# python test.py
    hello world
    

    with the line  # !/usr/bin/python, you can grant execution privilege to the file and run  

    [root@racnode1 test]# chmod u+x test.py
    [root@racnode1 test]# ./test.py
    hello world
    [root@racnode1 test]#
    

    once we have source code, we can generate a compiled file ".pyc".

    first you need to create another python file with below content.

    [root@racnode1 test]# cat compile.py
    import py_compile
    py_compile.compile("test.py")
    

    then run this file you will compile the "test.py"

    [root@racnode1 test]# python compile.py
    [root@racnode1 test]# ls
    compile.py  test.py  test.pyc
    [root@racnode1 test]# python test.pyc
    hello world
    

    then let`s see antother example for generating a ".pyo" file

    [root@racnode1 test]# ls
    compile.py  test.py  test.pyc  test.pyo
    [root@racnode1 test]# python -O -m py_compile test.py
    [root@racnode1 test]# python test.pyo
    hello world
    

    with the commind python -O -m py_compile  you will get an optimized py file.

  • 相关阅读:
    kafka参数解析+启动参数解析
    flume参数解析+启动参数解析
    flume+flume+kafka消息传递+storm消费
    C++快速读取大文件
    阿里云ecs : Couldn't connect to host, port: smtp.aliyun.com, 25; timeout -1;
    对渲染相关操作封装的类库
    51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
    51nod-1503 猪和回文
    51nod1073-约瑟夫环,递归。
    算法基础题
  • 原文地址:https://www.cnblogs.com/kramer/p/2915391.html
Copyright © 2011-2022 走看看