Python Learning note 12-module usage

Source: Internet
Author: User

Python Learning note 12-module usage

Module Os,sys


What is a module?

Module Os,sys

Modules are a basic way for Python to organize your code

A python script can be run on its own, or it can be imported into another script, imported with an import Hello statement, and not joined. PY


What is a python package?

Python modules can be organized into packages according to directory

Steps to create a package:

Create a directory with the name of the package name

Create a __init__.py file under Change directory

If necessary, place script files or compiled extensions and sub-packages in this directory

Import pack.m1,pack.m2,pack.m3


View Python

Sys.path

Export Pythonpat

in [311]: import sysin [312]: sys.pathout[312]: [',  '/usr/local/bin ',   '/usr/local/lib/python27.zip ',  '/usr/local/lib/python2.7 ',  '/usr/local/lib/python2.7/ Plat-linux2 ',  '/usr/local/lib/python2.7/lib-tk ',  '/usr/local/lib/python2.7/lib-old ',  '/usr/ Local/lib/python2.7/lib-dynload ',  '/usr/local/lib/python2.7/site-packages ',  '/usr/local/lib/ Python2.7/site-packages/ipython/extensions ',  '/root/.ipython ']in [313]: import stringin  [314]: import  T---------------------------------------------------------------------------importerror                                 Traceback  (most recent call  Last) <ipython-input-314-c2d7a4e4fb09> in <module> ()----> 1 import  timporterror: no module named t# cannot find this module [[email protected] ~]# ls /usr/local/lib/python2.7/ string.py/usr/local/lib/python2.7/string.py[[email protected] ~]# ls /usr/local/lib/ python2.7/os.py/usr/local/lib/python2.7/os.py



Customize the path of Python, some of the tools you have developed can be placed in this directory

Method One: Through Sys.path.append ('/root/library ')

[Email protected] ~]# Mkdir/root/libraryin [315]: Sys.path.append ('/root/library ') in [[]: sys.pathout[316]: [', '/ Usr/local/bin ', '/usr/local/lib/python27.zip ', '/usr/local/lib/python2.7 ', '/usr/local/lib/python2.7/plat-linux2 ' , '/usr/local/lib/python2.7/lib-tk ', '/usr/local/lib/python2.7/lib-old ', '/usr/local/lib/python2.7/lib-dynload ', ' /usr/local/lib/python2.7/site-packages ', '/usr/local/lib/python2.7/site-packages/ipython/extensions ', '/root/. Ipython ', '/root/library ']


Method Two, modify the/ROOT/.BASHRC

[Email protected] ~]# VIM/ROOT/.BASHRC

Add a line: Export pythonpath=/root/library

[Email protected] ~]#. /ROOT/.BASHRC [[email protected] ~]# echo $PYTHONPATH/root/library[[email protected] ~]# Ipythonin [2]: Import Sysin [3]: SYS.PATHOUT[3]: [' ', '/usr/local/bin ', '/root/library ', '/usr/local/lib/python27.zip ', '/usr/local/lib/python2.7 ', ' /usr/local/lib/python2.7/plat-linux2 ', '/usr/local/lib/python2.7/lib-tk ', '/usr/local/lib/python2.7/lib-old ', '/ Usr/local/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/site-packages ', '/usr/local/lib/python2.7/ Site-packages/ipython/extensions ',


Instance:

In [2]: with open ('/etc/hosts ')  as fd:   ...:      s = fd.read () in [4]: s.count (' \ n ')   #统计行数, using \ n line break as the standard for segmentation out[4]: 19 [[ Email protected] ~]# vim wordcount.py #!/usr/bin/pythondef wordcount (s):     chars = len (s)            #统计字符数     words = len (S.split ())    #统计单词数量,     lines  = s.count (' \ n ')     #统计行数, the number of occurrences is the standard, cannot use Lines=len (s.splid (' \ n ')), so that the final end of \ n will count a row of None     print lines,words,charss = open ('/etc/passwd '). Read () wordCount (s) [[ Email protected] ~]# python wordcount.py 42 71 2051[[email protected]  ~]# wc /etc/passwd42   71 2051 /etc/passwd[[email protected]  ~]# vim wordcount.py [[email protected] ~]# vim test.py   #默认是找的当前目录下的 #!/usr/bin/ pythonimport wordcount[[email protected] ~]# python test.py 42 71 2051 [[email protected] ~]# ll  # When wordcount.py is used as a module, it generates a compiled file Wordcount.pyc, and when referenced again, the higher the overall dosage  36drwxr-xr-x  2 root root  4096 9 Month    8 04:30 library-rw-r--r--  1 root root    38 9 Month    8 04:59 test.py-rw-r--r--  1 root root   182 9 Month    8 04:54 wordcount.py-rw-r--r--  1 root  Root  424 9 Month &NBSP;&NBSP;&NBSP;8&NBSP;05:00&NBSP;WORDCOUNT.PYC


This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1847468

Python Learning note 12-module usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.