{"id":5971,"date":"2022-04-23T22:16:43","date_gmt":"2022-04-23T20:16:43","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5971"},"modified":"2026-01-23T18:30:42","modified_gmt":"2026-01-23T17:30:42","slug":"packages-in-python","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/04\/23\/packages-in-python\/","title":{"rendered":"Packages in Python"},"content":{"rendered":"<p>In the world of modules, a package plays a similar role to a folder\/directory in the world of files.<\/p>\n<p><!--more--><\/p>\n<p>1. While a module is designed to couple together some related entities (functions, variables, constants, etc.), a package is a container which enables the coupling of several related modules under one common name. Such a container can be distributed as-is (as a batch of files deployed in a directory sub-tree) or it can be packed inside a zip file.<\/p>\n<p>2. During the very first import of the actual module, Python translates its source code into the semi-compiled format stored inside the <strong><code>pyc<\/code> <\/strong>files, and deploys these files into the <strong><code>__pycache__<\/code><\/strong> directory located in the module&#8217;s home directory.<\/p>\n<p>3. If you want to instruct your module&#8217;s user that a particular entity should be treated as private (i.e. not to be explicitly used outside the module) you can mark its name with either the <strong><code>_<\/code> <\/strong>or <strong><code>__<\/code> <\/strong>prefix. Don&#8217;t forget that this is only a recommendation, not an order.<\/p>\n<p>4. The names shabang, shebang, hasbang, poundbang, and hashpling describe the digraph written as <strong><code>#!<\/code><\/strong>, used to instruct Unix-like OSs how the Python source file should be launched. This convention has no effect under MS Windows.<\/p>\n<p>5. If you want convince Python that it should take into account a non-standard package&#8217;s directory, its name needs to be inserted\/appended into\/to the import directory list stored in the <strong><code>path<\/code> <\/strong>variable contained in the <strong><code>sys<\/code> <\/strong>module.<\/p>\n<p>6. A Python file named <strong><code>__init__.py<\/code><\/strong> is implicitly run when a package containing it is subject to import, and is used to initialize a package and\/or its sub-packages (if any). The file may be empty, but must not be absent.<\/p>\n<p>&nbsp;<\/p>\n<p>6. A repository (or repo for short) designed to collect and share free Python code exists and works under the name<strong> Python Package Index (PyPI)<\/strong> although it&#8217;s also likely that you come across a very niche name The Cheese Shop. The Shop&#8217;s website is available at https:\/\/pypi.org\/.<\/p>\n<p>&nbsp;<\/p>\n<p>7. To make use of The Cheese Shop the specialized tool has been created and its name is <strong>pip<\/strong> (pip installs packages while pip stands for&#8230; ok, don&#8217;t mind). As pip may not be deployed as a part of standard Python installation, it is possible that you will need to install it manually. Pip is a console tool.<\/p>\n<p>&nbsp;<\/p>\n<p>8. To check pip&#8217;s version one the following commands should be issued:<\/p>\n<pre class=\"lang:default decode:true \">pip --version<\/pre>\n<p>or<\/p>\n<pre class=\"lang:default decode:true \">pip3 --version<\/pre>\n<p>Check yourself which of these works for you in your OS&#8217; environment.<\/p>\n<p>&nbsp;<\/p>\n<p>9. List of main pip activities looks as follows:<\/p>\n<p><strong><code>pip help operation<\/code><\/strong> &#8211; shows brief pip&#8217;s description;<br \/>\n<strong><code>pip list<\/code> <\/strong>&#8211; shows list of currently installed packages;<br \/>\n<strong><code>pip show package_name<\/code><\/strong> &#8211; shows package_name info including package&#8217;s dependencies;<br \/>\n<strong><code>pip search anystring<\/code> <\/strong>&#8211; searches through PyPI directories in order to find packages which name contains anystring;<br \/>\n<strong><code>pip install name<\/code><\/strong> &#8211; installs name system-wide (expect problems when you don&#8217;t have administrative rights);<br \/>\n<strong><code>pip install<\/code><code>--user name<\/code> <\/strong>&#8211; install name for you only; no other your platform&#8217;s user will be able to use it;<br \/>\n<strong><code>pip install -U name<\/code><\/strong> &#8211; updates previously installed package;<br \/>\n<code><strong>pip uninstall name<\/strong><\/code> &#8211; uninstalls previously installed package;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<p>You want to prevent your module&#8217;s user from running your code as an ordinary script. How will you achieve such an effect?<\/p>\n<pre class=\"lang:default decode:true\">import sys\r\n\r\nif __name__ == \"__main__\":\r\n  print \"Don't do that!\"\r\n  sys.exit()<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>Some additional and necessary packages are stored inside the<strong><code> D:\\Python\\Project\\Modules<\/code><\/strong> directory. Write a code ensuring that the directory is traversed by Python in order to find all requested modules.<\/p>\n<pre class=\"lang:default decode:true \">import sys\r\n\r\n# note the double backslashes!\r\nsys.path.append(\"D:\\\\Python\\\\Project\\\\Modules\")<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 3<\/em><\/p>\n<p>The directory mentioned in the previous exercise contains a sub-tree of the following structure:<\/p>\n<pre class=\"lang:default decode:true\">abc\r\n|__ def\r\n|__ mymodule.py<\/pre>\n<p>Assuming that <strong><code>D:\\Python\\Project\\Modules<\/code><\/strong> has been successfully appended to the <strong><code>sys.path<\/code><\/strong> list, write an import directive letting you use all the mymodule entities.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">import abc.def.mymodule<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 4<\/em><\/p>\n<p>Where does the name &#8220;The Cheese Shop&#8221; come from?<\/p>\n<p id=\"sol\">It&#8217;s a reference to an old Monty Python&#8217;s sketch of the same name.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 5<\/em><\/p>\n<p>Why should I ensure which one of pip and pip3 works for me?<\/p>\n<p>When Python 2 and Python 3 coexist in your OS, it&#8217;s likely that pip identifies the instance of pip working with Python 2 packages only.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 6<\/em><\/p>\n<p>How can I determine if my pip works with either Python 2 or Python 3?<\/p>\n<pre class=\"lang:default decode:true \">pip --version<\/pre>\n<p>will tell you that.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 7<\/em><\/p>\n<p>Unfortunately, I don&#8217;t have administrative right. What should I do to install a package system-wide?<\/p>\n<p>You have to ask your sysadmin &#8211; don&#8217;t try to hack your OS!<\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 8<\/p>\n<p>You have installed a Python package to edit videos. The package is structured as follows:<\/p>\n<pre class=\"lang:default decode:true \">    editor\r\n        |________videos\r\n        |        |______videoproc.py\r\n        |        |______videoedit.py\r\n        |\r\n        |________sounds\r\n                    |____fx\r\n                          |____fx1.py\r\n                          |____fx2.py<\/pre>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You want to use the function <code>editfx()<\/code>\u00a0 from the <code>fx1<\/code>\u00a0 module ; you also want to rename the function <code>editfx()<\/code> as <code>editsound()<\/code> because you already have defined a function\u00a0 <code>editfx()<\/code> in your program.<\/p>\n<p>Which of the code below will achieve these requirements ?<\/p>\n<\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>A: <code>import editor.sounds.fx.fx1.editfx as editsound<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div>B: <code>from editor.sounds.fx.fx1 import editfx as editsound<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>C: <code>from fx1 import editfx as editsound<\/code><\/p>\n<p>D:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">import editor.sounds.fx.fx1\r\nrename editfx as editsound<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>&nbsp;<\/p>\n<p>Explanation:<\/p>\n<p>When importing a module from a package that has multiple subpackages (subfolders), you need to indicate the &#8220;path&#8221; of the module in the import statement, starting from the &#8220;root&#8221; package all the way to the actual module name.<\/p>\n<p>In the case of the above package, importing any functions from the <code>fx1<\/code> module would be done as follow:<\/p>\n<p><code>import editor.sounds.fx.fx1<\/code>\u00a0 -&gt; this would import all the functions, methods, etc. from the <code>fx1<\/code> module.<\/p>\n<p>To import a specific function (for example <code>editfx()<\/code>) from the <code>fx1<\/code> module, you would use the following command :<\/p>\n<p><code>from editor.sounds.fx.fx1 import editfx<\/code>\u00a0 -&gt; this would import the <code>editfx()<\/code> function from the <code>fx1<\/code> module.<\/p>\n<p>To rename a function when importing it, you can use the <code>as<\/code> keyword :<\/p>\n<p><code>from editor.sounds.fx.fx1 import editfx as editsound<\/code>\u00a0 \u00a0-&gt; this is the correct answer.<\/p>\n<p>B.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 9<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You have stored multiple Python modules in folder :<\/p>\n<p><code> <em>C:\\Users\\luke\\Documents\\My_modules<\/em><\/code> (on a Windows Operating System). You want those modules to be available for import in a Python script you are creating.<\/p>\n<p>Which code snippet below will achieve this ?<\/p>\n<\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div>A.<\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">from sys import path\r\npath.append('C:\\\\Users\\\\luke\\\\Documents\\\\My_modules')<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">B.<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">import sys\r\nsys.path.append('C:\\Users\\luke\\Documents\\My_modules')<\/pre>\n<p>C.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import os\r\nos.path.append('C:\\\\Users\\\\luke\\\\Documents\\\\My_modules')<\/pre>\n<p>D.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">import os\r\nos.path.append('C:\\Users\\luke\\Documents\\My_modules')<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Explanation:<\/p>\n<p>When importing a module, Python will search for those modules in all folders defined in the <code>path <\/code>variable. That variable can be read or modified using the <code>sys <\/code>module.<\/p>\n<p>In addition, because <code>\\<\/code> is a special character, an escape character (<code>\\<\/code>) must be use to represent it in a string &#8211; in other words : <code>\\\\<\/code><\/p>\n<p>So, the only valid answer is A :<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">from sys import path\r\npath.append('C:\\\\Users\\\\luke\\\\Documents\\\\My_modules')<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 10<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You have been provided with a new Python module file <code>moduleABC.py<\/code> which you placed in folder <code>C:\\MyModules<\/code> on your computer (Operating System is Windows).<\/p>\n<p>You have written a code snippet which uses this module (you have included <code>import moduleABC<\/code> in your code). However, when you run your program, you receive a <code>ModuleNotFoundError<\/code> exception.<\/p>\n<p>How can you resolve this issue and clear this exception ?<\/p>\n<\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>A. Add the folder <code>C:\\MyModules<\/code>\u00a0 in the <code>__init__.py <\/code> file, which will instruct Python on where to find the new module.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>B. Move the file <code>moduleABC.py<\/code>\u00a0 in the <code>__pycache__<\/code> directory.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>C. Use <em>pip <\/em>to install the module which will place the file in the correct folder.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>D. Update the <code>sys.path<\/code> list with folder <code>C:\\MyModules<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\"><label class=\"ud-heading-md ud-form-label ud-heading-sm\" for=\"form-group--3077\"><\/label><em>Explanation:<\/em><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>When Python searches for a module to import, it uses the variable <code>path<\/code> (accessible from module <code>sys<\/code>) which is a list of directories and search for the module in this list of directories. Adding the path of a directory in <code>sys.path<\/code> will instruct Python to also search for this directory when importing a module.<\/p>\n<p>So, the correct answer is :<\/p>\n<p>Update the <code>sys.path<\/code> list with folder <code>C:\\MyModules<\/code>.<\/p>\n<p>The other suggested answers are incorrect :<\/p>\n<p>&#8211;&gt; <code>__init__.py <\/code>\u00a0 is a file used within a package for its initialization &#8211; this has nothing to do with the question.<\/p>\n<p>&#8211;&gt; the <code>__pycache__<\/code>\u00a0 directory is a special directory created by Python after the first import of a module and that contains pyc files which are semi-compiled code. Again, this will not help with searching for a module.<\/p>\n<p>&#8211;&gt; <em>pip <\/em>is used to install Python packages. This will not help with a module search either.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the world of modules, a package plays a similar role to a folder\/directory in the world of files.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5971"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5971"}],"version-history":[{"count":12,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"predecessor-version":[{"id":6283,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5971\/revisions\/6283"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}