Using VS Code#

Note

VSCode is an IDE, which stands for Integrated Development Environment. Other popular IDEs for Python are Spyder and Pycharm. You can in principle use any of them if you choose, however, we can provide assistance with VSCode.

VSCode is the program we use to write and run Python code. It will not automatically make you a good programmer, however, it will help you a lot with mundane and annoying tasks, and that will in turn give you more time to learn to be a good programmer.

You can compare using VSCode to edit Python to using a textprocessor when you are writing a report, or using a fully equipped kitchen when you cook: Although you can in principle get the job done without these aids, better tools will as a rule allow you to accomplish more, and get a better result.

Buttons, buttons everywhere#

The downside of VSCode is that, well, it has an awful lot of buttons, which makes it quite unfriendly to look at when you first open it. We hope that you will look past your first impression: Microsoft Word too has a lot of buttons and features, but only very few are actually necessary for every day use.

The VSCode editor

To give an overview:

Menu bar:

Similar to word. You can use this to open files and folders

Actvity bar:

Use the Files button (top) to open the file navigation (which is currently open). Later, you probably also want to try the Test button (bottom)

Panel:

This is where you find the terminal (currently open)

Editor:

This contains your python code.

Run/Debug:

Pressing this button, or F5, will run the file which is currently open in the editor.

Running commands in the terminal#

Note

The terminal is also sometimes called a shell, command line, or console. Apologies if we mix up the terms!

In the above screenshot, the terminal is open in the panel in the bottom of the screen. If the terminal is not shown, you can immediately activate it by selecting Terminal ‣ New Terminal from the menu bar. The terminal allows you to type in commands to your operating system which will then be run in the current directory and display the result. Try for yourself by typing in the exact line python --version

$ python --version
Python 3.11.6

The tilde

The tilde in the front of the path means the location is relative to your home directory.

The blue text in the bottom-center pane tells you the current directory that the terminal is in: ~/Documents/02465students. Many of the commands you will use will behave differently in different directories. You can always print out the current path by typing cd (Windows) or pwd on (Mac/Linux)

$ pwd
/builds/02465material/02465public/02465students

To see the content of the directory, type dir (Windows) or ls (Mac/Linux). An example:

$ dir
LICENSE    irlc		  requirements_conda.txt  setup.py
README.md  irlc.egg-info  requirements_pip.txt	  solutions

Try it yourself

Run the commands cd cp, cd ex00 and pwd. Then try cd .. and pwd

You can compare that to the actual directory using a file explorer. The final command which you need to know is how to change directories. Use cd <directory name> (Windows/Mac/Linux) to go into a directory, and cd .. to go up one directory.

Starting the Python shell#

Hint

Press the up-button the keyboard to access previous commands. This is very useful if you make a typo in a command.

To start Python, type in python in the terminal. This will start Python and print the welcome message. You can then input commands as described in the book, for instance:

>>> print("Good job!!")
Good job!!
>>> print("You are doing it!")
You are doing it!

The Python shell offers a way to quickly test the effect of single commands. We will use it extensively in the documentation to provide examples of what the programs do, and you can always see when an example can be run in the Python shell because the lines start with >>>.

Running a Python file from the terminal#

Note

One of the benefits of doing it this way is that it is less sensitive to your current working directory in the terminal.

I recommend running python files as modules, i.e. using the python -m-format.

Lets say you have a Python file you want to run from the terminal, for instance, this could be irlc/project0/fruit_project_tests.py. You can run it from the terminal using the command:

python -m irlc.project0.fruit_project_tests

Tip

You may wonder why the module-version worked. The reason is that when you followed the installation guide, the command pip install -r requirements_pip.txt registered the directory 02465students/irlc with Python. Put simply, whenever Python sees python -m irlc.(etc. etc.), it knows it should look in the folder 02465students/irlc/(etc. etc.).

If you want a similar setup for your Fagpakkeproject, you should just include (and edit) the setup.py file, and the requirements.txt-file (edit this to contain your packages but keep the command -e .).