Detailed Python custom modules

Source: Internet
Author: User
Python can add a custom module

Method One:

echo ' Export pythonpath= '/root/pythondiy/' >>/ROOT/.BASHRC # This directory is the path to your module # then use the SYS module to view the environment variables import Syssys.path          # return a list of [' ', '/usr/local/bin ',/root/pythondiy ', '/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/setuptools-28.8.0-py2.7.egg ', '/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg ', '/usr/ Local/lib/python2.7/site-packages/ipython/extensions ', '/root/.ipython ']

Method Two:

Sys.path.append ('/root/pythondiy ') # This method is temporarily valid

2. Open opening file differences

Open ('/etc/passwd '). Read ()                        # Returns the full text of the Stropen ('/etc/passwd '). ReadLines ()                   # Returns the full text is a listopen ('/etc/passwd '). ReadLine ()                    # reads a row each time and returns STR

3. Custom module Calls

Vim Wc.py#!/usr/bin/python from sys import argv def WC (s):    chars = Len (s)    words = Len (S.split ())    lines = S.cou NT (' \ n ')    return  lines,words,chars if __name__== ' __main__ ':                      # The function is called only when the script is executed. With    Open (Argv[1]) as file1:        print WC (File1.read ())         vim Copy_wc.pyimport

4. Implementation results

[Root@peng pyth]# python wc.py  /etc/passwd    # returns passwd file statistics (+, 1066) [Root@peng pyth]# python copy_wc.py< c14/># returns the Hosts file statistics (2, 10, 158)

5. Import the module package

# need to create a __init__.py empty file in the package, or it can be a description of the package Touch __init__.pyipythonfrom pyth Import WC # This will import a module package 

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.