Skip to content

Python and Jupyter Notebooks in Visual Studio Code

etortorici edited this page Dec 4, 2024 · 3 revisions

Visual Studio Code should automatically be able to locate your Anaconda virtual environments. On Windows, Ctrl+Shift+P will open the search bar at the top with "Select Python Interpreter" as an option. This will allow you to select which environment you wish to use. Your Anaconda environments should automatically be detected. Other virtual environments (created with venv) can be located with the "Enter interpreter path..." option.

You can create Python scripts or Jupyter Notebooks by right-clicking in the Explorer tab and selecting "New File...", if you end with ".py" the result will be a script, and if you end with ".ipynb" the result will be a Notebook. You can also make LaTeX (.tex), CSV (.csv), text files (.txt), etc. this way.

Jupyter Notebooks

Notebooks are a nice tool for running snippets of Python code, and presenting that code nicely using Markdown cells. Here is a quick guide for using Markdown cells. Variables defined in cells can be called in any cell, and are always stored in RAM. The Python environment used for the kernel is shown in the top right corner of the window. If no environment is selected, it will say "Select Kernel" instead.

As long as the Jupyter extension is installed, Visual Studio Code will automatically know how to handle Jupyter Notebooks. If you don't have the ipykernel package installed, you will be prompted to install it: just select "Install" if prompted.

Python Scripts as Modules

Python scripts can be imported in other Python scripts or in Jupyter notebooks. For example, if you create a script "my_functions.py" that looks like this

def function1(x, y):
    return (x * y) ** (x + y)

def function2(x, y, z):
    return (x + y) ** z

Then in any other script or Notebook in the same directory, you can import and use these functions, like this:

import my_functions as mf

result1 = mf.function1(1, 2)
result2 = mf.function2(3, 2, 1.4)

or

from my_functions import function1, function2

result1 = function1(1, 2)
result2 = function2(3, 2, 1.4)

It is considered bad practice to wildcard import because it makes code far less understandable. A wildcard import looks like:

from my_functions import *

result1 = function1(1, 2)
result2 = function2(3, 2, 1.4)

Python Scripts as Scripts (and/or Modules)

With a Python script open, you can run it with the triangle that looks like a "play" button in the top right corner. Often times you will be writing scripts that get imported into either other scripts or into a Jupyter Notebook. In these cases, there may be code in the script that you only want to run when the script is ran as a script, but not when it is imported. The following if-statement can be used to have code that only runs when the script is run, but is ignored when the script is imported as a module:

if __name__ == "__main__":
    # code written under this will only run
    # if you run this script
    # and will be ignored if this script is imported

sys args

The "sys" module (for system) is a really powerful tool that can be used to accomplish many things. One really common use of the sys module is to collect arguments given with the script. For example, from a terminal, you can call a script like

python my_script.py

to run the script, but you can also pass arguments into the program like this:

python my_script.py 43 1 True

The arguments can be accessed with sys.argv which is a list of arguments. If "my_script.py" was

if __name__ == "__main__":
    import sys
    print(sys.argv)

The output would be

["my_script.py", "43", "1", "True"]

So sys.argv stores all the arguments passed after the python command. These can be used as inputs for a program.

argparse

The argparse module can also be used for making more sophisticated programs. Here is a tutorial: https://docs.python.org/3/howto/argparse.html

The terminal

The terminal can be opened with Ctrl+backtick (top left key of the keyboard above the Tab key). This should open a prompt or terminal with the selected virtual environment activated. If not, you can delete the terminal with the trashcan button and open a new one. From the terminal, you can install packages with pip or conda, run Git commands, and run Python scripts with sys args.

Clone this wiki locally