網站首頁 編程語言 正文
前言:
Python
可以引入指定路徑的文件,原理就是使用sys.path.append
加入到程序查找的路徑。
實驗目的:調用不同目錄的類和接口,entry
調用is_class
和is_method
的接口。
實驗過程:
使用sys.path.append('Dir1\\Dir2')
,把當前目錄下的“Dir1\\Dir2
”加入到python
查找文件的路徑下。import
方法或者類就會在Dir1\\Dir2路徑下查找。
測試目錄:C:\\Users\\OOXX\\Desktop\\test
目錄結構:
C:.
│ ?entry.py
│
└─Dir1
? ? └─Dir2
? ? ? ? │ ?is_class.py
? ? ? ? │ ?is_method.py
is_method.py內容:
def to_do(): ? ? print('method to do')
is_class.py內容
class Class: ? ? def __init__(self): ? ? ? ? print('class init') ? ? ? ?? ? ? def to_do(self): ? ? ? ? print('class to do')
entry.py內容:
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()
開始執行測試:
$ 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
'在環境變量的list內。
原文鏈接:https://blog.csdn.net/obanaganastar/article/details/121994366
相關推薦
- 2022-10-10 C++超詳細分析type_traits_C 語言
- 2022-12-15 網站壓力測試工具-ab工具apache?bench使用過程_服務器其它
- 2022-04-11 C#實現簡易計算器功能(1)(窗體應用)_C#教程
- 2022-08-15 springboot項目搭建和基礎應用
- 2022-03-21 C語言動態內存管理介紹_C 語言
- 2022-05-26 Python學習之文件的讀取詳解_python
- 2022-11-03 PyCharm利用pydevd-pycharm實現Python遠程調試的詳細過程_python
- 2022-08-05 C#實現動態數字時鐘和日歷_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支