For compiled languages, such as a .cs file in C#, a .java or compiled .class file in Java, it can be considered a module (but it is often not expressed as a module); it will be more intuitive for interpreted languages Some, such as PHP's .php file, which is a .py file in Python, can be considered a module. There are "packages" above "modules", mainly for the convenience of organizing and managing modules. For example, the compiled .dll file in C# (but it is often not expressed as a package, but a library library), the .jar file after the .class is packaged by Java, the .phar file of PHP (imitating the Java package), one in Python The specially defined folder is a package, which can be packaged as an egg file. But the interpreted language "package" does not mean that it is compiled into a low-level language and then packaged, but it is more convenient to modularize and manage dependencies between modules. Each programming language has certain conventions for module and package management. If you don't understand these conventions, it will bring obstacles to learning this language. Now I want to sort out these conventions of Python.
One, Python finds the path of the module
To run Python applications or reference Python modules, the Python interpreter must have a search process. You can add a search path for Python by setting an environment variable PYTHONPATH to facilitate finding related Python modules (the setting of environment variables for different operating systems is slightly different, and the default is the Windows environment below), which is different from many applications. The principle of system environment variables is the same. It can be set by the following command in the command line:
C:\Users\Administrator set PYTHONPATH=E:/Project/Python/ModuleAndPackage/
After entering the Python environment, you can obtain the configuration of the current search path through Python's sys.path attribute. You can see that the path we set before is already in the current search path.
C:\Users\Administrator python
Python 2.7.11(v2.7.11:6d1b6a68f775, Dec 52015,20:32:19)[MSC v.150032bit(Intel)] on win32
Type "help","copyright","credits" or "license"for more information.import sys
sys.path
['',' E:\Project\Python\ModuleAndPackage','C:\Windows\system32\python27.zip','C:\Python\DLLs','C:\Python\
lib','C:\Python\lib\plat-win','C:\Python\lib\lib-tk','C:\Python','C:\Python\lib\site-packages']
You can also increase the search path in the Python environment through the append method of the sys module.
sys.path.append("E:\Project\Python\ModuleAndPackage2")
sys.path
['',' E:\Project\Python\ModuleAndPackage','C:\Windows\system32\python27.zip','C:\Python\DLLs','C:\Python\
lib','C:\Python\lib\plat-win','C:\Python\lib\lib-tk','C:\Python','C:\Python\lib\site-packages','E:\
Project\Python\ModuleAndPackage2']
Two, modules and packages in Python
As mentioned earlier, each .py file can be considered as a Python module. The .py file can contain classes, methods, variables, and constants (Python does not have a constant in the strict sense, but it is agreed that capitalized variables are used as constants). You can also write all logic statements directly in the file and execute them directly from top to bottom when loading, which is similar to other interpreted languages. For example, we choose to create a text file person.py file in the folder ModuleAndPackage to create a simple Python module with the following content:
# - *- coding: utf-8-*-
ID =1
name ="This person"
print name
def say(something):
print name,'says', something
Then we can execute person.py in the Python environment. We can execute person.py directly like a batch file, by typing in the cmd command line:
Python E:/Project/Python/ModuleAndPackage/person.py
Essentially, the entry module of any Python application is executed in this way (like the main function in C# and Java), but to reference a module, it is necessary to establish the context for running it. We first set an environment variable PYTHONPATH so that the Python interpreter can find the person.py module, and then import the person module to access the methods or variables.
C:\Users\Administrator python
Python 2.7.11(v2.7.11:6d1b6a68f775, Dec 52015,20:32:19)[MSC v.150032bit(
Intel)] on win32
Type "help","copyright","credits" or "license"for more information.import person
This person
person.say("hello")
This person says hello
print person.name
This person
Python needs to go to certain fixed paths to find Python modules. Above we set it to find in ModuleAndPackage. But these paths also have a directory hierarchy. How does Python find modules in subdirectories? Especially when referencing third-party packages, we also need to know a certain level of relationship. In fact, Python builds a package structure through directories and files, and the package is nested layer by layer, which is the same as the layer by layer nesting of directories. This constitutes the access path (or namespace, or Python application) in the package. The namespace corresponds to its directory and file structure, which seems to lack some flexibility, but it is also simpler). For example, we create a folder animal under the ModuleAndPackage folder, and create a text file pet.py in it with the following content:
# - *- coding: utf-8-*-
ID =2
name ="This pet"
print name
def run(somewhere):
print name,'runs', somewhere
So how to reference the pet.py module? According to the Python convention, an empty text file named init.py needs to be created in the animal folder to identify that the animal folder is a package. If there is a folder as a package in the animal folder, it must also include the init.py file. In this way, the access path is identified layer by layer.
import animal.pet
This pet
print animal.pet.name
This pet
animal.pet.run("everywhere")
This pet runs everywhere
Or use the from keyword to directly import the attributes or methods in the module:
from animal.pet import name,run
print name
This pet
run("everywhere")
This pet runs everywhere
Knowledge point expansion:
**What are the benefits of using modules? **
When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including built-in Python modules and modules from third parties.
Modules can also avoid conflicts between function names and variable names. Functions and variables with the same name can be stored in different modules. But also pay attention, try not to conflict with built-in function names.
What if the module names written by different people are the same? In order to avoid module name conflicts, Python has introduced a method of organizing modules by directories, which is called Package.
So far this article on how to view python modules is introduced. For more relevant python modules, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!
Recommended Posts