[英]How to evaluate the path to a python script to be executed within Jupyter Notebook
Note: I am not simply asking how to execute a Python script within Jupyter, but how to evaluate a python variable which would then result in the full path of the Python script I was to execute. 注意:我不是简单地问如何在Jupyter中执行Python脚本,而是要如何求一个python变量的值,这将导致我要执行的Python脚本的完整路径。
In my particular scenario, some previous cell on my notebook generates a path based on some condition. 在我的特定情况下,笔记本上的某些先前单元会根据某种条件生成一条路径。
Example on two possible cases: 两种可能情况的示例:
script_path = /project_A/load.py
script_path = /project_B/load.py
Then some time later, I have a cell where I just want to execute the script. 然后过了一段时间,我有了一个只想执行脚本的单元。 Usually, I would just do:
%run -i /project_A/load.py
通常,我会这样做:
%run -i /project_A/load.py
but I want to keep the cell's code generic by doing something like: 但我想通过执行以下操作来保持单元的代码通用:
%run -i script_path
where script_path
is a Python variable whose value is based on the conditions that are evaluated earlier in my Jupyter notebook. 其中
script_path
是一个Python变量,其值基于Jupyter笔记本中先前评估的条件。
The above would not work because Jupyter would then complain that it cannot find script_path.py
. 上面的方法不起作用,因为Jupyter会抱怨说找不到
script_path.py
。
Any clues how I can have a Python variable passed to the %run
magic? 有什么线索可以将Python变量传递给
%run
魔术吗?
One hacky way would be to change the directory via %cd path
一种变通的方法是通过
%cd path
更改目录
and then run the script with %run -i file.py
然后使用
%run -i file.py
运行脚本
E: I know that this is not exactly what you were asking but maybe it helps with your problem. E:我知道这并不是您要问的,但也许可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.