The difference between directory and Python package in Pycharm

Source: Internet
Author: User

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.

Directory:
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

Package (A. py file is considered a module in Python):
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. The directory has a init.py file so that you can import the. py file in a from...import manner.

The following is a few examples of how to import the module, the directory structure is as follows:

Suppose file is an open source package with some existing methods that we need to use in one file.

hello.py Code:

class Package ():        def Foo (self):'helloworld'class  ()    : def Foo2 (self):        "hello Python"def write (str):    print str

For the module import, personally think that there are the following ways:
(1) Form: Import module (all the modules are imported)

= file.hello.package () # May import multiple module at the same time, so the method used needs to indicate which module is the method O. Foo () file.hello.write ("hello") # description is File.hello the Write method in this module

(2) The class name or method name to be used by the From module import

The FROM = Package () # has declared that the imported package is File.hello This module, so it is no longer necessary to use the prefix O.foo () write ("  Hello")
 

(3) from package import module

 from Image Import Hellohello.write ("hello"= hello.package ()  # It is also possible to import multiple modules, so it is necessary to indicate which module is O. Foo ()

It is important to note that you can import from the package, but the directory cannot be imported, and you want the. py file in a folder, you must have a init.py file, you can use the class, function, and variable in module after module import.

The above content comes from the network!!

The difference between directory and Python package in Pycharm

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.