Python導(dǎo)入自定義路徑的方法
前言:
Python
可以引入指定路徑的文件,原理就是使用sys.path.append
加入到程序查找的路徑。
實(shí)驗(yàn)?zāi)康模?/strong>調(diào)用不同目錄的類(lèi)和接口,entry
調(diào)用is_class
和is_method
的接口。
實(shí)驗(yàn)過(guò)程:
使用sys.path.append('Dir1\\Dir2')
,把當(dāng)前目錄下的“Dir1\\Dir2
”加入到python
查找文件的路徑下。import
方法或者類(lèi)就會(huì)在Dir1\\Dir2路徑下查找。
測(cè)試目錄: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()
開(kāi)始執(zhí)行測(cè)試:
$ 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Django獲取URL中的數(shù)據(jù)詳解
這篇文章主要介紹了Python Django獲取URL中的數(shù)據(jù)詳解,小編覺(jué)得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考2021-11-11django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
本文主要介紹了django生產(chǎn)環(huán)境搭建,主要包括uWSGI+django+nginx+python+MySQL,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08詳解pytorch中squeeze()和unsqueeze()函數(shù)介紹
這篇文章主要介紹了詳解pytorch中squeeze()和unsqueeze()函數(shù)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python將ip地址轉(zhuǎn)換成整數(shù)的方法
這篇文章主要介紹了python將ip地址轉(zhuǎn)換成整數(shù)的方法,涉及Python針對(duì)IP地址的轉(zhuǎn)換技巧,需要的朋友可以參考下2015-03-03Python單元測(cè)試工具doctest和unittest使用解析
這篇文章主要介紹了Python單元測(cè)試工具doctest和unittest使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(一)引言與準(zhǔn)備工作
這篇文章主要介紹了YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(一)引言與準(zhǔn)備工作,在這個(gè)教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車(chē)牌識(shí)別,幫助你快速掌握YOLOv5車(chē)牌識(shí)別技能,需要的朋友可以參考下2023-04-04python數(shù)據(jù)分析matplotlib的基礎(chǔ)繪圖使用
這篇文章主要為大家介紹了python數(shù)據(jù)分析matplotlib的基礎(chǔ)繪圖使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python學(xué)習(xí)之列表常用方法總結(jié)
這篇文章主要為大家介紹了Python中列表的幾個(gè)常用方法總結(jié),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python列表有一定幫助,需要的可以參考一下2022-03-03