I would like to use the %run magic command to run a script in a directory which is in the pythonpath variable. The script reads some files in the working directory. However, when I try to run the script using the command: %run "testscript_in_pythonpath.py ", it returns an error. I thought files in pythonpath would be accessible to the interpreter, no ??
1 Answer
(Reposting as an answer)
$PYTHONPATH is what Python uses to look up modules to import, not scripts to run.
To run a file from $PYTHONPATH, you can do import testscript_in_pythonpath. Or, in IPython:
%run -m testscript_in_pythonpath
The difference is that if the file has an if __name__ == '__main__': section, %run will trigger that.
From a system shell, you can do the same thing as:
python -m testscript_in_pythonpath
%run -m testscript_in_pythonpathto run it from its module name.