Python Installation Module & Module Search path

Source: Internet
Author: User

1.1   Mounting Module1.1.1   installing third-party modules

The third-party library will be Python official pypi.python.org website registration, to install a third-party library, you must first know the name of the library, you can search on the official website or PyPI , such as Pillow name called Pillow

Pip Install Pillow

1.1.2   Module Search Path

when we try to load a module, Python searches for the corresponding. py file under the specified path , and if it does not, it will get an error:

>>> Import MyModule

Traceback (most recent):

File "<stdin>", line 1, in <module>

Importerror:no module named MyModule

by default, The Python interpreter searches the current directory , all installed built-in modules, and third-party modules , and the search path is stored in the PATH variable of the SYS module :

>>> Import Sys

>>> Sys.path

[' ', '/library/frameworks/python.framework/versions/3.4/lib/python34.zip ', '/library/frameworks/python.framework /versions/3.4/lib/python3.4 ', '/library/frameworks/python.framework/versions/3.4/lib/python3.4/plat-darwin ', '/ Library/frameworks/python.framework/versions/3.4/lib/python3.4/lib-dynload ', '/library/frameworks/ Python.framework/versions/3.4/lib/python3.4/site-packages ']

If we want to add our own search directory, there are two ways:

one is to modify the Sys.path directlyand add the directories to be searched:

>>> Import Sys

>>> sys.path.append ('/users/michael/my_py_scripts ')

This method is modified at run time and is invalidated after the run is finished.

the second method is to set the environment variable PYTHONPATH, the contents of the environment variable are automatically added to the module search path. Settings are similar to setting the Path environment variable. Note that you only need to add your own search path, andPython 's own search path will not be affected.


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1821050

Python Installation Module & Module Search path

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.