Home About Archive RSS

Use the Python Shell or REPL from VSCode

Today, one of my students asked how to use the Python shell from VSCode. It is possible to do manually by navigating to the directory of the python file you are working with, writing python and pressing return to enter the shell, then write import filename (without .py extension) and then you may interact with the code. If you import this way, you have to use filename.function() or filename.variable to interact with the code. The other way is to specify what you want to import, for example, from oblig23 import alder. It is doable, but as I wrote in an earlier blog post, the Python shell integration in other IDEs and text editors are much more convenient to work with since you don't have to manually load the python shell from another shell inside a terminal.

So I used some time today to look into if there is better integration with the Python Shell hidden somewhere in VSCode or the Python Extension. After a rather long looking around, I concluded that there isn't. (Later on, I found out that if you don't mind using another implementation of Python than CPython, there is an ability to use a Jupyter kernel interactively.) There is, however, a slightly more convenient way to do this with the python interpreter in a terminal than first entering the shell and then importing. If you write python -i filename (with .py) when you are in the directory where the file is (or write the full path if you are not), you get the python shell with all the classes, functions and variables defined in the file imported. If you change the code, then you have to save and import again, either by leaving the shell and using python -i filename again or by staying in the shell and writing from x import y.

© Einar Mostad 2010 - 2025. Content is licensed under the terms of CC BY SA except code which is GNU GPL v3 or later.