欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python導(dǎo)入自定義路徑的方法

 更新時(shí)間:2022年04月29日 08:50:10   作者:決戰(zhàn)北京城  
這篇文章主要介紹了Python導(dǎo)入自定義路徑的方法,文章基于python的相關(guān)資料展開詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下

前言:

Python可以引入指定路徑的文件,原理就是使用sys.path.append加入到程序查找的路徑。

實(shí)驗(yàn)?zāi)康模?/strong>調(diào)用不同目錄的類和接口,entry調(diào)用is_classis_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)文章

最新評論