Python Learning Module & amp; Package reference, python Module

Source: Internet
Author: User

Python Learning Module & package reference, python Module

Glossary:

Module: A program file

Package: it is equivalent to a class library. After packaging and publishing, it is equivalent to a dll in c #. The package may contain several modules. For example, main. py is a module, which makes up a package for all modules in the test2 file.

For a package, it is destined to be referenced by other modules. Therefore, an initialization information, such as the package path, is required, these information is dependent on the _ init _ py file under the package. For the sub-packages under the package (sub-namespaces under the namespace), the _ init _ py file exists at each level.

For details, refer

The following describes how to reference a module or package in the code.

1: reference the provide. py module in the main program main. py and execute the Method

Import provide;
Provide. stringProvider ("hello ");

Or
From provide import stringProvider;
StringProvider ("hello ");

Or

From provide import stringProvider;
A = stringProvider;
A ("hello ");

2: reference the module under the test2 class library in the main program main. py and execute the Method

We want to access the info. py module method in tester under test2. Because the class library and module are not in the same directory, we must add an upper-layer path.

There are many ways to access different directories, you can refer to http://python.usyiyi.cn/python_343/tutorial/modules.html#packages-in-multiple-directories

The code is

Import sys;
Sys. path. append ("..");

Therefore, you can reference the data in the same way as above.

From test2.tester import info;
Info. printinfo ();

If it is written as import test2.tester.info;

The reference of the method must also be complete: test2.tester.info. printinfo ();

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.