Introduction to Python Module search concept and module installation method

Source: Internet
Author: User
Tags in python

This article mainly introduces the Python module search concept Introduction and the module installment method Introduction, this article explained in detail the Import module operation principle, the search path process as well as the module installment various methods, needs the friend to be possible to refer to under

"Import Module"

Unlike the #include in C, the import statement in Python is not simply inserting a file into another file.

The import is actually a run-time operation, and the first time the program imports the specified file, the following steps are performed,

1. Find the module file

2. Compile a bit code

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

and generates the. PYC bytecode file, which is done only when the program executes the first time the module is imported. After the same module is imported, the three steps are skipped, and only the module objects loaded in memory are extracted, much faster.

Note:

1. Python places the loaded module in the built-in Sys.modules dictionary, confirming that the referenced module has been loaded at the beginning of the import operation. If you want to see which modules have been loaded, you can import sys, print Sys.modules.keys ()

Copy code code as follows:

>>> Sys.modules.keys ()

[' Heapq ', ' functools ', ' pyreadline.console.ansi ',..., ' pyreadline.modes ']

2. Four types of files that can be import

1. source file (. py)

2. Byte code file (. PYc)

3.C Extended file (Windows:. dll or. pyd File | Linux:. So file)

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

"Search Path"

For example: Import math, do not need to add a path and suffix name, the system will automatically search the path, according to the search sequence, list the module search path.

1. Program's home directory (program currently running directory)

2. Pythonpath directory (in environment variable configuration and add to path)

3. Standard library directory (say: C:python27lib)

4. The contents of the file (usually placed in the Libsite-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)

And all these paths can be seen through Sys.path.

The code is as follows:

>>> Import Sys

>>> Sys.path

[', ' C:python27libsite-packagessetuptools-0.6c11-py2.7.egg ', ..., ' C:PYTHON27LIBSITE-PACKAGESWX-3.0-MSW ']

Temporary work path loading, you can use the

The code is as follows:

>>> sys.path.append (' d:test ')

>>> Sys.path

[', ' C:python27libsite-packagessetuptools-0.6c11-py2.7.egg ', ' c:python27lib, ' d:test ']

>>> sys.path.remove (' d:test ')

"Module Installation"

But the above method is only temporary, once exited the program or the current interactive environment, the failure. To be permanently effective, you have to add it to the directory above.

The strength of Perl is cpan, and, thankfully, Python is beginning to have a similar technology and functionality, such as the PIP, which can be installed with the PIP, including the module.

1. Self-writing module, directly copy to any of the above directories

2. Third party module, perform python setup.py install installation

3. Install with Easy_install

Download ez_setup.py file

Executive: Python ez_setup.py

Install the module you want to install: Easy_install py

4. Install with PIP

Use the easy_install pip command to install the PIP first

Then use PIP install markdown to install specific modules, assuming the module is named: markdown

5. Use of Third-party Module management library (for example, canopy can manage Scientific computing module, unfortunately, a lot of charges)

With this tool, you can install the Ipython environment

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.