Editors / IDE’s#

IDE’s is an engineers integral work-horse for creating and executing code. Any code, in any language, can be written in any text-editor. However, integrated development environments (IDE’s) will extend the editor functionality and aid the programmer in various ways, either by creating tooltips when typing code or spell checking, and many other things. Therefore choosing and becoming comfortable with an IDE can greatly improve the turn around for the developer.

Note

It is recommended that you use VS Code when beginning your studies at DTU. This will be the default IDE for all Polytechnical Foundation courses.

If you later wish to play around with other IDE’s, feel free to do so. They are all similar, and different at the same time.

Please note if course teachers recommends a specific IDE.

Visual Studio Code #

Download + Install VS Code

Download and install VS Code.

Add extensions to VS Code

Add extensions to enable additional features.

Ensure correct Python interpreter

Manually change the interpreter, mainly required when having multiple Python installations.

VS Code is a an open-source coding framework with a big community and many users. It can be used together with any other coding language or be used to edit configuration files (YAML, TOML, …).

It can be downloaded here.

Once installed it is vital to ensure the Python interpreter is pointing to the correct Python executable. This depends on whether you are using virtual environments, see here. Changing the interpreter location can be done by following this instruction.

User Interface#

VS Code interface - explaining the different regions of the editor; Menu, Editor, Run/Debug, Panel and Activity Bar

Editor: The main editor is where you write and edit your code or text files. It’s the central area of VS Code where you will spend most of your time.

Activity bar: The sidebar, located on the left side of the interface, provides quick access to various functionalities and information about your project.

Panel: An additional space for views below the editor. By default, it houses output, debug information, problems, and an integrated terminal.

Menu bar: You can use this to open files and folders, and more.

Run and debug: Pressing this button will run the file which is currently open in the editor.

Panel#

The Panel, located at the bottom of the interface (see above), consists of several important tabs:

Problems: Displays a list of coding errors, warnings, and messages, helping you identify and fix issues in your code.

Output: Shows output messages generated by your code, extensions, or tasks, providing valuable debugging and diagnostic information.

Debug Console: A console for interactive debugging, where you can input commands and view program output while debugging your code.

Integrated Terminal: Offers a command-line interface within Visual Studio Code, allowing you to run terminal commands and scripts directly from the editor.

Activity Bar#

Activity bar selectors -- Explorer, Run and debug and Extensions tabs explained

The Activity bar, located on the left side of the interface (see above), provides quick access to various functionalities and information about your project. The most used tabs in the activity bar is the Explorer tab, Run and debug tab and the Extensions tab.

Explorer: allows management of your project’s files and folders. It provides a file tree view of your project’s directory structure.

Run and debug: is essential for debugging your code and running your applications within VS Code.

Extensions: an App store equivalent for VS Code; it is your gateway to customizing and extending the functionality of VS Code through various extensions. VS Code is an extensible and versatile development environment. The extensions can greatly aid during the coding of varying problems. Some notable highlights include automatic API messages for methods, and integration with Jupyter notebooks.

Getting Started with Python in VS Code#

In the following we will prowide a step-by-step guide on how to get started with Python in VS Code. This includes creating a new project, working with files, importing packages, running your code, installing requirements.txt, debugging, virtual environment (venv).

Installing the python extension#

Before you can start working with Python in VS Code you have to install the Python extension. You can do this by following these steps in the extension guide (see here).

Creating and Opening Projects#

  1. Create a folder for a project. You can do this by creating a folder e.g. on the desktop.

  2. Open the project by clicking on File on the menu bar and then clicking on Open Folder… (see below) then navigate to your project folder.

    How to open a folder, select :guilabel:`File -> Open Folder...`
  3. Create a folder inside your project by clicking on the second most left icon (see below).

    How to create a folder, select :guilabel:`Explorer (Activity Bar) -> Folder+ (Icon)`

Working with Files#

  1. Create a new file inside your project by clicking on the left icon (see below) and e.g. naming it new_file.py - the ending .py makes it a python file. If you want to create a new file inside a folder in your project, make sure to click on the folder before creating the file.

    How to create a file, select :guilabel:`Explorer (Activity Bar) -> File+ (Icon)`
  2. Save the file by clicking on File -> Save (see below), or by pressing Ctrl+S (Command+S on MacOS). If you want autosave to be enabled, click on File -> Autosave, and make sure it is checked.

    How to save a file, select :guilabel:`File -> Save`, or press :kbd:`Ctrl+S` / :kbd:`Command+S` (Windows/Mac)
  3. To move a file to a folder simply click on the file and hold, then drag the file to the folder.

