Use of the python-module

Source: Internet
Author: User
Tags python script

Basic Concepts-modules are the basic way for Python to organize your code-A Python script can be run separately or imported into another script, and when the script is imported run, we call it module-All. py files can be imported as a module  features-The module name is the same as the file name of the scriptFor example, if you write a hello.py script, you can import it with an import Hello statement in another script-Python modules can be organized into packages by directoryto create a 1 package:-Create a directory with the name of the package name-Create a __init__.py file in this directory-store script files or compiled extensions and sub-packages in this directory, as needed-Import pack.m1,pack.m2,pack.m3 Extended-the location of the Python built-in module is stored in the (Sys.path environment variable, similar to the path in the shell)The main modules are installed under/usr/lib64/python 2.x/(Python-libs package installed)-Customize the Python module path1, Import sys package, use append to add pathsys.path.append ('/tmp ')2. Modify System VariablesVim ~/.BASHRCExport Pythonpath=/tmp Examplewrite a script like the WC command
# !/usr/bin/env python def     = = = Len (content.split (' \ n ')) print  = open ('/etc/passwd'). Read () printinfo (string)
compared with the WC command to find a row, the reason is that we count the line with \ n as a delimiter, although it is three rows, three \ n, then with \ n Split, it is 4 lines. Improved version:
# !/usr/bin/env python def     = = = Content.count (' \ n ') print  = open ('/etc/passwd'). Read () printinfo (string)

Only the number of occurrences can be counted.

 Script calls
Cat 2. py#!/usr/bin/env python

Import WC
then execute 2.py can go to call wc.py (the current directory, no words will go to pythonpath), the call will be generated after the Wc.pyc file, convenient next call, note that the module name and variable name definition rules are the same, cannot start with a number    

Use of the python-module

Related Article

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.