About Python packages, modules,. pyc files and file import understanding

Source: Internet
Author: User

Reference One, Package

A package is a folder that is used to store modules and sub-packages.

There is usually a __init__.py file (or no) in the package.

There will be a __pycache__ folder in the package that holds the intermediate bytecode (binary files) that the. py file is interpreted by the interpreter.

Second, the module

Files that can be used as modules have . py,. PYc,. Pyo,. PYD,. So,.dll files.

Iii.. pyc files with the. pyo file

These two files are binary files, and the Python interpreter transforms the. py file into a binary file that is designed to speed up interpretation and hide the source code.

When the Python interpreter interprets the. py file, it takes precedence over whether the latest. pyc file is available, or the. pyc file is loaded directly. Before loading, the. pyc file is checked for the latest (determine the modified time for the. py file), and if it is not up-to-date, the. py file is interpreted.

The. pyc file can be generated by Python's own module Py_compile, and a __pycache__ folder is automatically generated to hold the. pyc file.

If you do not have a. py file, only the corresponding. pyc file can also be called because the. pyc file can be used as a module.

The. pyo file is an optimized version of the. pyc file for the same purpose.

Iv. Role of __init__.py documents

The __init__.py function is to turn the package into a module (using a module like. py)

If there is a package name:

1. Add the __all__=[' OS ', ' sys ', ' base64 '] variables in the file, and the variables contain the module names that need to be used. This allows the modules in the __DATA__ list to be loaded in from the package import *.

2. Add the import OS to the file, import the packages that need to be imported so that the package can be called through the pack. OS (must add the package name).

How to put the module you wrote into the package import path

Import Sys

Sys.path.append (' Your path ')

Typically place a package or module in the current directory of the startup file

About Python packages, modules,. pyc files and file import understanding

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.