.. _navigate: Using VS Code =========================================================================================================== .. note:: :class: margin 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. .. _vscode: 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. .. image:: ../../shared/figures/vscode_annotated.png :width: 900 :alt: 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 :kbd:`F5`, will run the file which is currently open in the editor. .. _terminal: Running commands in the terminal --------------------------------------------------------------------------------------------------------------------- .. note:: :class: margin 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 :guilabel:`panel` in the bottom of the screen. If the terminal is not shown, you can immediately activate it by selecting :menuselection:`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 :bash:`python --version` .. command-output:: python --version .. admonition:: The tilde :class: margin 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: :file:`~/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 :bash:`cd` (Windows) or :bash:`pwd` on (Mac/Linux) .. command-output:: pwd :cwd: ../../../../02465students To see the content of the directory, type :bash:`dir` (Windows) or :bash:`ls` (Mac/Linux). An example: .. command-output:: dir :cwd: ../../../../02465students .. admonition:: Try it yourself :class: margin Run the commands :bash:`cd cp`, :bash:`cd ex00` and :bash:`pwd`. Then try :bash:`cd ..` and :bash:`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 :bash:`cd ` (Windows/Mac/Linux) to go into a directory, and :bash:`cd ..` to go up one directory. .. _shell: Starting the Python shell ---------------------------------------------------------------------------------------------------------------------- .. hint:: :class: margin 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 :bash:`python` in the :ref:`terminal `. This will start Python and print the welcome message. You can then input commands as described in the book, for instance: .. runblock:: pycon >>> print("Good job!!") >>> print("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 :bash:`>>>`. Running a Python file from the terminal ---------------------------------------------------------------------------------------------------------------------- .. note:: :class: margin 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 :bash:`python -m`-format. Lets say you have a Python file you want to run from the terminal, for instance, this could be :gitref:`../irlc/project0/fruit_project_tests.py`. You can run it from :ref:`the terminal ` using the command: .. code-block:: bash 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 :bash:`pip install -r requirements_pip.txt` registered the directory :file:`02465students/irlc` with Python. Put simply, whenever Python sees :bash:`python -m irlc.(etc. etc.)`, it knows it should look in the folder :file:`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 .``).