实际Python项目中导入自定义路径怎样做
Admin 2022-09-14 群英技术资讯 1231 次浏览
这篇文章主要介绍了“实际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随机函数random的用法,本文有random随机获取数字、字符串、列表等代码,有这方面学习需要的朋友可以参考,下面我们就一起来了解一下。
大家好,本篇文章主要讲的是python实现简易猜数小游戏,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
python中删除列表有pop()与remove()两种方法,而且他们都是原位操作。但是他们还是有区别的。pop()是按位进行删除;传递的是待删除元素的index;会返回被删除的值。而remove()是按照值来删除;传递待删除元素,如果多个元素一样,默认删除第一个;不会返回被删除的值。
今天给大家分享的是python中print格式化输出的问题,下文实例对大家学习python有一定的帮助,有这方面学习需要的朋友,接下来跟随小编一起来学习一下吧。
这篇文章主要介绍了python连接clickhouse数据库的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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