[Python] self-answer: python-m parameter?

Source: Internet
Author: User

Python-m xxx.py

Function: Start the xxx.py file as a module
But I never understood what it would be like to start a module. What's the difference between Python xxx.py and python-m xxx.py!

Ask yourself a self-answer:

    1. Python xxx.py
    2. Python-m xxx.py

Here are two ways to load a py file:
1 is called direct run
2 equivalent to import, called as a module to start

Different ways to load the Py file are mainly affected by the--sys.path property. Sys.path is equivalent to path in Liunx.

Here's a look at Sys.path

>>> import sys>>> sys.path[‘‘, ‘/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python27.zip‘,  ...]

I've only intercepted a section of the above. This is the environment in which the current Python interpreter runs, and the Python interpreter will look for a dependent library in these directories!
Note: "--For the current directory
***

Here are two examples of how the value of the Sys.path property differs by starting the same file in different ways.

# run.py 内容如下import sysprint(sys.path)
# 直接启动:python run.pytest_import_project git:(master) ? python run.py[‘/Users/sx/Documents/note/test_py/test_import_project‘, ‘/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python27.zip‘, ...]
# 以模块方式启动:python -m run.pytest_import_project git:(master) ? python -m run.py[‘‘, ‘/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python27.zip‘,

Careful classmates will find that the difference is in the first line.
Direct start is to put the run.py file, the directory in which the Sys.path attribute.
The module startup is the directory where you enter the command (that is, the current path) into the Sys.path attribute * * *

What's the use of the work scene?
# 目录结构如下package/    __init__.py    mod1.pypackage2/    __init__.py    run.py  
# run.py 内容如下import sysfrom package import mod1print(sys.path)
How can I start the run.py file?
# Direct Start (failed)?test_import_project git: (Master)? python package2/run.pytraceback (most recent call last) : file  "package2/run.py", line 2,  In <module> from package import mod1# started in a modular manner (success)? test_import_project git: (Master)? python-m package2.run[ "              

A module is referenced when a py file needs to be started. You need to be aware that you need to consider the path of your import module in Sys.path when booting up!
At this time, in the end is the use of direct boot, or module start? The goal is to place the path of the import module in the Sys.path. Did you get it?

Http://www.cnblogs.com/xueweihan/p/5118222.html

[Python] self-answer: python-m parameter? Go

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.