|
39 | 39 | "cell_type": "markdown", |
40 | 40 | "metadata": {}, |
41 | 41 | "source": [ |
42 | | - "Below is an example of a custom module (i.e. one we have writeen ourselves) called `print_things.py`:" |
| 42 | + "Below is an example of a custom module (i.e. one we have written ourselves) called `print_things.py`:" |
43 | 43 | ] |
44 | 44 | }, |
45 | 45 | { |
|
151 | 151 | "from print_things import *\n", |
152 | 152 | "```\n", |
153 | 153 | "\n", |
154 | | - "If there are any modules that we don't want to be imported (i.e. that we only want to be used within the module itself), we can prefix them with an underscore (`_`).\n", |
| 154 | + "Using this style of import, if there are any modules that we don't want to be imported (i.e. that we only want to be used within the module itself), we can prefix them with an underscore (`_`).\n", |
155 | 155 | "\n", |
| 156 | + "```python\n", |
| 157 | + "def print_names(list_of_names):\n", |
| 158 | + " for name in list_of_names:\n", |
| 159 | + " print(name)\n", |
| 160 | + "\n", |
| 161 | + "def print_dictionary(input_dictionary):\n", |
| 162 | + " for key, value in input_dictionary.items():\n", |
| 163 | + " print(\"Key: \" + key + \", Value: \" + str(value))\n", |
| 164 | + "\n", |
| 165 | + "def _private_function():\n", |
| 166 | + " print(\"I'm a private function!\")\n", |
| 167 | + "```\n", |
| 168 | + "\n", |
| 169 | + "Here, `_private_function()` can only be used within the `print_things.py` module. We can test this by importing the `print_things.py` module and trying to use it:" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": null, |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [], |
| 177 | + "source": [ |
| 178 | + "# Import all of the functions from the print_things.py module\n", |
| 179 | + "from print_things import *\n", |
| 180 | + "\n", |
| 181 | + "element_names = [\"Hydrogen\", \"Helium\", \"Lithium\"]\n", |
| 182 | + "\n", |
| 183 | + "# Use the print_names() function \n", |
| 184 | + "print_names(element_names)\n", |
| 185 | + "\n", |
| 186 | + "# Use the _private_function()\n", |
| 187 | + "_private_function()" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "This can be avoided by importing the module in the normal way:" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [ |
| 203 | + "# Import all of the functions from the print_things.py module\n", |
| 204 | + "import print_things\n", |
| 205 | + "\n", |
| 206 | + "element_names = [\"Hydrogen\", \"Helium\", \"Lithium\"]\n", |
| 207 | + "\n", |
| 208 | + "# Use the print_names() function \n", |
| 209 | + "print_things.print_names(element_names)\n", |
| 210 | + "\n", |
| 211 | + "# Use the _private_function()\n", |
| 212 | + "print_things._private_function()" |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "markdown", |
| 217 | + "metadata": {}, |
| 218 | + "source": [ |
156 | 219 | "Another useful way to use both the `import` / `from` statements is to `import` modules and use an alternative name:\n", |
157 | 220 | "\n", |
158 | 221 | "```python\n", |
159 | 222 | "from print_things import print_names as pn # Import print_names from the print_things module \n", |
160 | | - "import print_things as pt # Import the whole print_things module\n", |
| 223 | + "import print_things as pt # Import the whole print_things module\n", |
161 | 224 | "``` " |
162 | 225 | ] |
163 | 226 | }, |
|
199 | 262 | "cell_type": "markdown", |
200 | 263 | "metadata": {}, |
201 | 264 | "source": [ |
202 | | - "An important point that applies to packages (and by extension modules) is their location. When Python tries to execute an `import` function it looks in a number of places, including:\n", |
203 | | - "\n", |
204 | | - "* The specified `PYTHONPATH`\n", |
205 | | - "* The current working directory\n", |
206 | | - "* Install specific directories\n", |
207 | | - "\n", |
208 | | - "Luckily we usually don't need to worry about where packages are as we can use a dedicated package manager to automatically take care of their location for us. These managers are useful and contain a huge variety of different packages that aren't included within Python as standard. For example, packages such as `NumPy` are commonly retrieved this way. \n" |
| 265 | + "An important point that applies to packages (and by extension modules) is their location. Most of the time a package manager will sort out the location of installed packages so they are useable. Additionally, Python will also look in your current working directory for any [custom packages](https://pythoninchemistry.org/intro_python_chemists/advanced/custom_packages.html). \n" |
209 | 266 | ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "code", |
| 270 | + "execution_count": null, |
| 271 | + "metadata": {}, |
| 272 | + "outputs": [], |
| 273 | + "source": [] |
210 | 274 | } |
211 | 275 | ], |
212 | 276 | "metadata": { |
|
0 commit comments