024模組的概念,024模組概念

來源:互聯網
上載者:User

024模組的概念,024模組概念

###模組
calculate是自己寫的,出現紅色也可以調用

import   calculate      # 通過搜尋路徑找到calculate.py後,將calculate = calculate.py(的全部代碼)print(calculate.add(4,5))from  calculate  import *print(add(3,4),'    ',sub(3,4))from  calculate  import  add  as  plusprint(plus(3,4))

###包
匯入包中的模組

from  web  import  loggerlogger.logger()

匯入包中包的模組

from  web.web2  import  loggerlogger.logger()

匯入包中包模組的方法

from  web.web2.logger  import  loggerlogger()

 

匯入包解釋了__init__檔案
匯入模組和包的區別,匯入模組把模組解釋了一遍,匯入包只是解釋了__init__檔案

###項目中的模組匯入比較複雜
簡單目錄結構,最後執行bin.py

logger

 

main

這裡使用from module import  logger的原因是因為在bin下執行的,工作目錄在那裡,但是還是要使用sys模組添加工作路徑。

 #bin
# 所以就有了下面的步驟

import  sys,osBASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   #拿到這個路徑的上兩層路徑,也就是atm_demo路徑print(BASE_DIR)          #F:\python_code\atm_demosys.path.append(BASE_DIR)    #之所以在這裡添加,是因為from  module  import  mainmain.main()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.