Importing packages#

In order to write your code you can benefit from a bunch of different python-packages that will help you with your calculation. In Python these packages typically end with the letters py. For example there is sympy, which will help you with caluclating with variables (or symbols), numpy which is used for numerical calculations and matplotlib, which is used for plotting your data. You will typically import the packages you want to use in the top of your project so that you can use them throughout the following code.

  1. Open a new project.

  2. There are several different ways of importing packages. We will here show you some examples:

import <name of the package>

This is the simplest way of importing packages. In order to call functions from the package you have to write the name of the package, followed by . and then the function you want to use (for example numpy.sqrt(2)).

import <name of the package> as ...

If you don’t want to write the whole name of the packages every time you use a command from them, you can define a shorter name for them yourself, that you want to call them. Typically one will write import numpy as np instead of import numpy or import sympy as sp instead of import sympy, so that you don’t have to write the entire name every time you want to call a function from that package (for example np.sqrt(2)).

from <name of the package> import *

You can use this command if you want to import the whole package without having to write the name or the abbreviation of the package in front of the function. It already knows that you want to import the function from this package. The * indicates that you imported everything from this package. For example one can write from sympy import * and then create a matrix by writing Matrix([[... , ...],[... , ...]]) instead of writing sp.Matrix([[... , ...],[... , ...]]) or even sympy.Matrix([[... , ...],[... , ...]]).

Warning

This can cause side-effects, use with care if doing this with multiple packages.

from <name of the package> import <name of function>

You can use this command if you only want to import specific functions from the package. This will allow you to import only the functions that you are using. For example the command from matplotlib import pyplot will import only the function pyplot from the package matplotlib. You can then for example call the function pyplot.plot(x,y).

import <name of the package>.<name of function> as ...

This command is helpful if you again only want to import specific functions but you also want to call them something else (often times something shorter). If you for example run the command import matplotlib.pyplot as plt you will import the exact same function as in the example before but you don’t have to write as much in order to call the function: plt.plot(x, y).

Running your code#

There are several ways to run your code in VS Code. We will here show you some examples:

  1. Create a new file (see above).

  2. Write your code in the file. For example:

    import numpy as np
    print(np.sqrt(2))
    
  3. Save the file (see here).

  4. Run the file by clicking on the Run and Debug (see above).

  5. You can also run the file through the terminal (see here).

  6. Lastly, you can run the file by binding a key to the command (see here).

Install requirements.txt#

The requirements.txt file is a text file that contains a list of packages that you want to install. It is an easy and convenient way to install all the packages you need for your project. To install a requirements.txt file you have to do the following:

  1. Navigate into the folder from where you want to access the packages (see Getting Started with Python in VS Code).

  2. Open up your terminal. You can then copy the following command into your terminal and press enter to execute:

    python -m pip install -r requirements.txt
    
    python -m pip install -r requirements.txt
    
    python3 -m pip install -r requirements.txt
    
    python3 -m pip install -r requirements.txt
    

The installation of the packages will take a few minutes. After it is done you will se the path of the folder you are in followed by an %.

Note

If you are using a virtual environment, make sure to activate it before installing the packages. You can read more about virtual environments here.

Debugging#

Debugging is the process of finding and fixing errors within a script. VS Code provides a powerful debugging tool that allows you to set breakpoints, step through code, and inspect variables.

  1. Set a breakpoint, click on the left of the line number where you want to pause the execution of your code. A red dot will appear to indicate that a breakpoint has been set, it is possible to set multiple breakpoints in your code. To remove a breakpoint, click on the red dot again.

  2. Start a debugging session, click on the Run and Debug tab in the sidebar and select Run and Debug. This will open the debug panel at the top of the screen and start the debugging session.

    ../_images/Vscode-Debugging.png

The buttons in the debug panel allow you to control the execution of your code. From left to right the buttons are:

Continue: Continue running the code until the next breakpoint is reached.

Step over: Execute the next line of code.

Step into: Execute the next line of code, if the next line is a function call, step into the function.

Step out: Execute the remaining lines of the function and return to the line where the function was called.

Restart: Restart the debugging session.

Stop: Stop the debugging session.

  1. Follow the variables in your code and their values on the left.

    ../_images/Vscode-Debugging2.png

Virtual environment (venv)#

Virtual environments are isolated spaces in computing to manage software dependencies separately. They prevent conflicts, ensure project portability, and maintain system stability. You can read more about virtual environments here.

The interpreter is the program that executes the code you write. It is important to ensure that the interpreter is pointing to the correct Python executable (environment). To change the interpreter location open a python file and click on the Python version in the bottom left corner of the screen (see below).

