1

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 ??

2
  • I just used the command: "import testscript_in_pythonpath". The script executed as normal although an "ImportError" was thrown saying no module was found. However, the command served the purpose. Now if there was a better way for doing this, it would be just fine! Commented Feb 8, 2013 at 17:48
  • Pythonpath is for libraries, not scripts. You can %run -m testscript_in_pythonpath to run it from its module name. Commented Feb 8, 2013 at 18:56

1 Answer 1

3

(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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.