实际Python项目中导入自定义路径怎样做
Admin 2022-09-14 群英技术资讯 1080 次浏览
这篇文章主要介绍了“实际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 之父 Guido van Rossum(龟爷)终于在 Python 官方邮件组落实了 Python 2 7 的终焉之日(EOL)。说的是 Python
这篇文章主要是带大家写一个利用Turtle库绘制一些有趣的对称图形,文中的示例代码讲解详细,对我们学习Python有一定帮助,感兴趣的可以了解一下
这篇文章主要介绍了Python爬虫Xpath定位数据的方法,第一种方法直接右键,将文章路径复制下来点击Copy full Xpath,方法二使用@制定标签属性,搜索指定位置,每种方法给大家介绍的非常详细,需要的朋友可以参考下
在python的使用过程中我们会发现,如果编程的格式为一种形式,而我们输入的格式又是另外一种格式,那么这种情况下是不能计算的,要转换成相同格式才可以。
在本文中,云朵君将和大家一起了解装饰器的工作原理,如何将我们之前定义的定时器类 Timer 扩展为装饰器,以及如何简化计时功能,感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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