包與模組管理

2021-09-25 01:14:11 字數 1702 閱讀 7601

第乙個檔案命名為models.py

# models.py檔案

page =

5def

test()

:print

('models.test()'

)

第二個檔案命名為views.py

# views.py檔案

x =99

deftest()

:print

('views.test()'

)

import math

print

(math.pi)

3.141592653589793

f =

3.14159

print

(math.trunc(f)

)#向0取整

import models

print

(models.page)

import views

views.test(

)

views.test()

from models import page

defhello()

:print

(page)

hello(

)

from models import

*#把所有的都匯入

print

(page)

defhello()

: test(

)hello(

)

5

models.test()

# 為了區分名稱防止混淆,如下幾種方法

import models #方法一

import views

models.test(

)views.test(

)

models.test()

views.test()

from models import test as m_test  #方法二

from views import test as v_test

m_test(

)v_test(

)

models.test()

views.test()

# models.py檔案發生改變

page =

5def

test()

:print

('models.test()'

)print

('hahah'

)

當檔案發生改變,可以重新匯入模組,但是一直改變檔案內容,一直重新匯入會很麻煩,所以有了以下的方法

import importlib

importlib.

reload

(models)

models.py』>

出現這種提示時候代表新的資訊載入進去

models.test(

)

models.test()

hahah

Python包與模組管理

import module 匯入模組 from module import a 匯入模組中的變數或方法 from module import 匯入模組中所有的變數或方法模組是乙個 py檔案 裡面可能含有多個變數及方法,使用 import 模組名 匯入模組後,模組中的變數或方法可以通過 模組名.變數名...

py 類,模組, 包與庫 與 pip 管理

模組和包,庫的概念經常被混用.一般來講,跟著xx.py的後面還會有xx.pyc 這是py直譯器根據原始檔生成的中間檔案,方便下次使用.模組即原始檔.內含若干個類.py會在 sys.path 指定的若干個目錄下去尋找模組.這些目錄若不夠用還可以自己擴充,見 import sys 當引入其他模組時,嘗試...

python第7天包與模組管理

模組理解 為把功能邏輯寫入字尾名為py的檔案裡面,根據需要可以任意呼叫。模組使用 1.用import,在a的模組裡面想用b的成員 變數,函式,類 如果只使用import是匯入所有的,用的時候形式 模組.方法。2.from從某某裡面匯入具體的方法或物件是可以直接寫。import math from m...