Python module Runpy__python

Source: Internet
Author: User
Tags pprint

http://www.python.org/dev/peps/pep-0366/

http://www.python.org/dev/peps/pep-0338/

The Runpy module is used to locate and run Python modules without importing. Its main ' to implement the-m ' command line switch ' that allows ' scripts to be located using the Python module Namespac E rather than the filesystem.

The Runpy module is used to locate and execute the module when the Python module is not being import. The main purpose is to implement the results of the command line-m execution of the Python module, but in the script rather than the file system.

Runpy one on two functions:

Runpy.run_module (Mod_name, Init_globals=none, Run_name=none, Alter_sys=false)

Runs the specified module code and returns the global Dictionary of the module.

Example:

Import Runpy
from pprint import pprint

ttt = runpy.run_module (' Classproperty ',  alter_sys=true)
Pprint (TTT)

Runpy.run_path (File_path, Init_globals=none, Run_name=none)

Executes the specified script file and returns the module global dictionary.

Example:

First write a script

$ cat a.py 
print ' iiiiiiiiiiiiinnnnnnnnnnnnnn a%s '%bb

print __name__
print __file__
print __loader__< C4/>print __package__

If __name__ = = ' __main__ ':
    print ' in __main__ '
And then

Import runpy from
pprint import pprint

bb = ' UUU '
GGG = Runpy.run_path (' a.py ', init_globals={' BB ': bb}, Run_ Name= ' __main__ ')

print ' ===================== '
pprint (GGG)

Init_globals is a dictionary passed to the running module (file), and there are four global variables that are bound to pass through: __name__, __file__, __loader__ and __package__

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.