Search code examples
pythoneclipsepycharmpydevspyder

[Python IDE]Call a function defined in the .py file in console


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.


Solution

  • 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