160

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.

Chris
  • 1,416
  • 18
  • 29
Tyler Durden
  • 1,611
  • 2
  • 11
  • 3
  • `import ` is essentially the same as `exec()` in JavaScript or Perl. – Vortico Jul 31 '12 at 17:52
  • 18
    +1 for the syntax %run abc.py -- was trying to look for that – Sid Kshatriya May 03 '13 at 09:40
  • 1
    you can use the path: %run ../scripts/my_scripts for example – Jose A. Martín Sep 13 '13 at 08:52
  • for CLI scripts developed with the Click library it doesn't work to me, I get: IOErrorTraceback (most recent call last) /home/miguelfg/workspace/projects*********/db_preparation.py in () 1270 1271 if __name__ == "__main__": -> 1272 main() 1273 1274 ... – miguelfg Apr 27 '17 at 11:35

6 Answers6

166

from within the directory of "my_script.py" you can simply do:

%run ./my_script.py
rakke
  • 6,819
  • 2
  • 26
  • 28
32

How to run a script in Ipython

import os
filepath='C:\\Users\\User\\FolderWithPythonScript' 
os.chdir(filepath)
%run pyFileInThatFilePath.py

That should do it

CubeBot88
  • 1,080
  • 11
  • 14
16

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

Community
  • 1
  • 1
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
  • 1
    That is also what I thought. However, in practice modules can be imported but scripts are not found by the interpreter unless started from the directory they belong to. – Tyler Durden Jul 31 '12 at 17:59
  • 1
    Read http://docs.python.org/tutorial/modules.html#the-module-search-path for more info. It details where it must be put for python to find the file, More specifically, many modules can double as scripts using the "if `__name__` == '`__main__`':" line at the end – Snakes and Coffee Jul 31 '12 at 18:06
16

The %run magic has a parameter file_finder that it uses to get the full path to the file to execute (see here); as you note, it just looks in the current directory, appending ".py" if necessary.

There doesn't seem to be a way to specify which file finder to use from the %run magic, but there's nothing to stop you from defining your own magic command that calls into %run with an appropriate file finder.

As a very nasty hack, you could override the default file_finder with your own:

IPython.core.magics.execution.ExecutionMagics.run.im_func.func_defaults[2] = my_file_finder

To be honest, at the rate the IPython API is changing that's as likely to continue to work as defining your own magic is.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
7

Not exactly the answer to your question, but you can drop into ipython at the end of a script's execution by using the -i parameter to ipython:

ipython -i my_script.py

At the end of the script you're dropped into the ipython prompt with the script's variables available to you, just like python -i.

kristianp
  • 5,496
  • 37
  • 56
-3

for Python 3.6.5

import os
os.getcwd()
runfile('testing.py')
Evhz
  • 8,852
  • 9
  • 51
  • 69
Alinafe
  • 18
  • 1