../_images/Vscode-interpreter.png

Extensions#

  1. To install an extension click on the extensions tab in the activity bar (see below).

  2. Then search for the extension you want to install and click on install.

    ../_images/Vscode-Extensions.png

Extensions can also be installed by following these instructions.

Customization#

VS Code has a lot of options that allows you to tailor it towards your individual preferences. These options can be found by pressing the cogwheel button in the lower left corner.

../_images/Vscode-cogwheel.png

Themes: One thing that you might want to experiment with is the different themes that VS Code has to offer. These themes change the color coding of VS Code and code that you have implemented. By either using the keybinding Ctrl+K+T or pressing the cogwheel button and navigating to the option color theme, VS Code will bring up a palette of different themes that you have installed in your IDE.

New color themes can be acquired by navigating to Extensions under the Activity bar, and then searching for the color theme that you want to install. The library of color themes that VS Code Extensions has to offer can be found by pressing here.

Key Bindings: VS Code enables you to bind a key to pretty much any action imaginable within an IDE. A majority of the actions has already been allocated as defualt, but if you want to change some of the IDE’s key bindings you can press Ctrl+K+S. This key binding brings up VS Code’s keyboard shortcut menu.

For starters, you would perhaps want to add a shortcut for running a python file. This can be done by searching run python file in the keyboard shortcuts menu, and double-clicking on Python: Run Python File. Thereafter, you can enter a personal shortcut for the functionality and finishing by pressing enter again.

../_images/Vscode-keybindings.png

For more options on key bindings press here.

Settings: For more customization you can navigate to the settings option after pressing on the cogwheel. Here you will be able to change miscellaneous settings of your VS Code like font size, cursor style etc.

Working with Jupyter Notebooks in VS Code#

We can create and work with Jupyter Notebook files in VS Code. A file in VS Code will be recognized as a Jupyter Notebook file with .ipynb.

  1. Preliminaries: For Jupyter Notebook to run in VS Code, we need to install a few Extensions. These include Jupyter, Jupyter Keymap, and Jupyter Notebook Renderers, as seen on the following picture.

    ../_images/Vscode-jupyter_extensions.png

    Note: Usually, when you install the Jupyter extension, it will also install the two other extensions. However, check when you have installed the Jupyter extension the other extensions are also installed.

  2. Creating a new Jupyter Notebook: Creating a Jupyter Notebook in VS Code is relatively simple. When you create an ordinary python file with the suffix .py, you instead write .ipynb to create a Jupyter Notebook file. Or, you can press Ctrl+Shift+P and search Create: New Jupyter Notebook. This will also create a Jupyter Notebook file in VS Code, however not in a predetermined directory.

  3. Kernels: Setting up your Jupyter Notebook: When working in a .ipynb file, you need to select a kernel for the file. Kernel is just a fancy word for the conda or virtual environment that will run your code in the cells of the notebook. Selecting a kernel can be done by either pressing Ctrl+Shift+P, selecting Python: Select Interpreter, and choosing your environment or you can press the Select Kernel button in the top right corner of the notebook (illustrated on the following picture).

    ../_images/Vscode-native-kernel-picker.png

Now you are ready to tackle your first project using Jupyter Notebooks in VS Code. However, if you are still uncomfortable with running cells or other functionalities of Jupyter Notebook, follow this link for more information.

Warning

When using virtual environments it is vital that the ipykernel Python package is installed in the environment before Jupyter Notebook can be used.

Spyder#

Warning

Spyder on MacOS with an M1/M2 processor can cause problems when not installed through conda. Therefore we recommend to use another IDE when on MacOS and M1.

Spyder is a free and open-source coding framework written in and for Python. It has intrinsic editor, IPython console and a variable explorer + many more.

It can be downloaded here.

Spyder is extensible by plugins, if needed. Generally its base installation should suffice.

Once installed it is vital to ensure the Python interpreter is pointing to the correct Python executable. This depends on whether you are using virtual environments or not. Changing the interpreter location can be done by following this instruction.

PyCharm#

PyCharm comes in two variants; a Professional (paid) version and a Community edition (free).

PyCharm focuses on a simplistic and powerful interface for code editing with Python as its core language. PyCharm community edition does not contain Jupyter notebook integration.

It can be downloaded here.

Once installed it is vital to ensure the Python interpreter is pointing to the correct Python executable. This depends on whether you are using virtual environments or not. Changing the interpreter location can be done by following this instruction.

Notepad++#

Notepad++ is a pure Windows program meant to extend the Notepad application with more features and supports several languages.

It can be downloaded here.