Search for and install Python modules and search for python modules

Source: Internet
Author: User

Search for and install Python modules and search for python modules

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.Compiled into bits

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 have been loaded. If you want to see which modules have been loaded, you can import sys and print sys. modules. keys ()

>>> sys.modules.keys()['heapq', 'functools', 'pyreadline.console.ansi',...,'pyreadline.modes']

2.Four file types that can be imported

  • Source file (. py)
  • Bytecode file (. pyc)
  • C extension file (Windows:. dll or. pyd file | Linux:. so file)
  • 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. . PthContents of the file (usually 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)
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 Working path loading, which 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]

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

  • DownloadEz_setup.pyFile
  • Run:Python ez_setup.py
  • Install the modules to be installed: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 third-party module management libraries (for example, Canopy can manage scientific computing modules, 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.