Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to use module and package

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the methods of using module and package". In the daily operation, I believe that many people have doubts about the use of module and package. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about the use of module and package. Next, please follow the editor to study!

Module

1. The module is the file xxx.py and the name of the module is xxx

two。 If my other modules need to import this module, first import xxx, and then let the python interpreter find the source code of the module, which requires several ways to tell the python interpreter

Method 1: sys.path.append () directly adds the address of this module to the sys.path

Method 2: you can put it in the site-packages directory in python2. In python3, I'm looking at where this directory is.

Method 3: include the directory where the module is located in the PYTHONPATH environment variable. In linux, the environment variable is defined in the file ~ .bashrc, and you can use statements.

The statement export PYTHONPATH=$PYTHONPATH:~/python can add ~ / python to the environment variable, that is, the directory / home/lee/python.

In addition, the current directory of the current module is also in PATHONPATH.

Package

1. The package is the same as in Java, which is a folder for module. In java, you put class. You must have _ _ init__.py in this package, that is, in this folder, it is the package.

two。 Package is also a module. If I import variables, functions and classes in _ _ init__.py directly by import package1, I can access them with package1.classname.

3. Be sure to pay attention to the several import methods when I import the package module, assuming that there are three modules in package1: A.pyreB.pyre.py.

Import package1-> only _ _ init__.py was imported

Import package1.A-> imported A, but only package1.A can call this A module

Form package1 import B-> imported B, directly using B module

Form package1 import *-> imports variables, functions, and classes from the list of _ _ all__ in package1

4. View module content dir (copy) View the location of the source code of the copy module copy.__file__

Fileter and map

Both 1.filter and map can be used with list expressions

For example, def fileterFun (x): return not x.startswith ('_')

Def mapFun (x): return Xero1

Fileter (filterFun,dir (copy) = [x for x in dir (copy) if x not startswith ('_')]

Map (mapFun,range (10)) = map (lambda x: Xerox for x in range (10)) = [Xero1 for x in range (10)]

2.filter and map are both functional programming, and both return generator-like objects. If you want to use it, you need the method list (). In addition, this function is run only when it is actually used, similar to deferred loading.

At this point, the study on "what is the use of module and package" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report