一般來說,我們會將自己寫的Python模組與python自帶的模組分開存放以達到便於維護的目的。那麼如何在Python中加入自訂的模組呢?
在解答這個問題之前,我們首先要先明確兩點:
1.嚴格區分套件(package)和資料夾。包的定義就是包含__init__.py的資料夾。如果沒有__init__.py,那麼就是普通的資料夾。
2.模組導入寫法,注意只要包路徑,不要資料夾路徑。
Python 運行環境在尋找庫檔案時是對sys.path 清單進行遍歷,如果我們想要在運行環境中註冊新的類別庫,主要有以下2種方法:
1. 在sys.path清單中新增新的路徑。
2. 將庫檔案複製到sys.path清單中的目錄裡(如site-packages目錄)。
我們可以透過執行程式碼來查看sys.path
import sys print sys.path
運行結果:
['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
這兩種方法中第一種比較簡單,而且對環境的影響最小。
下面我們來看看第一種方法具體如何操作:
在python安裝目錄的site-package資料夾中新建pythontab.pth,上面site-package的路徑是:/ Library/Python/2.7/site-packages,檔案的內容是:需要導入的package所在的資料夾路徑。
這樣,Python 在遍歷已知的庫文件目錄過程中,如果見到一個.pth 文件,就會將文件中所記錄的路徑加入到sys.path 設定中,這樣.pth 檔說指明的package也就可以被Python運作環境順利找到, 我們就可以像使用內建模組一樣引入自訂模組了。
如果預設的sys.path中沒有含有自己的模組或套件的路徑,我們也可以使用sys.path.apend方法來動態加入套件路徑。
以上是如何在Python中加入自訂模組的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

ArraysarecrucialinPythonimageprocessingastheyenableefficientmanipulationandanalysisofimagedata.1)ImagesareconvertedtoNumPyarrays,withgrayscaleimagesas2Darraysandcolorimagesas3Darrays.2)Arraysallowforvectorizedoperations,enablingfastadjustmentslikebri

ArraySaresificatificallyfasterthanlistsForoperationsBenefiting fromDirectMemoryAcccccccCesandFixed-Sizestructures.1)conscessingElements:arraysprovideconstant-timeaccessduetocontoconcotigunmorystorage.2)iteration:araysleveragececacelocality.3)

ArraySareBetterForlement-WiseOperationsDuetofasterAccessCessCessCessCessCessCessCessAndOptimizedImplementations.1)ArrayshaveContiguucuulmemoryfordirectAccesscess.2)列出sareflexible butslible butslowerduetynemicizing.3)

在NumPy中进行整个数组的数学运算可以通过向量化操作高效实现。1)使用简单运算符如加法(arr 2)可对数组进行运算。2)NumPy使用C语言底层库,提升了运算速度。3)可以进行乘法、除法、指数等复杂运算。4)需注意广播操作,确保数组形状兼容。5)使用NumPy函数如np.sum()能显著提高性能。

在Python中,向列表插入元素有兩種主要方法:1)使用insert(index,value)方法,可以在指定索引處插入元素,但在大列表開頭插入效率低;2)使用append(value)方法,在列表末尾添加元素,效率高。對於大列表,建議使用append()或考慮使用deque或NumPy數組來優化性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具