Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the details in the py file of Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the details in the py file of Python". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A module is a file with a .py suffix, in which we can edit Python code, such as variables, functions, classes, logic, and so on, to meet a particular function.

For example, to create a new test_for_xxxx.py file, we have created a test_for_xxxx module whose name is the name of the .py script file.

Declaration code

Write the following declaration code at the head of the file:

#! / usr/bin/python or #! / usr/bin/env python#coding:utf-8

#! / usr/bin/python specifies that the Python interpreter in the [/ usr/bin/python] directory executes the Python script.

#! / usr/bin/env python executes the Python script based on the first Python interpreter specified in the PATH environment variable. [recommended]

# coding:utf-8 declares that the encoding format of the file is utf-8. Enter this code to make the py file support Chinese display.

Program entry

Add the following code to the file:

If _ name__ = = "_ _ main__"

If name = 'main' is equivalent to the program entry for Python simulation, which is not specified by Python itself, which is just a coding habit. When the .py file is run directly, the code block under if name = 'main' is run; when the .py file is imported as a module, the code block under if name =' main' is not run.

The concept of package

A package is a concept on top of a module, which packages files for ease of management, which is essentially a directory. For example, when two module files with different functions have the same name, and it is impossible to distinguish which module is imported, we can construct a package and put the module under the package folder. The module name refers to the module. Usually a package file consists of _ _ init__.py and many other .py files. The _ _ init__.py content can be empty or you can write some initialization code when the package is executed.

In addition to the module file, the package directory can also contain a subdirectory. If there is an init.py in the subdirectory, then it is a subpackage of the package.

Common package structures:

Package

├── init.py

├── module1.py

└── module2.py

Sometimes the concept of library is mentioned. Python library emphasizes its functionality and can be regarded as a collection of modules and packages with related functions, that is to say, modules and packages with certain functions can be called libraries. For example, Python has built-in powerful standard libraries and third-party libraries with powerful scientific computing functions such as NumPy and Pandas.

The usage of import

Next, we will introduce how to use import. Modules can be imported in the following ways:

Import module name as custom name # can avoid conflicts import module name 1import module name 1, module name 2

For example, import in import test_for_xxxx essentially interprets and compiles all the code in test_for_xxxx.py and assigns a value to a variable test_for_xxxx of the same name of the current module.

We need to write test_for_xxxx.name when we want to access the name property in test_for_xxxx.py, and test_for_xxxx.add () if we want to call the add () function inside. After importing the module, the folder where the module is located will automatically generate a corresponding _ _ pycache__\ test_for_xxxx.cpython-36.pyc file.

All modules loaded into memory are placed in sys.modules, so when you execute import, you first query whether modules have been added to the list. If you have already added to the sys.modules, you only need to add the name of the module to the local space where the module is being called. If it has not been added to sys.modules, you need to find the files of the module in order in the directory of all paths to sys.path. These files are generally suffixed with ".py", ".pyo", ".pyc", ".pyd", ".dll". After finding these modules, you can add these modules to sys.modules, and then import module name to the local.

If you want to import a specific function or method within a module, you can use from... Import... , as follows:

From module name import function name 1, function name 2, function name 3 from module_name import * # Import all methods, not recommended, to avoid conflicts

From... Import... The essence of import in test_for_xxxx.py is to interpret and compile the name attribute and add () function code in test_for_xxxx.py and assign values to variables called name and add. If you call the add () function, you can call it directly.

In addition, the direct difference between the import module and the import package is that the import package only executes the _ _ init__.py file under the package.

This is the end of the content of "what are the details in the py file of Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report