[英]Calling and Running Another python script and running all the code from it
I am writing a python script on jupyter where the first script: a.ipynb has the majority of the analysis within, and multiple other scripts, which include the data cleaning [b.ipynb, c.ipynb, d.ipynb etc]. 我在jupyter上编写了一个python脚本,其中第一个脚本:a.ipynb具有大部分分析内容,还有多个其他脚本,包括数据清理[b.ipynb,c.ipynb,d.ipynb等]。 I am trying to call the data cleaning scripts [b.ipynb, c.ipynb, d.ipynb etc] into a.ipynb, but it keeps throwing errors. 我正在尝试将数据清理脚本[b.ipynb,c.ipynb,d.ipynb等]调用为a.ipynb,但它始终会引发错误。
I have tried some various different ways to read the data but these all are throwing errors: 我尝试了几种不同的方式来读取数据,但这些方式均会引发错误:
subprocess.getoutput([sys.executable,'d.ipynb'])
Error: "execution_count": null,\nNameError: name \'null\' is not defined'
b_checks = open("d.ipynb", 'r').read()
exec(b_checks)
---> 21 exec(b_checks)
NameError: name 'null' is not defined
exec(open('d.ipynb').read())
NameError: name 'null' is not defined
Currently, I am conditionally calling the scripts based on variable value as the data is imported from SQL and has multiple different properties prop
目前,我呼吁有条件基于变量值的脚本,数据从SQL导入并有多个不同的属性prop
# a.ipynb
prop = 'y'
#function to call data: checks
b_checks = checks(dbcon,prop)
if prop == 'w':
b_checks = subprocess.getoutput([sys.executable, 'b.ipynb'])
elif prop =='x':
b_checks = subprocess.getoutput([sys.executable, 'c.ipynb'])
elif prop == 'y':
b_checks = subprocess.getoutput([sys.executable,'d.ipynb'])
elif prop == 'z':
b_checks = subprocess.getoutput([sys.executable, 'e.ipynb'])
and the script I am trying to call at the moment is d.ipynb
which includes the data cleaning of a certain column. 而我现在要调用的脚本是d.ipynb
,其中包括特定列的数据清理。
#d.ipynb
def replace_server(to_r, server):
return b_checks['Server'].str.replace(to_r, server)
b_checks['Server'] = b_checks['Server'].str.upper()
b_checks['Server'] = b_checks['Server'].str.strip()
b_checks['Server'] = replace_server('xxxx', 'x')
b_checks['Server'] = replace_server('123', 'x')
(there are more lines within this script) (此脚本中有更多行)
My Current output of b_checks in the uncleaned dataset xxxx, 123
, but the output I am hoping for is to run the entire second script d.ipynb
so that the first script a.ipynb
updates the variables in the b_checks table 我当前在未清理的数据集xxxx, 123
的b_checks输出,但我希望输出的是运行整个第二个脚本d.ipynb
以便第一个脚本a.ipynb
更新b_checks表中的变量
it's a good practice to make a kind of API in another notebook: say you have a func() in b.ipynb, and you need to call it from a.ipnyb, then 'a' notebook should contain: 在另一个笔记本中创建一种API是一个好习惯:假设您在b.ipynb中有一个func(),并且需要从a.ipnyb中调用它,那么“ a”笔记本应包含:
%run ./b.ipynb
func()
I can not recommand you enough to store all this functions in real python scripts. 我不能推荐您足够将所有这些功能存储在真实的python脚本中。 Let's say you have 3 Notebooks: a.ipynb
, b.ipynb
and c.ipynb
假设您有3个笔记本: a.ipynb
, b.ipynb
和c.ipynb
If you want in c
to use functions stored in a
or b
, copy and pasted the functions (and all the imports needed) and save them in a.py
, b.py
如果要在c
中使用存储在a
或b
的函数,请复制并粘贴函数(以及所有需要的导入)并将其保存在a.py
, b.py
you can then easily from a.py import funct_1, funct_2
然后,您可以轻松地from a.py import funct_1, funct_2
Their might be some solution to actually import functions directly from notebooks but i do not recommend that 他们可能是一些直接从笔记本电脑直接导入功能的解决方案,但我不建议这样做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.