Python导入自定义路径的方法

作者:决战北京城 时间:2021-06-30 13:43:28 

前言:

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

实验目的:调用不同目录的类和接口,entry调用is_classis_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

标签:Python,导入,自定义,路径
0
投稿

猜你喜欢

  • django缓存配置的几种方法详解

    2022-08-29 02:37:16
  • python集成开发环境配置(pycharm)

    2021-08-28 08:23:10
  • python设置检查点简单实现代码

    2023-09-25 19:08:44
  • TensorFlow查看输入节点和输出节点名称方式

    2022-11-27 04:50:11
  • 豆瓣可以做而且值得做的几件事情

    2009-04-24 12:07:00
  • Pandas提取单元格的值操作

    2022-10-19 08:34:21
  • asp的access数据库备份 压缩 恢复及清理数据库函数

    2008-10-31 12:36:00
  • python实现Nao机器人的单目测距

    2021-04-09 16:37:10
  • 十分钟轻松掌握dataframe数据选择

    2021-03-03 11:11:40
  • Python一句代码实现找出所有水仙花数的方法

    2021-10-09 08:10:44
  • python微信跳一跳系列之棋子定位颜色识别

    2023-01-16 04:52:49
  • Python回文字符串及回文数字判定功能示例

    2022-05-09 21:59:30
  • 利用setuptools打包python程序的方法步骤

    2023-07-20 08:57:47
  • 详解pandas使用drop_duplicates去除DataFrame重复项参数

    2021-09-12 12:36:26
  • WinHttp.WinHttpRequest--XmlHttp获取跳转页面的功能

    2010-03-22 14:34:00
  • SQL Server数据库入门学习总结

    2012-08-21 11:01:33
  • 用ASP动态生成JS表单验证代码

    2007-09-30 20:38:00
  • Python异步爬虫requests和aiohttp中代理IP的使用

    2021-06-26 11:56:05
  • django-crontab 定时执行任务方法的实现

    2021-04-12 06:22:36
  • Django web自定义通用权限控制实现方法

    2021-07-31 06:21:09
  • asp之家 网络编程 m.aspxhome.com