Python.Module.site

Source: Internet
Author: User

site

"This module is automatically imported during initialization. The automatic Import

Can be suppressed using the interpreter ' s -s option.

Importing this module would append site-specific paths to the module search path and add a few builtins. [Ref[1]

1. Import Path

When the Interpreter (interpreter) is started, the site is automatically imported (imported). At the time of import, site extension sys.path.

The way to extend is to use Sys.prefix and Sys.exec_prefix. The definition of prefixes in site.py is as follows, Ref[3]:

1 # prefixes for site-packages, add additional prefixes like/usr/local here 2 prefixes = [Sys.prefix, Sys.exec_prefix]

Q: So what are the meanings of sys.prefix sys.exec_prefix and Sys.path respectively?

REF[4]

Demo-1: View site. Prefixes

1 ImportSYS2 ImportOS3 ImportPlatform4 Importsite5 6 if 'Windows' inchplatform.platform ():7suffixes = [8         "',9         'lib/site-packages',Ten         ] One Else: Asuffixes = [ -         'lib/python%s/site-packages'% Sys.version[:3], -         'Lib/site-python', the         ] -  - Print 'Path Prefixes:' -  forPinchsite. Prefixes: +     Print '  ', P -  +  forPrefixinchSorted (Set (site. prefixes)): A     Print at      forSuffixinchSuffixes: -Path =os.path.join (prefix, suffix). Rstrip (OS.SEP) -         PrintPath -         Print 'exists:', os.path.exists (path) -         Print 'In path:', PathinchSys.path

2. User directories (Users directory)

http://pymotw.com/2/site/

2.1 user_base and User_site

"In addition to the global site-packages paths, site was responsible for adding the

User-specific locations to the import path. " REF[2]

What are Q:globale site-package and import path respectively?

The user-specific path is based on user_base . In User_base, the directory is site-packages directory where the path is

User_site.

"The user-specific paths is all based on the user_base directory, which usually

Located in a part of the filesystem owned (and writable) by the current user. "

A):

Demo-2: View User_base user_site

1 Import site 2 3 Print ' Base: ' , site. User_base4print'site:', site. User_site

The output is:

"

Base:/users/xiaokl/library/python/2.7

Site:/users/xiaokl/library/python/2.7/lib/python/site-packages

"

User_base can be set by the environment variable pythonuserbase .

B): command to view user_base User_site:

$ python-m Site--user-base

$ python-m Site--user-site

$ pythonuserbase=/tmp/$USER python-m site--user-base

$ pythonuserbase=/tmp/$USER python-m site--user-site

Python's-M Module-name:

"-m module-name

Searches Sys.path for the named module and runs the correspond-

ing . py file as a script. "Ref[man python2.7]

2.2 Disable User Directory

The user directory can also is explicitly disabled on the command line with- s.

$ python-s site_enable_user_site.py

3. Path configuration Files

When the path is added to the import path, the path configuration file is scanned for parsing.

A path configuration file is a plain text file with the extension . PTH.

Each line can be:

    1. A full or relative path to another location, should is added to the import path.
    2. A Python statement to is executed. All such lines must begin with an import statement.
    3. Blank lines is ignored.
    4. A line starting with # is treated as a comment and ignored.

The role of the path configuration file:

"path configuration files can be used to extend the import Path to look in locations that

Would not has been added automatically. [Ref[2]

Demo-3: Case with path configuration file not used

Create a directory with_modules.

$ (Root_dir)

|--site_addsitedir.py

|

|--with_modules

|--mymodule.py

site_addsitedir.py: site_addsitedir.py

mymodule.py: mymodule.py

$ python site_addsitedir.py with_modules

"If the directory given to Addsitedir () includes any files matching the pattern *.pth,

They is loaded as path configuration files. " REF[2]

Demo-4: Case with path configuration file

$ (Root_dir)

|--site_addsitedir.py

|

|--with_modules

|--pymotw.pth

|--subdir

|--mymodule.py

The contents of pymotw.pth are:

1 # Add A single subdirectory to the path. 2 ./subdir

Run the output of "$ python site_addsitedir.py with_modules" as follows:

__file__: site_addsitedir.pycwd:/users/xiaokl/projects/privateproject/pythonproject/sitebasename:site_ Addsitedir.pyabspath:/users/xiaokl/projects/privateproject/pythonproject/site/site_addsitedir.pyscript_ Directory:/users/xiaokl/projects/privateproject/pythonproject/sitemodule_directory:with_modulescould not import Mymodule:no module named MyModule------------New paths:   /users/xiaokl/projects/privateproject/ Pythonproject/site/with_modules   /users/xiaokl/projects/privateproject/pythonproject/site/with_modules/ SubDir-------------Loaded mymodule from with_modules/subdir/mymodule.py

4. Sitecustomize

"The site module is also responsible for loading site-wide customization defined by the local

Site owner in a sitecustomize module.

Uses for sitecustomize include extending the import path and enabling coverage, profiling, or

Other development tools. " REF[2]

Demo-5: Use of Sitecustomize

Create sitecustomize.py as follows:

1 Print 'Loading sitecustomize.py'2 3 Importsite4 ImportPlatform5 ImportOS6 ImportSYS7 8Path = Os.path.join ('/opt','python', Sys.version[:3], Platform.platform ())9 Print 'Adding New Path', PathTen                      OneSite.addsitedir (PATH)

Create site_sitecustomize.py as follows:

1 Import SYS 2 3 Print ' Running Main Program ' 4 5 Print ' End of Path: ', sys.path[-1]

Place the scripts sitecustomize.py and site_sitecustomize.py in the With_sitecustomize directory:

with_sitecustomize/

|--sitecustomize.py

|--site_sitecustomize.py

Then execute the following command:

1 $ pythonpath=with_sitecustomize python with_sitecustomize/site_sitecustomize.py

The output is as follows:

/opt/python/2.7/darwin-13.4. 0-x86_64-i386-/opt/python/2.7/darwin-13.4. 0-x86_64-i386-64bit

"Since sitecustomize.py is meant for system-wide configuration, it should be installed Somewere

The default path (usally in the site-packages directory). This example sets PYTHONPATH explicitly

To ensure the module was picked up. " REF[2]

Q: What is a PYTHONPATH?

"The Import search path list can be modified before starting the interpreter by setting

The shell variable PYTHONPATH to a colon-separated list of directories. "

5. Usercustomize

"Similar to sitecustomize, the usercustomize module can is used to set up user-specific setting S

Each time the interpreter starts up. usercustomize is loaded after sitecustomize, so Site-wide

Customizations can be overridden. " REF[2]

"When the User Site Directory feature was disabled, usercustomize is not imported" ref[2 "

6. Disabling site

- s: Disable the import of the module site and the site-dependent manipulations of sys.path tha T it entails.

$ python-s site_import_path.py

Reference

1. site -site-specific configuration Hook

Https://docs.python.org/2/library/site.html

2. Site

http://pymotw.com/2/site/

3. site.py Source Code

https://hg.python.org/cpython/file/2.7/Lib/site.py

4. Sys-prefix & Sys-exec_prefix

Http://pymotw.com/2/sys/interpreter.html#sys-prefix

TODO

Running Code at Python startup

Modules and Imports

Setuptools

Python.Module.site

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.