Python基礎學習簡記 模組與包(Day17)

2021-09-29 08:58:08 字數 1109 閱讀 6686

python語言的另乙個強大之處在於python的強大第三方模組。我們只需要簡單的import就可以使用第三方的各種函式功能以達到我們的目標。

1、匯入模組的方法

import

from…import…

import…as…

4.from…import *

import copy

import math

import os

from pytorch_pretrained_bert import berttokenizer

from torch.utils.data import dataset

import tensorflow as tf

import pandas as pd

from keras.optimizers import

*from keras.layers import

*

2、包

建立包的三個步驟:

建立乙個資料夾,用於存放相關的模組,資料夾的名字即包的名字。

在資料夾中建立乙個_init_.py 的模組檔案,內容可以為空。

將相關的模組放入資料夾中。

通常我們並不主張使用 * 這種方法來匯入模組,因為這種方法經常會導致**的可讀性降低。

在匯入乙個包的時候,python 會根據 sys.path 中的目錄來尋找這個包中包含的子目錄。

目錄只有包含乙個叫做_init_.py 的檔案才會被認作是乙個包,最簡單的情況,放乙個空的 _init_.py 就可以了。

目錄只有包含乙個叫做init.py 的檔案才會被認作是乙個包,最簡單的情況,放乙個空的init.py 就可以了。

匯入包的方法和匯入模組是一樣的。

import sound.effects.echo #這將會匯入子模組 sound.effects.echo。 他必須使用全名去訪問。

from sound.effects import echo

from sound.effects.echo import echofilter #直接匯入echo中的函式

3、參考文獻

Python基礎學習簡記 條件與迴圈(Day2)

1 條件語句 ifelse elif assert def guess age height 178print 試著猜一下我的身高吧 temp input try cm 175 180 guess int temp assert 174181if guess height print 對不起,猜大嘍...

Python基礎學習簡記 列表與元組(Day3)

序列是python中最基本的資料結構。序列中的每個元素都分配乙個數字 它的位置,或索引,第乙個索引是0,第二個索引是1,依此類推。python有6個序列的內建型別,但最常見的是列表和元組。序列都可以進行的操作包括索引,切片,加,乘,檢查成員。此外,python已經內建確定序列的長度以及確定最大和最小...

python學習手冊 簡記

匹配hello開頭 world結尾字串 中間的任意字元儲存在group中.import re match re.match hello t world hello python world match group 1 python match re.match usr home lumberjack...