Running python script inside ipython

Is it possible to run a python script (not module) from inside ipython without indicating its path? I tried to set PYTHONPATH but it seems to work only for modules. I would like to execute

%run my_script.py

without being in the directory containing the file.


从“my_script.py”的目录中,您可以简单地执行以下操作:

%run ./my_script.py

How to run a script in Ipython

import os
filepath='C:UsersUserFolderWithPythonScript' 
os.chdir(filepath)
%run pyFileInThatFilePath.py

That should do it


In python there is no difference between modules and scripts; You can execute both scripts and modules. The file must be on the pythonpath AFAIK because python must be able to find the file in question. If python is executed from a directory, then the directory is automatically added to the pythonpath.

Refer to What is the best way to call a Python script from another Python script? for more information about modules vs scripts

There is also a builtin function execfile(filename) that will do what you want

链接地址: http://www.djcxy.com/p/54836.html

上一篇: Python单元测试导入失败

下一篇: 在ipython内运行python脚本