{"id":112927,"date":"2022-06-14T10:47:00","date_gmt":"2022-06-14T10:47:00","guid":{"rendered":"https:\/\/codeinstitute.net\/global\/?p=112927"},"modified":"2024-04-03T10:08:27","modified_gmt":"2024-04-03T10:08:27","slug":"what-are-python-modules","status":"publish","type":"post","link":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/","title":{"rendered":"What are Python Modules?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In short Python Modules are just files with Python code.&nbsp;Python modules refer to files with the \u2018.py\u2019 extension that contain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python statements<\/li>\n\n\n\n<li>Python classes<\/li>\n\n\n\n<li>Python definitions (functions)<\/li>\n\n\n\n<li>Data in any form acceptable to python, eg(lists, dicts, objects, arrays)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Usually, modules would be grouped in packages that can be installed in your local development environment, after which you can import functions from those packages and use them within your code.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, you can also create your own python modules (you can create your own packages, but we\u2019ll stick to the basics of modules for now).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Clean, reusable code. It\u2019s what every developer aspires to.&nbsp;&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To this end, we have reusable functions that are written to:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>accept parameters,&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>|&nbsp; also called arguments (<\/em><strong><em>*args<\/em><\/strong><em>) and\/or keyword arguments (<\/em><strong><em>**kwargs<\/em><\/strong><em>)&nbsp; |<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>handle the data within those parameters,&nbsp;<\/li>\n\n\n\n<li>and return said data.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">What if we wanted to take it a step further? If we wanted to, not only write clean reusable code but also organize and store our code in clearer, structured modules?&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This also comes into play when using design patterns, writing and organizing data classes, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is where modules come in &#8211; Not only can we have code that can be used and reused within a project, we can create modules that can be used in any project. In this post, we\u2019re going to talk specifically about Python Modules.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What are python modules?<\/li>\n\n\n\n<li>How can we use them?<\/li>\n\n\n\n<li>How do we install them?<\/li>\n\n\n\n<li>How can we import them?<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>In the world of web development, certain terminology can be confusing.&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019ve recently begun learning python you might ask \u201cwhat is the difference between an app, and a module?\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An \u2018app\u2019 can be just one \u2018module\u2019 within a larger app. However, while you can have multiple python modules within one app, you cannot have apps within a module. This could be confusing, especially if early on in your coding journey, you\u2019ve begun learning Python and working with, say <strong><a href=\"https:\/\/codeinstitute.net\/global\/blog\/what-is-django\/\" target=\"_blank\" rel=\"noreferrer noopener\">Django<\/a><\/strong>.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create our main project, in this case, it\u2019s a site for a local photographer so we\u2019ll call it photography_philosophy<\/li>\n\n\n\n<li>we create the main <strong>app<\/strong> and let\u2019s say we create an <strong>app <\/strong>specifically to handle the logic for the initial landing page (home)&nbsp;<\/li>\n\n\n\n<li>and an <strong>app <\/strong>to handle the logic for site Users (users)<\/li>\n\n\n\n<li>Our project folder structure looks like this,&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-\/photography_philosophy<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;&nbsp;\u2014\/photography_philosophy<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;&nbsp;\u2014\/home<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;&nbsp;\u2014\/users<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We add the <strong>apps <\/strong>to the INSTALLED_APPS config var (environment variable) in <strong>settings.py<\/strong> but we have a typo. The <strong>app folder<\/strong> is called <strong>\u2018users\u2019<\/strong>, but we\u2019ve put <strong>\u2018user\u2019 <\/strong>into our settings.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/6BlqDdKk1mFLhNts_i5uhNabme_DyH-JaQ0JgiwElrW3fpaMwi5cNOz9haYyf_5_RNspHdQ04pHobN4F52kdWDi0w2Vu_GuebhirRtaOOg1pPQUk7O0Hr9FoVwtd_tKfzMX2ty_n3CTkUWFE7g\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We get a | &nbsp; <strong>Module<\/strong>NotFoundError&nbsp; | when trying to run the project.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/4bMnJTOxB95gclJYXk4o9zY2rzM9TG604XHW_XGVpwESzr89wfpUNMwNZN4AExz0ZLc5Oqk69fZ45h4n3Mkmfzy8enyXEVXduNJmKCpk1L9CD6hyrr0mWEhyTXMYyN9wmtM0p2DXiu9KN601QQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u201cBut it\u2019s an <\/em><strong><em>app<\/em><\/strong><em>, not a <\/em><strong><em>module<\/em><\/strong><em>? The command to start it is literally, start<\/em><strong><em>app<\/em><\/strong><em>?\u201d<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is an example of how the terms <strong>\u2018app\u2019 <\/strong>and <strong>\u2018module\u2019 <\/strong>can sometimes be used interchangeably and might cause confusion.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a basic python module&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">At its most basic, this is as simple as creating a file, giving it the \u2018.py\u2019 extension, and adding a few lines of code.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I\u2019ll create a file: <strong>app.py<\/strong> and in this file, I\u2019ll write a small function that takes in a single parameter (argument, or *args) and prints the value of that parameter to the terminal.&nbsp;<\/li>\n\n\n\n<li>Then I\u2019ll call the function and pass in a familiar string, the foundation on which all coding tutorials are built, the ever-present \u2018Hello World!\u2019.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/yO_bZL4F_HwWQj1lp_PfxkcBgJRyWJ-6oWdXCL5eka-ANz4ycHkgKsK_0bidOSt8jX-Ap2CrwRvUz5XvGVnEgIb9mmQIPsNmI04oMUBbO0RsIipGG6CFxXI7vvc9HlnDVy_cVv_IMxiz-AhsTQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">There we have it. We\u2019ve written our first python module! We\u2019ll go further into this later in the article.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How to Install Modules in Python<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>You\u2019ll need the Python package manager (pip)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python has a package manager called <strong>pip <\/strong>which is used to install packages, either system-wide or within a specific environment. <strong>Hold on<\/strong>, \u201cI thought we were installing <strong>modules<\/strong>?\u201d you may be thinking.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, in essence, we are, but here\u2019s where understanding certain terminology comes into play.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a <strong>package <\/strong>vs what is a <strong>module<\/strong>?&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a <strong>package <\/strong>as a group of related files (<strong>modules<\/strong>) and <strong>associated functions\/classes<\/strong> that are within those <strong>modules <\/strong>that work together to form a complete <strong>package<\/strong>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019ve worked with NodeJS or React, you may have used one of the following, npx or npm (node package manager). We can start to see how, across coding languages, across frameworks, certain patterns start to emerge, as regards coding concepts and even some naming conventions (packages\/apps\/modules \u2026etc).&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Back to python however and the syntax for installing a Python module using <strong>pip <\/strong>is as follows:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>pip install module-name<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re working with Gitpod the syntax is almost identical, you just need to specify the version<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>pip3 install module-name<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">For this to work, you must have pip installed, and the installation process differs according to your system. The following would be to install pip system-wide.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installing pip on Windows<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure that you have at least Python 3.4 or newer installed. Python versions beyond 3.4 ship with pip by default, so it should already be installed. The following examples and commands should be run from the terminal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>If you\u2019re running these commands outside your <\/em><strong><em>IDE<\/em><\/strong><em> |&nbsp; Integrated Development Environment&nbsp; | run from a Git Bash or PowerShell terminal on Windows, or a Bash terminal on macOS and Linux).<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can check which version of python you have installed by running this command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>python --version<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">However, if pip is not available, just run this command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>py -3 -m ensurepip<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installing pip on macOS<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, open a Bash terminal and download the get-pip.py installation file with curl:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">And then run the script to install pip:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>python get-pip.py<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installing pip on Linux<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, open a Bash terminal and download the package using this command:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">curl -O <a href=\"https:\/\/bootstrap.pypa.io\/get-pip.py\">https:\/\/bootstrap.pypa.io\/get-pip.py<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And install it with the following command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>sudo python get-pip.py<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re using one of either Debian or Ubuntu you might be able to use the systems package manager with one of the following commands:&nbsp;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>sudo apt install python-pip<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>sudo apt install python3-pip<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Have you noticed something? The file we\u2019re downloading and then running is a python module, <strong>get-pip.py &#8211; <\/strong>but more than that, it\u2019s a module that then installs the entire <strong>pip <\/strong>package and any associated modules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From here most of the time it\u2019s simply a case of referring to the documentation for the latest stable release of whichever package you are looking to install.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How to Import Modules in Python<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Importing code from one module to another<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While the syntax may vary, depending on:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the project setup<\/li>\n\n\n\n<li>exactly what you are importing, etc<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">the underlying concept remains the same. Think of it as asking someone to fetch something specific from a specific location. In a real-life situation a person might ask someone, \u201chey would you please bring me the car keys, they are on my desk, thanks\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In pythonic form were one to ask Python to hand them their car keys:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>from desk import car_keys<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s assume the <strong>desk is located in a separate room<\/strong>, the <strong>home office,<\/strong> for example, we could use dot notation to <strong>specify the desk within the home office<\/strong>:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>from home_office.desk import car_keys<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, we could import the entire desk from home_office, and then access the car_keys:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>from home_office import desk \nmy_car_keys = desk.car_keys<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Once a module has been imported, we can consider it to be a class, whose variables (keys, objects, functions) can be accessed.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Let\u2019s look at how this could be applied in a coding situation.&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this, I\u2019m going to use an example of importing directly from Python, and also some examples using a combination of built-in packages\/modules from Django, as well as local user-created modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Importing built-in modules directly from Python.<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python comes with its own built-in modules, methods, and <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html\">functions<\/a>. To dive further into this, check out the <a href=\"https:\/\/docs.python.org\/3\/library\/index.html\">Python Standard Library<\/a>. For this example, we\u2019ll use Python\u2019s built-in datetime. This assumes you have Python installed on your system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To import directly from python is as simple as:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>import datetime<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">For how to use the specific python module we use google, or whichever is your preferred search engine.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve imported the module we can use it:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>import datetime\n\ttoday = datetime.datetime.now()\nprint(today)<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re wondering why I\u2019ve used datetime.datetime, that\u2019s because the overall package is called datetime, and the main class of the package is also called datetime, the specific function I\u2019m finally accessing is now() which will give me the current time. To delve into the <a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\">datetime module further click here<\/a>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Importing in Django.<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I\u2019m using dot notation to import a specific module, <strong>render<\/strong>, from a <strong>Django module<\/strong> called <strong>shortcuts<\/strong>&nbsp;<\/li>\n\n\n\n<li>I\u2019m then returning this module from my index() function, and passing in the request and a template as arguments (*args)<\/li>\n\n\n\n<li>The render method (function) is a built-in Django module that expects one required argument, request, and can take in multiple arguments (*args) and specific keyword arguments (**kwargs)<\/li>\n\n\n\n<li>You can take a look at the <a href=\"https:\/\/github.com\/phpdude\/django-render\">source code here<\/a>. As per the <a href=\"https:\/\/docs.djangoproject.com\/en\/4.0\/topics\/http\/shortcuts\/#render\">Django documentation<\/a> it: Combines a given template with a given context dictionary and returns an <a href=\"https:\/\/docs.djangoproject.com\/en\/4.0\/ref\/request-response\/#django.http.HttpResponse\"><strong>HttpResponse<\/strong><\/a> object with that rendered text.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AMa57sCdoW-KKFMF6ibSWBrO4TmF1VIUU8uUKgwRTOkNE8J4A9r8KnDQKhDcWz4DPQ89bbq_3dZ6BIgddph0fvKIZCPnnm_dMUo21R83cFRXbNB16UCwintsX74mLruhDDFMqwCkWrZeOHvRMQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The render module can also take in a context object &#8211; This is extremely useful because we can pass any data we want to a template via this context object using | <strong>\u2018key\u2019: value<\/strong> | pairs. Here\u2019s how it looks in the code:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Qik4r4_dzR1BQZHXP-ETK4zblX_lwF5kAmac2r96OmegRoHET4bGMqoDKI7f2eRKO2eeMxD4bPTzhhtOBPGXybfnsnZmvGBPQeFNJoOshi6Hpkj1D1dllnqAqOTkllwjCjEbcpTz_6ZrFYggIQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example,&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I\u2019m <strong>defining a variable on line 7<\/strong>, <strong>welcome_text<\/strong>, assigning it a string value.<\/li>\n\n\n\n<li>On <strong>line 10 I define a context object<\/strong>, called <strong>context<\/strong>, and within that object, I declare a \u2018key\u2019: this key I\u2019ve called \u2018welcome_text\u2019 and I assign it that same variable welcome_text I defined on line 7.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Essentially I\u2019m telling Django. This key \u2018welcome_text\u2019 in this context object, has the value of the variable welcome_text on line 7.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I\u2019m also now storing the route to the template in a variable called template on line 9<\/li>\n\n\n\n<li>Both context and template are keyword arguments (**kwargs) that the render() method expects<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">And now the magic. On the actual template file, index.html, I can access that context object and any \u2018key\u2019 within to display the value stored at that \u2018key\u2019:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember the key was \u2018welcome_text\u2019:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/pr5kO6b9MiGDDbCtoVU7dxzy_qBTTt8QUaqifLsVY3xLeTpp_Fe6aQHRa5IqKEs0Fsc2fX0rdv9NonSIJM94uFSZ4-EJb2JvFC2D8TnKEifvxlng7mlSlAh1aKnzw1yc0MjBVEnsPyA4K5whUg\" width=\"449\" height=\"82\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And the result when I run the project (I\u2019ve added some basic styling using <a href=\"https:\/\/getbootstrap.com\/docs\/5.0\/getting-started\/introduction\/\">bootstrap<\/a>):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/M-6JeMzqfA5lrlt8kkYwzclne_dNnp_WEf2F2HzL-WsqT7PaWsjy55xWoryb4Mf8QAj2W90BBpnM2his3Gqi5KaBJ3Wp7fJs_u3dBKho86K4k-H9bFfltfYQ_zjJ63QOmcQybeJOnZEgeuFEmQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And there we have it. We imported a module from Django and used it to render data (in this case a string) to a template. This method can be used to pass other forms of data to a template as well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s get back to Python Modules and how to work with them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Importing various types of data.<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So far we\u2019ve only talked about importing modules and functions, but you can also import data models, classes, lists, and dicts. In Django, you can import Forms from one module into another. Essentially, if you can define it, you can import it from one module into another. I\u2019ll show one brief example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example below is from a project using Django as its framework, the module here is a forms.py module and the class is defining a form.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/KC8gUvqiOl4a9KbYUkLDRkIlvLnTzFZ4n3Vk81iZNdK1P3pAcmk0N7OjNuOwNHhX-fCxl0Zqoveb16XepF5aVaumqX0ullWKz25Ewa302kF1Y6a_88JsRMNiDp0nIEo_cmZwuqQ3F5MDZKCSJQ\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Notice the first import on <strong>line 1<\/strong>, importing <strong>forms <\/strong>from <strong>django<\/strong>&nbsp;<\/li>\n\n\n\n<li>Now look at <strong>line 6<\/strong>, I\u2019m passing that <strong>forms <\/strong>module into my <strong>UserProfileImageForm class<\/strong>, and via dot notation importing a specific module\/method from within the forms module: <strong>ModelForm<\/strong>.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Below is a separate module, this one is called <strong>views.py<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/oVMgq8VU0kI_Qb-GO3nImzPmY6wYPds-OmcHSsxMnuCIZsHFtkVZCyvf2yeyOZPomcjM4WpucXqc5_moC_76tPrOgs6TiCrexFL7nmncihiZzhYbuA-cue6BvIaiFkbO_CXtTsHr8bi-1AufUA\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I\u2019m importing that <strong>UserProfileImageForm<\/strong> class on<strong> line 6.<\/strong>&nbsp;<\/li>\n\n\n\n<li>I\u2019m using from <strong>.forms<\/strong> because I\u2019m importing from a local module located in the same directory, and the dot <strong>.<\/strong> instructs Django to import from the <strong>forms.py<\/strong> module within this specific directory.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">How to write your own Modules in Python<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Let\u2019s write a couple of basic Python modules<\/strong> and import from one module into the other. We\u2019ll take our first example <strong>app.py<\/strong> &#8211; In it, we had a very basic function and we\u2019re going to <strong>move that function into an entirely different module<\/strong>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019re going to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a file called <strong>print.py<\/strong><\/li>\n\n\n\n<li>Cut the function <strong>(only the function)<\/strong> definition from <strong>app.py<\/strong> and paste it into our new <strong>print.py <\/strong>file.<\/li>\n\n\n\n<li><strong>Import<\/strong> that same function from <strong>print.py<\/strong> back into <strong>app.py<\/strong>&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s look at our file structure. We now have 2 modules &#8211; note they are both on the same level in the same root directory&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/gv0XllwdQdKrFDx8Mk38eWJ3Eufxb3f_WswMLojQkDnEO5ju5pg7S60KSsHsPlGaYo0p27giJwYaL1coxTZ7xNu1iEU2hTbSXh2CSaqWbwVnXdtY6pDwq0o1BrhKEe6rhfUIbHBhNjlj8G6xUw\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And here are the contents of our two modules:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/kugfR3L0Fc4-LBrWE1Q8AoOM2NYAVYGMCmNcBoeMBB_Advp8JZJHXWE5x0xHz0kN24lAN3Q1TVa7naxrhGJs5xewqj9Mc5d99qsV9lFopB_Di7DLYxsvc658hBCF5BYzU-fh1D_H3WKUXrUC7A\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/wXT8sdMcuMbkAgjtxM4kwKKVfJcpkkp0zNEMoiYnJwzet3FbiKevvoiP3NujVeQSOv9ma6aLmccwdEu3Cs12gCRONGojgwKlerWLVvUfxOWcB1WyEtz7c-UkmSBpiplilqXW7XAtho3HnyWPKw\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We run our main module with the command:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Specify the python version if working in Gitpod:&nbsp;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>python3 app.py <\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">else:&nbsp;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism off-numbers lang-python\" data-lang=\"Python\" data-show-lang=\"0\"><code>python app.py<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">And the result<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AmEiiEAxfEGln1H9VJURg4riZBllt78kZz_C_tiEH5a_iNw4sy7Ai7iQ1cBOqhIU1j0xZej0uekfrQScHAsN3D8PEJp1UcY4vVKp8SxKL4kO8Q7pFGeSrXmtU_5bkvrQnlroPMgjBJKToOSidg\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Congratulations. You\u2019ve just created 2 python modules and imported a function from one to the other.&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is a very basic example, and in the course of being a developer there will surely come times where you may encounter tricky issues, but the underlying concept remains the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><em>\u201cHold on! In the Django example, we used <\/em><strong><em>from .forms<\/em><\/strong><em> but in the above example, we used <\/em><strong><em>from print? <\/em><\/strong><em>Why didn\u2019t you have to pre-face <\/em><strong><em>from print <\/em><\/strong><em>with a full stop as in the previous Django example <\/em><strong><em>from .forms<\/em><\/strong><em>?\u201d<\/em><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I didn\u2019t have to use from .print import, because this is an example with only one root folder and only those 2 modules, print.py, and app.py.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I didn\u2019t have to specify \u201cuse the corresponding module directly within this folder\u201d using a full stop: from <strong>.forms<\/strong> because that was already implied<\/li>\n\n\n\n<li>There\u2019s only one folder, and only 2 files, print.py, and app.py. Because of this, the command: <strong>import print<\/strong> doesn\u2019t need to be more specific than that.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A Django project would most likely be far more complex, with multiple apps, and modules, and specifying from <strong>.module<\/strong> import directly tells the main Django app, \u201cimport directly from within this app, from within this folder\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Defining a module that already exists.<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One thing to be aware of is to make sure you\u2019re not re-defining a module that already exists. Here are a couple of examples: math and datetime. Both of these are already defined pre-built Python modules.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Be aware that defining your own can cause issues. If you run into such a situation, simply re-name your module making sure to update any instances where you are importing this module to reflect your changes to the module name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Importing from other modules can also be done using dot notation.&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if I know the exact function I want to access, I could import the entire module and simply use the function I want as seen below:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/eG34AaiyuuMwrsDDmiZHdIfS27lP0Kgo24YmUomizMn_5RgZAeIhov1u0wqSqrHtEWtqSMUMROkQOw0pqB42RLy4TqKWxlLOayIfOUidkcBmW5SSluNwJAsT03Mu6ZT54NbARfAEBSnysdqR4g\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">While this is possible, <strong>it might not always be the best idea<\/strong> due to potentially importing a lot of code that you may not need. Assuming that the print module has multiple functions, if you only need the one function, only import that one function from that module (as in our first example, <code>from print import printValue<\/code>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also import modules as x where x would be the variable of your choosing. In this case, whatever you set the name as it will hold any functions\/code from within the module that you imported.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/r60b_yirvYMaiK1BiSF1o4QwdOrBcy2FZaXdUzqa_vgBXmFKD6-B0ca6kj1VULKKfrJfnR8yu3yG_M50QjXx27JxEeH6NmbHcLQ9dCan8HV8Vs39tWBMhr8Aa3UOJY6P5zJukGVRNhZsHl7j6g\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The same can be done for individual functions:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/ALC8IpXv7Lq8s_zimEOpVi_m9uxyEiH7w4mko202qzG0cwzE0cTzxvM3I1bYmyFh4x7w5LvG1qFBEUbK006xzBFPEemzSztV29R5jUTiyDcC4Dxs4HlCzUWjwAlxhTFC9kuyM0T93kslV-8tlw\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The result of which will be the same. I run the <strong>app.py<\/strong> module and the string \u2018Hello World!\u2019, is printed to the terminal:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AmEiiEAxfEGln1H9VJURg4riZBllt78kZz_C_tiEH5a_iNw4sy7Ai7iQ1cBOqhIU1j0xZej0uekfrQScHAsN3D8PEJp1UcY4vVKp8SxKL4kO8Q7pFGeSrXmtU_5bkvrQnlroPMgjBJKToOSidg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Excellent! I hope you\u2019ve enjoyed this introduction to Python modules. If you want to go further in-depth and read more about how to create your own packages, here\u2019s a link to the docs: <a href=\"https:\/\/docs.python.org\/3\/tutorial\/modules.html#packages\">Python Packages<\/a>.<\/p>\n\n\n\n<p class=\"has-text-align-right wp-block-paragraph\"><em>John Traas, Code Institute Graduate<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How can you use Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you are interested in getting keyed up with Python, you can learn with our\u00a0<a href=\"https:\/\/codeinstitute.net\/global\/ie\/full-stack-software-development-diploma\/\" target=\"_blank\" rel=\"noreferrer noopener\">Full Stack Diploma<\/a>.\u00a0To find out more, schedule a call with an education advisor through the form at the bottom of this page. Alternatively, to find out more about Python, <a href=\"https:\/\/codeinstitute.net\/global\/blog\/what-is-python-used-for\/\" target=\"_blank\" rel=\"noreferrer noopener\">click here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In short Python Modules are just files with Python code.&nbsp;Python modules refer to files with the \u2018.py\u2019 extension that contain: Usually, modules would be grouped in packages that can be installed in your local development environment, after which you can import functions from those packages and use them within your code.&nbsp; However, you can also [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":112934,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[79,109],"class_list":["post-112927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-technology"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Modules: An Introduction - Code Institute Global<\/title>\n<meta name=\"description\" content=\"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Modules: An Introduction - Code Institute Global\" \/>\n<meta property=\"og:description\" content=\"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Institute Global\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-14T10:47:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-03T10:08:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-2.png.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Guest Author\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-2.png.webp\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Guest Author\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/\"},\"author\":{\"name\":\"Guest Author\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#\\\/schema\\\/person\\\/59a8fa654948023b958f9dba01fb87e7\"},\"headline\":\"What are Python Modules?\",\"datePublished\":\"2022-06-14T10:47:00+00:00\",\"dateModified\":\"2024-04-03T10:08:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/\"},\"wordCount\":2678,\"publisher\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/Python-Modules-1.png.webp\",\"keywords\":[\"Python\",\"Technology\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/\",\"url\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/\",\"name\":\"Python Modules: An Introduction - Code Institute Global\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/Python-Modules-1.png.webp\",\"datePublished\":\"2022-06-14T10:47:00+00:00\",\"dateModified\":\"2024-04-03T10:08:27+00:00\",\"description\":\"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#primaryimage\",\"url\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/Python-Modules-1.png.webp\",\"contentUrl\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/Python-Modules-1.png.webp\",\"width\":1500,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/what-are-python-modules\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What are Python Modules?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#website\",\"url\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/\",\"name\":\"Code Institute Global\",\"description\":\"A New Career in Tech\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#organization\",\"name\":\"Code Institute Global\",\"url\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Web_grey_logo-1.png.webp\",\"contentUrl\":\"https:\\\/\\\/d3m1rm8xuevz4q.cloudfront.net\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/Web_grey_logo-1.png.webp\",\"width\":251,\"height\":105,\"caption\":\"Code Institute Global\"},\"image\":{\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/#\\\/schema\\\/person\\\/59a8fa654948023b958f9dba01fb87e7\",\"name\":\"Guest Author\",\"description\":\"From time to time, students, graduates, and colleagues of Code Institute contribute to our blogs and articles. Here's where you will find them.\",\"url\":\"https:\\\/\\\/codeinstitute.net\\\/global\\\/blog\\\/author\\\/guest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Modules: An Introduction - Code Institute Global","description":"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/","og_locale":"en_US","og_type":"article","og_title":"Python Modules: An Introduction - Code Institute Global","og_description":"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.","og_url":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/","og_site_name":"Code Institute Global","article_published_time":"2022-06-14T10:47:00+00:00","article_modified_time":"2024-04-03T10:08:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-2.png.webp","type":"image\/webp"}],"author":"Guest Author","twitter_card":"summary_large_image","twitter_image":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-2.png.webp","twitter_misc":{"Written by":"Guest Author","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#article","isPartOf":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/"},"author":{"name":"Guest Author","@id":"https:\/\/codeinstitute.net\/global\/#\/schema\/person\/59a8fa654948023b958f9dba01fb87e7"},"headline":"What are Python Modules?","datePublished":"2022-06-14T10:47:00+00:00","dateModified":"2024-04-03T10:08:27+00:00","mainEntityOfPage":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/"},"wordCount":2678,"publisher":{"@id":"https:\/\/codeinstitute.net\/global\/#organization"},"image":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-1.png.webp","keywords":["Python","Technology"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/","url":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/","name":"Python Modules: An Introduction - Code Institute Global","isPartOf":{"@id":"https:\/\/codeinstitute.net\/global\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#primaryimage"},"image":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-1.png.webp","datePublished":"2022-06-14T10:47:00+00:00","dateModified":"2024-04-03T10:08:27+00:00","description":"In short python modules are just files with Python code. In this informative blog, we look at an introduction to Python Modules.","breadcrumb":{"@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#primaryimage","url":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-1.png.webp","contentUrl":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/05\/Python-Modules-1.png.webp","width":1500,"height":500},{"@type":"BreadcrumbList","@id":"https:\/\/codeinstitute.net\/global\/blog\/what-are-python-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeinstitute.net\/global\/"},{"@type":"ListItem","position":2,"name":"What are Python Modules?"}]},{"@type":"WebSite","@id":"https:\/\/codeinstitute.net\/global\/#website","url":"https:\/\/codeinstitute.net\/global\/","name":"Code Institute Global","description":"A New Career in Tech","publisher":{"@id":"https:\/\/codeinstitute.net\/global\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeinstitute.net\/global\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codeinstitute.net\/global\/#organization","name":"Code Institute Global","url":"https:\/\/codeinstitute.net\/global\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeinstitute.net\/global\/#\/schema\/logo\/image\/","url":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/02\/Web_grey_logo-1.png.webp","contentUrl":"https:\/\/d3m1rm8xuevz4q.cloudfront.net\/wp-content\/uploads\/2022\/02\/Web_grey_logo-1.png.webp","width":251,"height":105,"caption":"Code Institute Global"},"image":{"@id":"https:\/\/codeinstitute.net\/global\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codeinstitute.net\/global\/#\/schema\/person\/59a8fa654948023b958f9dba01fb87e7","name":"Guest Author","description":"From time to time, students, graduates, and colleagues of Code Institute contribute to our blogs and articles. Here's where you will find them.","url":"https:\/\/codeinstitute.net\/global\/blog\/author\/guest\/"}]}},"_links":{"self":[{"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/posts\/112927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/comments?post=112927"}],"version-history":[{"count":4,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/posts\/112927\/revisions"}],"predecessor-version":[{"id":160526,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/posts\/112927\/revisions\/160526"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/media\/112934"}],"wp:attachment":[{"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/media?parent=112927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/categories?post=112927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinstitute.net\/global\/wp-json\/wp\/v2\/tags?post=112927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}