python-modules and common modules

Source: Internet
Author: User
Tags first string

Python module of the standard file template, of course, can also be deleted all do not write, but, according to the standard is certainly correct.

Lines 1th and 2nd are standard comments, and the 1th line of comments allows the hello.py file to run directly on Unix/linux/mac.
The 2nd line of comments indicates that the. py file itself uses standard UTF-8 encoding;
Line 4th is a string representing the document comment of the module, and the first string of any module code is treated as a document comment of the module;
Line 6th uses the __author__ variable to write the author in, so that when you open the source code, others will be able to admire your name;

The SYS module has a argv variable that stores all the parameters of the command line with the list. ARGV has at least one element, because the first parameter is always the name of the. py file, for example:

Run Python3 hello.py obtained sys.argv is [' hello.py '];
The sys.argv of running Python3 hello.py Michael is [' hello.py ', ' Michael '.
---can pass in parameters when running a. py file; This example is the incoming parameter Michael
So in Django, Python manage.py runserver;python manage.py make migrations, all incoming parameters into manage

You can then look at the configuration in the manage.py to see if there is also a argv variable

__name__ when running a. py file, if __name__ is the contents of the. py file, then = = "__main__";
If __name__ is imported from another module, the module name of the __name__== import module


Normal functions and variable names are public and can be directly referenced, for example: ABC,X123,PI, etc.;

Variables like __xxx__ are special variables that can be referenced directly, but have special uses, such as the __author__,__name__ above is a special variable,
The document annotations defined by the Hello module can also be accessed with special variable __doc__, and our own variables are generally not used in this variable name;

Functions or variables such as _xxx and __xxx are non-public (private) and should not be referenced directly, such as _ABC,__ABC, etc.

The __init__.py of a package is itself a module, and its module name is MyCompany (package name)


Installing third-party modules

In Python, the installation of a third-party module is done through the Package management tool PIP.

If you are using Mac or Linux, install the PIP itself this step can be skipped.
Commands for installing Pillow: PIP3 Install Pillow

If you do not want to use PIP one installation time and effort, but also need to consider compatibility. Can be directly used anaconda, which is a python-based data processing and scientific computing platform, it has built up a number of very useful third-party libraries, we installed Anaconda, is equivalent to the dozens of third-party modules installed automatically, very easy to use.

If you install the third-party library times wrong:

Module Search Path


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

If we want to add our own search directory, there are two ways:
One is to modify the Sys.path directly, add the directory to be searched-----This method is temporary, the file after the end of the operation is invalid
The second method is to set the environment variable Pythonpath, and the contents of the environment variable are automatically added to the module search path.

python-modules and common modules

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.