{"id":1901,"date":"2019-06-11T13:06:38","date_gmt":"2019-06-11T13:06:38","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1901"},"modified":"2019-06-13T00:09:27","modified_gmt":"2019-06-13T00:09:27","slug":"section-9-modules-and-packages","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1901","title":{"rendered":"Section 9: Modules and Packages"},"content":{"rendered":"<h1>Section 9 Menu<\/h1>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1893\" rel=\"noopener\">&lt; Section 8<\/a> | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1908\">Section 10 &gt;<\/a><\/p>\n<ol start=\"'68\">\n<li>Pip Install and PyPi<\/li>\n<li>Modules and Packages<\/li>\n<li>__name__ and &#8220;__main__&#8221;<\/li>\n<\/ol>\n<h1>Pip Install and PyPi<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497628#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497628#questions<\/a><\/p>\n<ul>\n<li>Pypi is a repository for open-sourced third party Python packages.<\/li>\n<li>It is similar to RubyGems in the Ruby world, PHP&#8217;s Packagist, CPAN for Perl and NPM for Node.js<\/li>\n<li>So far, we&#8217;ve only used the libraries that come internally with Python.\n<ul>\n<li>math<\/li>\n<\/ul>\n<\/li>\n<li>There are many other libraries available that people have open-sourced and shared on PyPi.<\/li>\n<li>We can use pip install at the command line to install these packages.<\/li>\n<li>There are packages already created for almost any use case you can think of<\/li>\n<li>A quick google search will usually help you discover a link to the PyPi page for the package, or for the package documentation.<\/li>\n<\/ul>\n<h4>Following examples may require firewall rules to be altered to allow for pip installations.<\/h4>\n<h3>requests<\/h3>\n<pre>C:\\Users\\red01&gt;pip install requests\r\nRequirement already satisfied: requests in c:\\programdata\\anaconda3\\lib\\site-packages (2.21.0)\r\nRequirement already satisfied: idna&lt;2.9,&gt;=2.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests) (2.8)\r\nRequirement already satisfied: chardet&lt;3.1.0,&gt;=3.0.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests) (3.0.4)\r\nRequirement already satisfied: urllib3&lt;1.25,&gt;=1.21.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests) (1.24.1)\r\nRequirement already satisfied: certifi&gt;=2017.4.17 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests) (2019.3.9)<\/pre>\n<h3>colorama<\/h3>\n<pre>pip install colorama\r\nRequirement already satisfied: colorama in c:\\programdata\\anaconda3\\lib\\site-packages (0.4.1)<\/pre>\n<pre>python\r\n&gt;&gt;&gt; from colorama import init\r\n&gt;&gt;&gt; init()\r\n&gt;&gt;&gt; from colorama import Fore\r\n&gt;&gt;&gt; print(Fore.RED + \"some red text\")<\/pre>\n<p><span style=\"color: #ff0000;\">some red text<\/span><\/p>\n<h2>Searching for packages<\/h2>\n<p>&#8220;python package for excel&#8221;<\/p>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/openpyxl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1905\" src=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/openpyxl.png\" alt=\"\" width=\"688\" height=\"72\" srcset=\"https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/openpyxl.png 688w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/openpyxl-300x31.png 300w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/openpyxl-150x16.png 150w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/a><\/p>\n<pre>pip install openpyxl<\/pre>\n<p>&nbsp;<\/p>\n<h1>Modules and Packages<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497632#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497632#questions<\/a><\/p>\n<h2>Writing your own modules and packages<\/h2>\n<ul>\n<li>Now that we understand how to install external packages, let&#8217;s explore how to create our own modules and packages.<\/li>\n<li>Modules are just .py scripts that you cal in another .py script<\/li>\n<li>Packages are a collection of modules.\n<ul>\n<li>Needs to be an __init__.py script in the folder to show python the collection of scripts needs to be treated as a package.\n<ul>\n<li>This file needs no contents!\u00a0 It just needs to exist!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Example:<\/p>\n<p>Create 2 files in the same folder. myprogram.py and mymodule.py<\/p>\n<p><b>Create myprogram.py<\/b><\/p>\n<pre>from mymodule import my_func()\r\nmy_func()<\/pre>\n<pre>python myprogram.py<\/pre>\n<p>Traceback (most recent call last):<br \/>\nFile &#8220;myprogram.py&#8221;, line 1, in<br \/>\nfrom mymodule import my_func<br \/>\nModuleNotFoundError: No module named &#8216;mymodule&#8217;<\/p>\n<p><b>Create mymodule.py<\/b><br \/>\nmymodule.py<\/p>\n<pre>def my_func():\r\n    print(\"You found mymodule.py!\")<\/pre>\n<pre>python myprogram.py<\/pre>\n<p>You found mymodule.py!<\/p>\n<h2>Create a package<\/h2>\n<p>Folders and file setup:<\/p>\n<pre>|- myprogram.py\r\n+- MyMainPackage\r\n   |- __init__.py\r\n   |- mainpackage.py\r\n   +- SubPackage\r\n      |- __init__.py\r\n      |- subpackage.py<\/pre>\n<p><b>subpackage.py<\/b><\/p>\n<pre>def sub_report():\r\n    print(\"I'm in folder SubPackage\")<\/pre>\n<p><b>mainpackage.py<\/b><\/p>\n<pre>def main_report():\r\n    print(\"I'm in 'mainpackage.py' located in folder MyMainPackage\")<\/pre>\n<p><b>myprogram.py<\/b><\/p>\n<pre>from MyMainPackage import mainpackage\r\nfrom MyMainPackage.SubPackage import subpackage<\/pre>\n<p>Run the program<\/p>\n<pre>python myprogram.py<\/pre>\n<p>I&#8217;m in &#8216;mainpackage.py&#8217; located in folder MyMainPackage<br \/>\nI&#8217;m in folder SubPackage<\/p>\n<p>mainpackage.main_report()<br \/>\nsubpackage.sub_report()<\/p>\n<h1>__name__ and &#8220;__main__&#8221;<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497634?start=0#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497634?start=0#questions<\/a><\/p>\n<p>Sometimes when you are importing from a module, you would like to know whether a mdule&#8217;s function is being used as an import, or if you are using the original .py file of that module.<\/p>\n<p>Example: if someone runs a script directly from the command line, Python creates the string __name__ and sets it to value &#8216;__main__&#8217;<\/p>\n<pre># one.py\r\ndef func():\r\n    print(\"func() in one.py\")\r\nprint(\"Top level in one.py\")\r\nif __name__=='__main__':\r\n    print(\"one.py is being ran directly\")\r\nelse:\r\n    print(\"one.py has been imported as a module\")<\/pre>\n<pre># two.py\r\nimport one\r\nprint(\"Top level in two.py\")\r\none.func()\r\nif __name__=='__main__':\r\n    print(\"two.py is being ran directly\")\r\nelse:\r\n    print(\"two.py has been imported as a module\")<\/pre>\n<pre>python one.py<\/pre>\n<p>Top level in one.py<br \/>\none.py is being ran directly<\/p>\n<pre>python two.py<\/pre>\n<p>Top level in one.py<br \/>\none.py has been imported as a module<br \/>\nTop level in two.py<br \/>\nfunc() in one.py<br \/>\ntwo.py is being ran directly<\/p>\n<h3>Typical Use<\/h3>\n<pre>#somefile.py\r\ndef a():\r\n    pass\r\ndef b():\r\n    pass\r\ndef c():\r\n    pass\r\nif __name__=='__main__':\r\n    # now start your coding\r\n    # yes, below the functions.  Odd, I know.<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Section 9 Menu &lt; Section 8 | Section 10 &gt; Pip Install and PyPi Modules and Packages __name__ and &#8220;__main__&#8221; Pip Install and PyPi https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9497628#questions Pypi is a repository for open-sourced third party Python packages. It is similar to RubyGems in the Ruby world, PHP&#8217;s Packagist, CPAN for Perl and NPM for Node.js So far, ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1901\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1901","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1901"}],"version-history":[{"count":5,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":1914,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions\/1914"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}