Python導(dǎo)入自定義路徑的方法
前言:
Python可以引入指定路徑的文件,原理就是使用sys.path.append加入到程序查找的路徑。
實(shí)驗(yàn)?zāi)康模?/strong>調(diào)用不同目錄的類和接口,entry調(diào)用is_class和is_method的接口。
實(shí)驗(yàn)過程:
使用sys.path.append('Dir1\\Dir2'),把當(dāng)前目錄下的“Dir1\\Dir2”加入到python查找文件的路徑下。import方法或者類就會在Dir1\\Dir2路徑下查找。
測試目錄:C:\\Users\\OOXX\\Desktop\\test
目錄結(jié)構(gòu):
C:.
│ entry.py
│
└─Dir1
└─Dir2
│ is_class.py
│ is_method.py
is_method.py內(nèi)容:
def to_do():
? ? print('method to do')is_class.py內(nèi)容
class Class:
? ? def __init__(self):
? ? ? ? print('class init')
? ? ? ??
? ? def to_do(self):
? ? ? ? print('class to do')entry.py內(nèi)容:
import sys
?
sys.path.append('Dir1\\Dir2')
import is_method
from ? is_class import Class
?
print(sys.path)
print('----------------------------------------------------')
?
print('class import example.............................')
Class().to_do()
?
print('')
print('method import example............................')
is_method.to_do()開始執(zhí)行測試:
$ python entry.py ['C:\\Users\\OOXX\\Desktop\\test', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\Ouyanghy\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Ouyanghy\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\Pythonwin',? 'Dir1\\Dir2'] ---------------------------------------------------- class import example............................. class init class to do ? method import example............................ exec to do
打印sys.path可以看到'Dir1\\Dir2'在環(huán)境變量的list內(nèi)。
到此這篇關(guān)于Python導(dǎo)入自定義路徑的方法的文章就介紹到這了,更多相關(guān)Python導(dǎo)入路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python采集腳本之家電子書資源并自動(dòng)下載到本地的實(shí)例腳本
這篇文章主要介紹了python采集jb51電子書資源并自動(dòng)下載到本地實(shí)例教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
django使用xlwt導(dǎo)出excel文件實(shí)例代碼
這篇文章主要介紹了django使用xlwt導(dǎo)出excel文件實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
使用python requests模塊發(fā)送http請求及接收響應(yīng)的方法
用 python 編寫 http request 消息代碼時(shí),建議用requests庫,因?yàn)閞equests比urllib內(nèi)置庫更為簡捷,requests可以直接構(gòu)造get,post請求并發(fā)送,本文給大家介紹了使用python requests模塊發(fā)送http請求及接收響應(yīng)的方法,需要的朋友可以參考下2024-03-03
python實(shí)現(xiàn)余弦相似度文本比較的示例
這篇文章主要介紹了python實(shí)現(xiàn)余弦相似度文本比較的示例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Python實(shí)現(xiàn)遍歷目錄的方法【測試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)遍歷目錄的方法,涉及Python針對目錄與文件的遍歷、判斷、讀取相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
PyCharm Community安裝與配置的詳細(xì)教程
這篇文章主要介紹了PyCharm Community安裝與配置的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

