{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 13 - Working with Python files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous blocks, we've mainly used notebooks to develop and run our Python code. In this chapter, we'll introduce how to create Python modules (.py files) and how to run them. The most common way to work with Python is actually to use .py files, which is why it is important that you know how to work with them. You can see python files as one cell in a notebook without any markdown." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before we write actual code in a .py file, we will explain the basics you need to know for doing this:\n", "\n", "* Choosing an editor\n", "* starting the terminal (from which you will run your .py files)\n", "\n", "**At the end of this chapter, you will be able to**\n", "* create python modules, i.e., .py files\n", "* run python modules from the command line\n", "\n", "If you have **questions** about this chapter, please contact us **(cltl.python.course@gmail.com)**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Editor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first need to choose which editor we will use to develop our Python code. \n", "\n", "There are two options.\n", "\n", "1. You create the python modules in your browser. After opening Jupyter Lab, you can click `File` -> `New` and then `Python File` to start developing Python modules.\n", "\n", "2. You install an editor.\n", "Please take a look [here](https://wiki.python.org/moin/PythonEditors) to get an impression of which ones are out there.\n", "We can highly recommend [Atom](https://atom.io/) (for macOS, Windows, Linux). Other options are [BBEdit](https://www.barebones.com/products/bbedit/download.html) (for macOS) and [Notepad++](https://notepad-plus-plus.org/) (for Windows). A simple way to create a new .py file usually is to open a new file and save it as name_of_your_program.py (make sure to use indicative names). \n", "\n", "Please choose between options 1 and 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Starting the terminal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To run a .py file we wrote in an editor, we need to start the terminal. This works differently for windows and Mac:\n", "\n", "1. On Windows, please look at [Anaconda Prompt](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html)\n", "2. on OS X/macOS (Mac computer), please type **terminal** in [Spotlight](https://support.apple.com/nl-nl/HT204014) and start the terminal\n", "\n", "It's a useful skill to know how to navigate through your computer (i.e., go from one directory to another, all the files and subdirectories in a directory, etc.) using the terminal. \n", "\n", "For Windows users, [this](https://www.computerhope.com/issues/chusedos.htm) is a good tutorial.\n", "\n", "For OS X/macOS/Linux/Ubuntu users, [this](https://www.digitalocean.com/community/tutorials/basic-linux-navigation-and-file-management) is a good tutorial." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Running your first program \n", "\n", "Here, we'll show you how to run your first program (hello_world.py).\n", "\n", "In the same folder as this notebook, you will find a file called **hello_world.py**.\n", "\n", "Running it works differently on Windows and Mac. Below, instructions for both can be found:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A.) Running the program on OS X/MacOS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please use the terminal to navigate to the folder in which this notebook is placed by copying the **output** of following cell in your terminal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "cwd = os.getcwd()\n", "cwd_escaped_spaces = cwd.replace(' ', '\\ ')\n", "print('cd', cwd_escaped_spaces)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`cd` means 'change directory'. Here, you are using it to go to the directory we are currently working in. We use the os module to print the path to this directory (`os.getcwd`). \n", "\n", "Please run the following command in the terminal:\n", "\n", "**python hello_world.py**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've succesfully run your first Python program!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## B.) Running the program on Windows\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please use the terminal to navigate to the folder in which this notebook is placed by copying the **output** of the following cell in your terminal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cwd = os.getcwd()\n", "cwd_escaped_spaces = cwd.replace(' ', '^ ')\n", "print('cd', cwd_escaped_spaces)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please run the **output** of the following command in the terminal:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "print(sys.executable + ' hello_world.py')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've succesfully run your first Python program!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Import your own functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Chapter 12, you've been introduced to **importing** modules and functions/methods.\n", "You can see any python program that you create (so any .py file) as a module, which means that you can import it into another python program. Let's see how this works.\n", "\n", "\n", "Please note that the following examples only work if all your python files are in the same directory. There are ways of importing python modules from other directories, but we will not discuss them here. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.1 Importing your entire module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When importing your own functions from your own modules, several things are important. We have created two example scripts to illustrate them called **the_program.py** and **utils.py**. We recommend to open them to check the following things:\n", "\n", "* The extension .py is not used when importing modules. **import utils** will import the file **utils.py** [line 1 the_program.py]\n", "* We can use any function from the file. We can call the count_words function by typing **utils.count_words** [the_program.py line 6]\n", "* We can use any global variable declared in the imported module. E.g. **utils.x** and **utils.python** declared in utils.py can be used in the_program.py " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.2 Importing functions and variables individually" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can import specific functions using the syntax **from MODULE import FUNCTION/VARIABLE**\n", "\n", "This can be seen in the file **the_program_v2.py** (lines 1-3). (Open the files **the_program_v2.py** and **utils.py** in an editor to check this). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.3 Importing functions and variables to python notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please note that you can also import functions and variables from a python program while using notebooks. In this case, simply treat the notebook as the python files the_program.py and the_program_v2.py. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from utils import count_words" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "words = ['how', 'often', 'does', 'each', 'string', 'occur', 'in', 'this', 'list', '?']\n", "\n", "word2freq = count_words(words)\n", "print('word2freq', word2freq)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1**: \n", "\n", "Please create and run your own program using an editor and the terminal. Please copy your beersong into your first program. Tip: simply open a new file in the editor and save it as `beersong.py'. \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2**: \n", "\n", "Please create two files:\n", "* **my_second_program.py**\n", "* **my_utils.py**\n", "\n", "Please create a helper function and store it in **my_utils.py**, import it into **my_second_program.py** and call it from there. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 4 }