1

Is it possible to configure PyDev-in-Eclipse or PyCharm in order to be able to call a function defined in the editing .py file in the console ?

For example, in the editor, there is a test.py open:

def add(x, y):
    return x+y

Then in the console:

>> add(3,4)

I noticed that it is possible in Spyder. However, by default, in PyCharm and PyDev, the console will return "No name 'add' defined" error. My question is: is it possible to achieve this interaction between editor and console in PyCharm or PyDev ?

All answers and suggestions are appreciated.

TristeShine
  • 79
  • 1
  • 2
  • 10
  • Not an answer but a suggestion. If you debug the program in pycharm you can view the output of functions by clicking the evaluate expressions button or alt+F8. Alternatively you can copy past the function into the console and then you should be able to call it. – Albert Rothman Nov 17 '16 at 23:16
  • Possible duplicate of [How to give the Python console in PyCharm access to the variable space of a script?](http://stackoverflow.com/questions/26354977/how-to-give-the-python-console-in-pycharm-access-to-the-variable-space-of-a-scri) – ospahiu Nov 17 '16 at 23:18
  • This is not the case for Spyder, unless you're using a very old version of it (2.2.x or 2.1.x). And in that case, this was only true because you defined a function called `add` (which is part of Numpy). If you call your function `add_foo` then you'll see the same error as in PyDev and PyCharm. – Carlos Cordoba Nov 18 '16 at 18:39
  • @CarlosCordoba,Yes, you're right ! Thank you ! I am sorry for this stupid question. – TristeShine Nov 21 '16 at 00:24

2 Answers2

1

In PyDev, use Ctrl+Alt+Enter to make a runfile of the current editor in the console (if no text is selected), so that its symbols are available for further experimentation (and it may also be used to open a console if there's no open console).

See: http://www.pydev.org/manual_adv_interactive_console.html for more details on how to properly use the interactive console in PyDev.

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
0

In any IDE to my knowledge, if you are running from the same directory as the file, it should be as simple as

import test

test.add(3,4)

or

from test import add

add(3,4)

What is your ultimate goal though? python packaging is a bit more complex if you are looking to distribute or use elsewhere in your code.

see - https://docs.python.org/2/tutorial/modules.html

Vincent Buscarello
  • 395
  • 2
  • 7
  • 19