1

In python_test.py file, I've inserted :

def my_contains(elem, lst):
    return elem in lst
def my_first(lst):
    return lst[0]

import IPython
IPython.embed()

After having executed python3 python_test.py, I got :

Traceback (most recent call last):
  File "python_test.py", line 6, in <module>
    import IPython
ImportError: No module named 'IPython'

In fact, I'd like if the shell could stay open after having executed my code in such a way that I could test that code. Could anyone be able to help me at this point?

Maroun
  • 94,125
  • 30
  • 188
  • 241

1 Answers1

0

From the man page:

-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception.

I tested your code as follows and it worked for me:

python3 -i python_test.py
>>> my_first([3, 2, 1])
3
>>> my_contains(2, [1, 10, 100])
False
>>> my_contains(1, [2, 1, 3])
True

This answer is copied almost verbatim from https://stackoverflow.com/a/5280210/7554621

As for IPython, it seems that it cannot find the module which means it has not been installed, was installed incorrectly, doesn't know where to find it, the name is incorrect, or its being used incorrectly. I looked into installing it here http://ipython.readthedocs.io/en/stable/install/index.html.

After installing it, I simply used ipython instead of python3 to load the file and enter an interactive shell:

@WillemVanOnsem made a good point in the comments above that pip3 may need to be used instead of pip. For me, pip worked fine and installed correctly.

pip install ipython
ipython -i python_test.py
Python 3.5.1 (default, Apr 18 2016, 11:46:32)

In [1]: my_contains(1, [1, 2, 3])
Out[1]: True

In [2]: my_first([1, 2, 3])
Out[2]: 1
Community
  • 1
  • 1
Joaufi
  • 16
  • 3