Introduction to Python module search concepts and module installation methods

Source: Internet
Author: User

Introduction to Python module search concepts and module installation methods

This article mainly introduces the concept of Python module search and the module installation method. This article describes in detail the operating principle of the import module, the process of searching paths, and various methods of module installation, for more information, see

[Import module]

Unlike # include in C, the import Statement in Python does not simply insert a file into another file.

The import operation is a running operation. When the program imports a specified file for the first time, it performs the following steps,

1. Find the module File

2. Compile the code into a bid.

3. Execute the code in the module to create the defined Module

And generate the. pyc bytecode file. These three steps are only performed when the program is executed and the module is imported for the first time. When importing the same module, the three steps are skipped, and only the loaded module objects in the memory are extracted, which is much faster.

NOTE:

1. Python puts the loaded modules in the built-in sys. modules dictionary. At the beginning of the import operation, it will confirm whether the referenced modules are loaded. If you want to see which modules have been loaded, you can import sys and print sys. modules. keys ()

Copy the Code as follows:

>>> Sys. modules. keys ()

['Heapq', 'functions', 'pyreadline. console. ansi ',..., 'pyreadline. mode']

2. Four file types that can be imported

1. source file (. py)

2. bytecode file (. pyc)

3. C extension file (Windows:. dll or. pyd file | Linux:. so file)

4. Directory with the same variable name (for package import)

[Search path]

For example, if you do not need to add a path or suffix to import math, the system will automatically search for the path and list the module search path in the search order.

1. Main directory of the program (the directory currently running the program)

2. PYTHONPATH directory (the configuration in the environment variable is the same as that added to the Path)

3. Standard Library directory (for example, C: \ Python27 \ Lib)

4. pth file content (usually placed in the Lib \ site-packages directory, as follows: easy-install.pth)

The Code is as follows:

Import sys; sys. _ plen = len (sys. path)

/Setuptools-0.6c11-py2.7.egg

/Py-1.4.26-py2.7.egg

/Sphinx-1.3b1-py2.7.egg

/Colorama-0.3.2-py2.7.egg

/Babel-1.3-py2.7.egg

/Snowballstemmer-1.2.0-py2.7.egg

/Docutils-0.12-py2.7.egg

/Pygments-2.0.1-py2.7.egg

/Jinja2-2.7.3-py2.7.egg

/Six-1.8.0-py2.7.egg

/Pytz-2014.9-py2.7.egg

/Markupsafe-0.23-py2.7.egg

/Pyzmq-14.4.1-py2.7-win-amd64.egg

/Tornado-4.0.2-py2.7-win-amd64.egg

./Backports. ssl_match_hostname-3.4.0.2-py2.7.egg

/Certifi-14.05.14-py2.7.egg

/Egg-0.2.0-py2.7.egg

/Chicken-0.1.0-py2.7.egg

Import sys; new = sys. path [sys. _ plen:]; del sys. path [sys. _ plen:]; p = getattr (sys, '_ egginsert', 0); sys. path [p: p] = new; sys. _ egginsert = p + len (new)

All these paths can be seen through sys. path.

The Code is as follows:

>>> Import sys

>>> Sys. path

['', 'C: \ Python27 \ lib \ site-packages \ setuptools-0.6c11-py2.7.egg ',..., 'C: \ Python27 \ lib \ site-packages \ wx-3.0-msw ']

Temporary Working path loading, which can be used

The Code is as follows:

>>> Sys. path. append ('d: \ test ')

>>> Sys. path

['', 'C: \ Python27 \ lib \ site-packages \ setuptools-0.6c11-py2.7.egg ', 'c: \ Python27 \ lib \, 'd: \ test']

>>> Sys. path. remove ('d: \ test ')

[Module installation]

However, the above method is only temporary. Once you exit the program or the current interactive environment, it becomes invalid. To take effect permanently, you must add it to the preceding directory.

The power of Perl lies in CPAN. Fortunately, Python has begun to have similar technologies and functions, such as pip, which can be used to install modules.

1. The self-Writing module is directly copied to any of the preceding directories.

2. Run python setup. py install to install the third-party module.

3. Install with easy_install

Download the ez_setup.py File

Run: python ez_setup.py

Install the required module: easy_install py

4. Install with pip

Install pip with the easy_install pip command

Use pip install Markdown to install a specific module. Assume the module name is Markdown.

5. Use a third-party module to manage the database (for example, Canopy can manage the scientific computing module, but a lot of fees are required)

With this tool, you can install the ipython environment.

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.