在python中实现导入一个需要传参的模块
作者:哦摩西罗伊 时间:2022-04-08 18:48:24
最近跑实验,遇到了一个问题:
由于实验数据集比较多,每次跑完一个数据集就需要手动更改文件路径,再将文件传到服务器,再运行实验,这样的话效率很低,必须要专门看着这个实验,啥时候跑完就手动修改运行下一个实验。我个人无法忍受这样低效率,就想能不能有什么解决的办法。
我们期望的解决办法是通过命令行传参来解决这个问题,因为接下来是需要编写shell脚本来批量运行实验,如果用输入语句的方式显得太笨拙。
在编写实验代码的时候,我将所有的参数集中到一个py文件中,这样便于后期的维护,现在的问题就是需要通过命令行传参的方式改变该文件中的某些值。
关于传参,python中提供了argparse这一模块。通过这个模块 就能很快的实现命令行传参的功能。
下面举个例子:
文件test1.py
import argparse
from argparse import RawTextHelpFormatter
parse = argparse.ArgumentParser(description="The parameters for the feature select method", formatter_class=RawTextHelpFormatter)
parse.add_argument('name', type = str)
args = parse.parse_args()
name = args.name
文件test3.py
import test1
str = test1.name + 'asdasdasd'
文件test2.py
import test3
print test3.str
运行:
我们可以通过改变命令行的参数来改变这个name的值,其实关于这一点也比较好理解,我们可以想成python将这三份代码拼接在一起,再一起执行。
这样就实现了我刚才想要的功能。
补充:python调用模块时传参出错的解决方案
首先定义了一个Login模块
传参是两个:username,password
#coding=utf-8
def login(self,username,password):
driver=self.driver
self.driver.implicitly_wait(30)#不加等待会出错
driver.switch_to.frame(0)
driver.find_element_by_class_name("dlemail").clear()
driver.find_element_by_class_name('dlemail').send_keys("username")
driver.find_element_by_class_name('dlpwd').clear()
driver.find_element_by_class_name('dlpwd').send_keys("password")
driver.find_element_by_id("dologin").click()
def logout(self):
driver=self.driver
driver.find_element_by_link_text(u"退出").click()
driver.quit()
在Test.login中调用该模块:
#coding=utf-8
from selenium import webdriver
import unittest,time
from public import Login
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.126.com/"
self.verificationErrors = []
def test_login(self):
driver=self.driver
driver.get(self.base_url)
Login.login(self,"jinbian3333","jinbian76")#传参是这两个
text=driver.find_element_by_id("spnUid").text
self.assertEqual(text,"jinbian3333@126.com")
Login.logout(self)
def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)
if __name__ == '__main__':
unittest.main()
可是运行后发现出错:
明明传参是"jinbian3333","jinbian76",为什么登录信息是图中呢?
回头查看被调用模块,
按我理解就是 加了" " 就不是变量了,是常量了, 你调用了该模块,即使传参 它还是按照模块本身的设置:“username”"password"运行
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://www.cnblogs.com/mlgjb/p/10739112.html
标签:python,导入,传参,模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Python实时检测CPU和GPU的功耗
2021-06-11 17:05:58
![](https://img.aspxhome.com/file/2023/8/101138_0s.png)
ASP/php/aspx生成静态网页的方法
2007-10-25 12:01:00
python pandas库的安装和创建
2023-09-25 22:49:02
![](https://img.aspxhome.com/file/2023/7/98827_0s.png)
python中设置超时跳过,超时退出的方式
2023-12-25 01:38:20
SQL server不支持utf8 php却用utf8的矛盾问题解决方法
2023-07-20 12:01:28
Go项目配置管理神器之viper的介绍与使用详解
2023-09-17 01:05:52
![](https://img.aspxhome.com/file/2023/1/89021_0s.jpg)
Python如何向SQLServer存储二进制图片
2021-01-06 08:23:54
Python pygame项目实战英雄动画特效实现
2021-02-22 21:07:50
![](https://img.aspxhome.com/file/2023/0/70760_0s.png)
使用python爬取微博数据打造一颗“心”
2022-05-28 22:30:09
![](https://img.aspxhome.com/file/2023/3/103653_0s.png)
在python image 中安装中文字体的实现方法
2021-08-06 06:19:35
使用Python处理KNN分类算法的实现代码
2023-11-03 07:03:07
![](https://img.aspxhome.com/file/2023/4/63244_0s.png)
超轻量级MSXML多功能测试程序
2009-05-19 12:38:00
![](https://img.aspxhome.com/file/UploadPic/20095/19/20090513224141384-92s.gif)
浅谈ASP自动采集程序及入库
2007-08-17 11:25:00
Python内置方法和属性应用:反射和单例(推荐)
2022-08-04 03:23:48
np.concatenate()函数的具体使用
2023-06-21 11:15:19
960网格系统
2009-02-17 12:22:00
![](https://img.aspxhome.com/file/UploadPic/20092/17/960-grid-system-092-54s.jpg)
扩展性很好的一个分页存储过程分享
2011-11-03 17:04:16
简单谈谈Python中的模块导入
2021-02-24 20:28:17
Python抖音快手代码舞(字符舞)的实现方法
2023-11-08 14:12:23
![](https://img.aspxhome.com/file/2023/5/107285_0s.gif)
python 读取竖线分隔符的文本方法
2023-03-12 14:45:14