[Python Basics] Python package (library)

python package

custom package

  • What are Python packages?
    • Physically , a package is a folder that contains a __init__.pyfile, which can be used to contain multiple module files
    • From a logical point of view , the essence of a package is still a module

__init__.pyis a special file, as long as this file exists in this folder, then this folder is a python package

  • The role of the package:

    When we have more and more module files , the package can help us manage these modules . The function of the package is to contain multiple modules , but the essence of the package is still a module

How to create the corresponding package?

Proceed as follows:

  1. new packagemy_package
  2. New in-package module: my_module1andmy_module2
  3. The code in the module is as follows

Basic steps in Pycharm:

[New] -> [Python Package] -> Enter the package name -> [OK] -> Create a new function module (connected module)

Tip: After creating a new package, a file will be automatically created inside the package __init__.py, which controls the import behavior of the package

How to use the package? /import package

method one:

1、import

import 包名.模块名
包名.模块名.目标
# 导入自定义的包中的模块,并使用
import my_package.my_module1
import my_package.my_module2

my_package.my_module1.info_print1()
my_package.my_module2.info_print2()

2、from

# 导入 方式(2)
from my_package import my_module1
from my_package import my_module2

my_module1.info_print1()
my_module2.info_print2()

3、from

# 导入 方式(3)
from my_package.my_module1 import info_print1
from my_package.my_module2 import info_print2

info_print1()
info_print2()

Method 2:

tip: must __init__.pybe added in the file __all__ = []to control the list of modules allowed to be imported

from 包名 import *
模块名.目标
# 通过__all__变量,控制import *
from my_package import *
my_module1.info_print1()
# my_module2.info_print2()

summary

  1. What are python packages?

    A package is a folder in which many python modules (code files) can be stored. Through the package, a batch of modules can be logically classified into one category, which is convenient for use.

  2. __init__.pyWhat does the file do?

    Creating a package will automatically create a file by default. This file indicates that a folder is a python package, not an ordinary folder.

  3. __all__What does the variable do?

    What you learned in the same module is a function to control the content that can be imported by import *

Install third-party packages

What are third-party packages?

These third-party packages have greatly enriched the ecology of python and improved programming efficiency

But because it is a third party, python is not built-in, so we need to install them before we can import and use

Install third-party packages - pip

The installation of third-party packages is very simple, we only need to use Python's built-in pip program

Open: Command Prompt program, enter in it:

pip install package-name

You can quickly install third-party packages through the network

Although successfully connected to the Internet, the speed is very slow (because the connection is a foreign website)

pip network optimization

Since pip is the next week to connect to foreign websites for packaging, sometimes the speed will be very slow

We can use the following command to let it connect to the domestic website to install the package:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package name

//This link is the website provided by Tsinghua University, which can be used by the pip program to download third-party packages

Install third-party packages - PyCharm

1. Configure the python interpreter in the lower right corner [Interpreter Settings...] / [Interpreter Settings]

2. You can add third-party packages through the "plus sign" in the settings

3. Search the name of the package directly, select the one you need, and click [Install Package] to install it

Since this is also installed directly from foreign websites, if you think it is slow

You can check [Options]/ [Options], write-i https://pypi.tuna.tsinghua.edu.cn/simple

summary

  1. What are third-party packages? what's the effect?

    Third-party packages are non-Python official built-in packages, which can be installed to expand functions and improve development efficiency

  2. how to install?

    • Inside the command prompt:
      • pip install package-name
      • pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package name
    • Install in PyCharm

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/131118042