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.

  • 相关阅读:
    SPOJ 4487. Can you answer these queries VI splay
    Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置改动及測试步骤
    欧拉函数
    安装Windows7步骤
    在Eclipse中执行、配置Hadoop
    java设计模式演示样例
    VC中获取窗体句柄的各种方法
    HTML5 Canvas中实现绘制一个像素宽的细线
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
  • 原文地址:https://www.cnblogs.com/kramer/p/2915391.html
Copyright © 2011-2022 走看看