程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python foundation] modules and packages

編輯:Python

Modules and packages

  • One 、 Be careful
  • Two 、 How to import modules
  • 3、 ... and 、 Other information about the module
  • Four 、 Package information
  • 5、 ... and 、 Module classification

One 、 Be careful

1. python A module is essentially a python file .
2. Be careful : Self defined python The file name of the file must not conflict with the existing module .
3. Importing a module is essentially loading and executing the contents of the module .

Two 、 How to import modules

First set up under ck.py file , The written functions and contents are as follows

digits = [1,2,3,4]
def login():
print('hello')

1、 The first way to import
Import all

import sys
sys.path.append('E:\pythonProject\linux Study \d7') Add the current file as the search path
print(ck.digits)
ck.login()
# result 
[1, 2, 3, 4]
hello

2、 Partial import

import sys
sys.path.append('E:\pythonProject\linux Study \day7')
from ck import login
ck.login()
# result 
hello

3、 Take the alias

import sys
sys.path.append('E:\pythonProject\linux Study \day7')
from ck import login as Login module
Login module ()
# result 
hello

3、 ... and 、 Other information about the module

1、 Query search path information
import sys
print(sys.path) # Query path of module

2、 see ck Variables and functions that modules can use …
import ck
print(dir(ck)) # see hello Variables and functions that modules can use …

3、 Additional information
print(ck.doc) # View the module documentation
print(ck.file) # Displays the absolute path of the module
print(ck.name) # __name__ When the module is imported , Displays the name of the module .

4、 About _name_ Specific links of the module
stay ck modular , Run by yourself (name), It is shown that main function


digits = [1,2,3,4]
def login():
print('hello')
print(__name__)
# result 
__main__

In the main function , Import ck Module operation (ck.name), It is shown that ck file name

import ck
print(ck.__name__)

5、__all__ Import restrictions
stay ck Write in the module __all__ The message inside , Can be imported externally

digits = [1,2,3,4]
name = ['westos','ck']
def login():
print('hello')
print(__name__)
__all__=[digits,login()] It limits the scope of import to this

Import ck modular , Using functions outside the module fails

from ck import *
login()
print(digits)
print(name)
# result 
NameError: name 'name' is not defined

Four 、 Package information

Build a new one py The package is named sdk, Will generate a _init_ file . establish import py Package is actually execution _init_ Contents of the document

write in ali With Huawei py file

The import method is as follows
Method 1:

from sdk import ali
from sdk import huawei
ali.create_ecs()
huawei.create_ecs()

Method 2: Relatively troublesome , Need to be in the bag __init__.py Add import information .

import sdk
sdk.ali.create_ecs()
sdk.huawei.create_ecs()

5、 ... and 、 Module classification

1、 Module classification :
Built-in module :time,datetime,random, string
Third-party module : requests, pandas, colorama, faker
Custom module : self-written python file ( modular ) Or a bag

2、 Specific information of time module
The first module time

import time
print(time.time()) # Calculate timestamp 
print(time.ctime()) # The time of the string , Sun Feb 7 17:09:55 2021
tuple_time = time.localtime() # Tuple type time 
print(tuple_time.tm_year) # 2022

The second module datetime

from datetime import date, datetime, timedelta
print(date.today()) # Get today's date 
print(datetime.now()) # Get the current time 2021-02-07 17:13:17.170345
print(date.today() + timedelta(days=3)) # obtain 3 Days after 
print(date.today() - timedelta(days=3)) # obtain 3 The day before 
print(datetime.now() + timedelta(minutes=10)) # obtain 10 Time information after minutes 
print(datetime.now() - timedelta(minutes=10)) # obtain 10 Time information minutes ago 

3、random modular

import random
print(random.random()) # Generate 0-1 Decimal between 
print(random.randint(1, 10)) # Generate 1-10 Integer between 
print(random.choice([' Zhao Yan ', ' Zhang Hongyu ', ' Zhang Qianjun '])) # Choose an element at random 
print(random.sample([' Zhao Yan ', ' Zhang Hongyu ', ' Zhang Qianjun '], 2)) # Random selection n(n=2) Elements 
print(random.choices([' Zhao Yan ', ' Zhang Hongyu ', ' Zhang Qianjun '], weights=[100, 10, 10])) # Choose an element at random , You can specify weights 

4、 Install external modules
Windows Configure global pip Mirror source : https://blog.csdn.net/u011627161/article/details/92766340

How to install modules ?

>pip install colorama -i https://pypi.douban.com/simple


Use externally installed colors

from colorama import Fore
print(Fore.RED + 'Error: The host does not exist ')
print(Fore.GREEN + 'Success: Host created successfully ')

result

5、faker modular
Module for generating test information

from faker import Faker
fake = Faker('zh-cn') Generated in Chinese
print(fake.name()) Generate a random name
print(fake.address()) Generate a random address
print(fake.email()) Generate a random mailbox

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved