The difference between directory directory and package package in PyCharm for getting started with Python

For Python , it is important to recognize that, as a relatively lightweight and easy-to-use scripting language (of course, its functions are not limited to this, this feature is only discussed here), as the program develops As you grow, you may want to split it into several files for clarity and maintainability, or you may want to use a function in several programs without having to copy and paste it into all programs.

To support this, Python has a way to define functions in a file and use them in scripts. Such files are called modules, and definitions in one module can be imported into other modules, or into the main module.

In simple terms, a module in python refers to a py file. If we put all related codes in a py file, the py file is both a program and a module, but the design purposes of the program and the module are different. The purpose of a module is to run, and the purpose of a module is to be referenced by other programs.

    • Dictionary

      Dictionary is a folder in pycharm, where resource files are placed, which corresponds to the directory used to place css/ js files during JavaWeb development, or the folder used to store background images during object recognition. This folder does not contain the _ _ init.py_ _ file

    • Python package

      For the Python package folder, the difference from Dictionary is that it automatically creates an _ _ init__.py file. 
      Simply put, a python package is a directory that includes a set of modules and a _ _ init__.py file.

      Image/
      _init  _.py
      jpg.py
      tiff.py
      bmp.py

As long as the image directory is a subdirectory of our program directory, we can import any module in the image directory for our use, which can be used as follows:

import Image.bmp
tool = Image.bmp.read('a.bmp')

_ _ init_ _.py

This file is related to Python's import mechanism, which is related to which of your .py files are externally accessible. Sometimes, if there are many modules in a package, it is very troublesome and inelegant to import so many modules on the caller side. In this case, you can complete the task by modifying _ _ init_ _.py. Define a special variable _  all_ 
in _ _ init_  .py, copy the module to be included to this variable, for example , define  all _=['tiff', 'bmp', 'jpg'] in Image/  _ init_  .py  , here The all corresponds to the module indicated by * in from …import *. At this time, the following statement is used in the reference side:

from image import *
tool = tiff.read('a.tiff')

 

 

Reference: https://www.cnblogs.com/LancyWu/p/7283889.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608833&siteId=291194637