实际Python项目中导入自定义路径怎样做
Admin 2022-09-14 群英技术资讯 981 次浏览
这篇文章主要介绍了“实际Python项目中导入自定义路径怎样做”相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇实际Python项目中导入自定义路径怎样做文章都会有所收获,下面我们一起来看看吧。前言:
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内。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要和大家分享三个Python中提取人脸特征的方法,文中的示例代码讲解详细,对我们学习Python有一定的帮助,需要的可以参考一下
这篇文章主要为大家介绍了Keras搭建孪生神经网络Siamese network比较图片相似性,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
本文实例为大家分享了Python实现双人五子棋对局的具体代码,供大家参考,具体内容如下效果:自己需要两个棋子:服务器玩家全部代码:# 案列使用TCP连接# 这是服务器端import socketim
前言在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识)、python type()(数据类型)和valu
元类一般用于创建类。在执行类定义时,解释器必须要知道这个类的正确的元类。解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008