Python module search and module installation

Source: Internet
Author: User

"Import Module "

Unlike the # include in C, theimport 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 the. PYc bytecode file, These three steps are performed only when the program executes, 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 ()

>>> Sys.modules.keys () [' Heapq ', ' functools ', ' pyreadline.console.ansi ',..., ' pyreadline.modes ']

2. Four types of files that can be import

    • source file (. py)
    • Byte-code file (. PYc)
    • C Extension Files (Windows:. dll or. pyd Files | Linux:. so files)
    • 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)

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.eggimport 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.
>>> 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

>>> 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
    • re-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.



Python module search and module installation

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.