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.

  • 相关阅读:
    我的屌丝giser成长记-研二篇
    我的屌丝giser成长记-研一篇(下)
    C#连接Oracle数据库的方法(Oracle.DataAccess.Client也叫ODP.net)
    C# 日期格式化的中的(/)正斜杠的问题(与操作系统设置有关)
    C#,SOAP1.1与1.2的发布与禁用(SOAP 1.2 in .NET Framework 2.0)
    C#使用WebService 常见问题处理
    sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。
    eclipse中的XML文件无法快捷键注释问题
    对比两个表中,字段名不一样的SQL
    oracle 恢复备份
  • 原文地址:https://www.cnblogs.com/kramer/p/2915391.html
Copyright © 2011-2022 走看看