4

If you are familiar to Matlab you know that you are able to use any defined function from its definition file, if it is in the workspace path, you do not need to call or import.
Is there any mechanism in ipython mimics that behaviour of matlab. My current work flow is to write the code in the Sublime text and paste it to ipython (seems stupid). Would you suggest any other way to achieve efficient

I am too lazy to do so but writing a periodic auto import code in the startup file of Ipython in some way might work. Maybe ipython curators would consider this.

erogol
  • 13,156
  • 33
  • 101
  • 155
  • +1. This appears to be the top inconvenience of Python as compared to Matlab. – A. Donda Feb 23 '14 at 14:45
  • The key difference is that (traditional) Matlab has a 1 to 1 match between file and function. In Python a file contains a module, which can have many functions. For interactive work on small projects the Matlab approach is convenient. But it makes managing large projects harder. Over the years Mathworks (and Octave) has loosened that 1-1 correlation in various ways. – hpaulj Feb 23 '14 at 17:47
  • @Erogol, have a look at [ipython notebook](http://ipython.org/notebook.html). It is closer to the interactive working style one is used to from Matlab, but at the same time much more powerful than the Matlab console. – A. Donda Aug 28 '14 at 18:51

3 Answers3

1

I guess you'll have to reimport your module on every code change. And you could use from my_module import * to avoid module name before every function call, though this construction works slowly.

roboslone
  • 2,847
  • 3
  • 20
  • 28
0

If you're running the Python interpreter in your workspace, you can import the file:

/path/to/workspace$ ls
some_files    youtube.py     other_files

/path/to/workspace$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
Type "help", "copyright", "credits" or "license" for more information.
>>> import youtube
# I know there's a method get_video_thumbnail in my file
>>> youtube.get_video_thumbnail('http://youtu.be/IAooXLAPoBQ')
'http://img.youtube.com/vi/IAooXLAPoBQ/hqdefault.jpg'
>>> youtube.NO_PREVIEW
'/static/img/no_preview.png'
>>> # etc.

If you edit the Python file, you can re-import it, it will overwrite the module namespace and its content in the interpreter.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • but in that case the changes in source file does not effect the interpreter in real time. Most probably, I need to re-import the module. – erogol Feb 23 '14 at 14:30
  • @Erogol Yes, you need to write the `import` again. But I don't have any better solution in mind right now :) – Maxime Lorant Feb 23 '14 at 14:34
0

I'm surprised no one has suggested using the %edit magic function.

From another answer I gave on the same topic, if you setup Sublime to be your editor, you can simply run:

In [72]: %ed somefile.py

After you save & close, IPython will automatically execute the code contained in the file - so instead of copy/paste, you just let IPython do its magic.

I've never used Sublime, so I don't know exactly how that would work. As an alternative, you could look at how LightTable communicates with IPython, and write a Sublime extension for that.

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290