The difference between directory and Python package in Pycharm

Source: Internet
Author: User

the difference between directory and Python package in Pycharm

For Python, one thing to know is that Python is a relatively lightweight, easy-to-use scripting language (which is not limited to this, and is only discussed here), and as the program grows, you may want to divide it into several files so that logic is clearer, better maintained, Or you 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 place definition functions in a file and use them in scripts, which are called modules, and definitions in a module can be imported into other modules, or in the main module.

In short, the module in Python refers to a py file, if we put all the relevant code in a py file, the py file is not only the program is the module, but the program and the module is designed to be different, the purpose of the program is to run, and the purpose of the module is for other programs to reference

    • Dictionary

      Dictionary is a folder in Pycharm, where a resource file is placed, corresponding to the directory used to place the Css/js file during Javaweb development, or the folder used to store the background image when the object is recognized. This folder does not contain _ _ init.py_ _ File

    • Python Package

      The Python package folder differs from dictionary in that it automatically creates _ _ init.py_ _ files.
      Simply put, the 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 under the image directory for our use, as follows:

    • _ _ Init_ _.py

      This file is related to the import mechanism of Python, which relates to which of your. py files are externally accessible. Sometimes, if there are many modules under a package, it is cumbersome and not elegant to import so many modules in the caller, at this point you can do this by modifying _ _ Init_ _.py.
      Define the Special variable _ All_ in _ _ init_. py , and copy the module that will be included to the variable, for example , define all _=[' TIFF ' in image/_ init_ . py , ' BMP ', ' jpg '], where all corresponds to the From ... import * refers to the module, at this time in the reference party using the following statement:

In fact _ _ Init_ . Py can be empty, when it is empty, the from image import * will refer to all the modules under the image package, if you want to control the referenced module, you can define the all _

The difference between directory and Python package in Pycharm

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.