SoFunction
Updated on 2024-11-13

How python modules are viewed

For compiled languages, such as a .cs file in C#, a .java or compiled .class file in Java can be considered a module (but often not expressed as a module); for interpreted languages will be more intuitive, such as PHP's .php file, in Python it is a .py file can be considered a module. On top of "module" there is "package", mainly to facilitate the organization and management of modules. For example, C# compiled .dll file (but often not expressed as a package Package, but Library Library), Java will be .class packaged .jar file, PHP .phar file (imitating the Java package), in Python, a special definition of the folder is a package, can be packaged as an egg file. But for interpreted languages "package" does not mean compiled into a low-level language and then packaged, just more convenient modularization and management of inter-module dependencies. Each programming language has certain conventions for module and package management, and not understanding these conventions will bring obstacles to learning the language. I'd like to take a look at some of these conventions in Python.

I. Python to find the path of the module

When running a Python application or referencing a Python module, the Python interpreter has to go through a search process. You can set an environment variable PYTHONPATH for Python to add a search path, in order to easily find the relevant Python module (different operating system environment variable settings are slightly different, the default following are WIndows environment), which is the same as many applications need to set a system environment variable. You can set it on the command line with the following command:

C:\Users\Administrator>set PYTHONPATH=E:/Project/Python/ModuleAndPackage/

After entering the Python environment you can, through the properties of Python to get the configuration of the current search path, you can see that the path we set before has been in the current search path.

C:\Users\Administrator>python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> 
['', 'E:\\Project\\Python\\ModuleAndPackage', 'C:\\Windows\\system32\\','C:\\Python\\DLLs','C:\\Python\\
lib', 'C:\\Python\\lib\\plat-win', 'C:\\Python\\lib\\lib-tk', 'C:\\Python', 'C:\\Python\\lib\\site-packages']

>>>

It is also possible to add search paths in the Python environment through the append method of the sys module.

>>> ("E:\\Project\\Python\\ModuleAndPackage2")
>>> 
['', 'E:\\Project\\Python\\ModuleAndPackage', 'C:\\Windows\\system32\\','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']
>>>

II. Modules and Packages in Python

As mentioned earlier, each .py file can be considered a Python module. A .py file can contain classes, methods, variables and constants (Python does not yet have constants in the strict sense of the word, but only conventionally capitalized variables as constants), and all logical statements can be written directly within the file and executed directly from above and below when it is loaded, which is similar to other interpreted languages. For example, we have created a simple Python module by choosing to create a text file file in the folder ModuleAndPackage with the following contents:

# -*- coding: utf-8 -*-
ID = 1
name = "This person"
print name
def say(something):
  print name,'says', something

Then next we can execute it in the Python environment. We can just execute it like a batch file, by typing at the cmd command line:

Python E:/Project/Python/ModuleAndPackage/

Essentially any Python application's entry module is executed this way (like the main function in C# and Java), but referencing a module involves setting up the context environment in which to run it. We first set an environment variable PYTHONPATH so that the Python interpreter can find the module, and then import person module to access the methods or variables in it.

C:\Users\Administrator>python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import person
This person
>>> ("hello")
This person says hello
>>> print 
This person
>>>

Python needs to go to certain fixed paths to find Python modules, and above we set up to find them in ModuleAndPackage. But these paths have a hierarchy of directories, so how does Python find modules in subdirectories? Especially when referencing third-party packages, we also need to know a certain hierarchy. In fact, Python builds its package structure from directories and files, and packages are nested hierarchically, in the same way that directories are nested hierarchically, so that they form access paths within packages (or namespaces, or you could say that the namespace of a Python application corresponds to its directory and file structure, which seems to be missing some flexibility, but is also simpler). For example, let's create a folder animal in the ModuleAndPackage folder, and inside it create a text file with the following contents:

# -*- coding: utf-8 -*-
ID = 2
name = "This pet"
print name
def run(somewhere):
  print name,'runs', somewhere

So how do you reference this module? According to Python conventions, an empty text file named __init__.py needs to be created in the animal folder to identify the animal folder as a package. If there are other folders within the animal folder that are packages, they must also contain the __init__.py file. This identifies the access paths in a hierarchical manner.

>>> import 
This pet
>>> print 
This pet
>>> ("everywhere")
This pet runs everywhere
>>>

Or use the from keyword to import properties or methods directly from within the module:

>>> from  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. We also often reference other modules when we write programs, including Python's built-in modules and modules from third parties.

Modules also avoid function name and variable name conflicts. Functions and variables with the same name can exist in separate modules. But also be careful to try not to conflict with built-in function names.

What if different people write modules with the same name? To avoid module name conflicts, Python introduced another way to organize modules by directories, called packages.

to this article on how to view the python module is introduced to this article, more related python module where the content please search my previous posts or continue to browse the following related articles I hope that you will support me more in the future!