Python Module Search concept Introduction and Module installation Method introduction

Source: Internet
Author: User
"Import Module"

Unlike the # include in C, the import statement in Python does not simply insert a file into another file.
Import is actually run-time operations, the first time the program imports the specified file, the following steps are performed,

1. Locate the module file
2. Compile into a bit code
3. Execute the code in the module to create the defined module

and generate. PYc bytecode files, these three steps are performed only when the program is executed and the module is imported the first time. When the same module is imported, the three steps are skipped, and only the loaded module objects in memory are fetched much faster.
Note:

1. Python places the loaded module in the built-in Sys.modules dictionary, confirming that the referenced module is loaded at the beginning of the import operation. If you want to see which modules are loaded, you can import sys, print Sys.modules.keys ()
Copy the 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 extension Files (Windows:. dll or. pyd Files | Linux:. so files)
4. Directory with the same variable name (for package import)

"Search Path"

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

1. The program's home directory (the directory that the program is currently running)
2. Pythonpath directory (as configured in the environment variable and added to path)
3. Standard library directory (e.g., C:\PYTHON27\LIB)
4. The contents of the. pth file (typically placed in the Lib\site-packages directory, as follows: Easy-install.pth)

Copy the Code code 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.
Copy the Code code 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 work path loading, can be used
Copy CodeThe 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"

But the above method is only temporary, and once exiting the program or the current interactive environment, it will fail. If you want to make it permanent, you have to add it to the list above.
The power of Perl is CPAN, and, happily, Python is starting to have similar technologies and features, such as Pip, which can be used to install modules with PIP included.

1. Self-write module, copy directly to any of the above directories
2. Third-party module, execute Python setup.py install installation
3. Install with Easy_install

Download ez_setup.py file
Execution: Python ez_setup.py
Install the module you want to install: Easy_install py

4. Install with PIP

Install PIP with the easy_install pip command first
Then use PIP install Markdown to install the specific module, assuming that the module name is: Markdown

5. Using a third-party module to manage the library (for example, canopy can manage the scientific computing module, but 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.