调用其他python脚本文件里面的类和方法过程解析
作者:He_LiangLiang 时间:2021-01-11 13:27:14
这篇文章主要介绍了调用其他python脚本文件里面的类和方法过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
问题描述:
自己编写了若干个Python脚本。
在testC.py里面需要调用testA.py和testB.py里面的若干类和方法。要怎么办?
需要都打包、安装,再去调用吗? 其实不必那么麻烦。
这里有个前提,testA.py, testB.py, testC.py在同级目录下。
如果不在同级目录,后面会补充介绍如何把路径包含过来。
# testA.py
# -*- coding: utf-8 -*-
class testA:
def testA1():
print("----testA1")
def testA2(str):
print("testA2---: " + str)
# testB.py
# -*- coding: utf-8 -*-
def testB():
print("this is testB")
# testC.py
import logging
from testA import *
from testB import *
logging.basicConfig(level=logging.INFO, filename='mylog.log')
logging.info('Starting program')
# 这个logging仅仅为了掩饰日志记录功能,和这里讨论的主题无关
logging.info("test testA.py")# 调用里面的类
testa = testA
testa.testA1()
# 调用里面的方法
testA2("How are you?")
logging.info("test testB.py")
testB()
logging.info('Ending program')
这里有3个文件(testA.py, testB.py, testC.py)。
在testC.py里面调用另外连个.py脚本的方法就是 import 模块脚本的全部内容。
from testA import *
from testB import *
函数调用语法细节,请参看testC.py里面的代码。
遗留问题:
如果不在当前路径怎么办?
用sys模块,将路径添加进来即可。
例如,我这里就把testA.py放在了当前目录的today文件夹下面。把testB.py放在了父级目录(上一级目录)的yesterday文件夹下面。
import sys
sys.path.append(r'./today')
sys.path.append(r'./../yesterday')
from testA import *
from testB import *
来源:https://www.cnblogs.com/music-liang/p/11863023.html
标签:调用,python,脚本,类,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python删除列表中重复元素的七种方法举例
2021-08-16 06:10:15
PyTorch CNN实战之MNIST手写数字识别示例
2021-09-06 15:20:21
![](https://img.aspxhome.com/file/2023/5/79575_0s.jpg)
基于Python实现的微信好友数据分析
2021-07-26 20:27:54
![](https://img.aspxhome.com/file/2023/5/103505_0s.jpg)
Tensorflow与RNN、双向LSTM等的踩坑记录及解决
2021-04-29 21:25:55
![](https://img.aspxhome.com/file/2023/2/81492_0s.png)
用Dreamweaver MX巧妙格式化表格
2008-03-18 16:39:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113211174s.jpg)
python 产生token及token验证的方法
2023-05-19 17:25:13
PHPMyadmin2.10中文显示为乱码的解决办法
2007-08-22 08:18:00
FSO组件之驱动器操作asp源码
2010-05-03 10:59:00
浅谈Pandas Series 和 Numpy array中的相同点
2022-06-11 15:20:17
网站更新短平快
2007-02-03 11:39:00
Python Pygame实战之超级炸弹人游戏的实现
2023-07-24 00:56:11
![](https://img.aspxhome.com/file/2023/3/120353_0s.jpg)
Python Pandas读写txt和csv文件的方法详解
2022-08-05 17:06:31
![](https://img.aspxhome.com/file/2023/4/112594_0s.png)
django中模板的html自动转意方法
2023-06-28 15:33:49
记一次pyinstaller打包pygame项目为exe的过程(带图片)
2023-12-29 12:45:19
![](https://img.aspxhome.com/file/2023/7/124127_0s.png)
python 执行函数的九种方法
2021-03-10 18:12:08
用SQL语句删除重复记录的四种方法
2011-05-03 09:25:00
Pycharm基本操作及调试代码
2022-08-13 08:38:55
![](https://img.aspxhome.com/file/2023/8/76038_0s.jpg)
python实现图片变亮或者变暗的方法
2023-02-20 03:59:00
![](https://img.aspxhome.com/file/2023/9/115419_0s.jpg)
oracle中的视图详解
2009-12-22 11:48:00
Pytorch DataLoader 变长数据处理方式
2022-08-06 23:07:50