In the world of modules, a package plays a similar role to a folder/directory in the world of files.
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.
2. During the very first import of the actual module, Python translates its source code into the semi-compiled format stored inside the pyc files, and deploys these files into the __pycache__ directory located in the module’s home directory.
3. If you want to instruct your module’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 _ or __ prefix. Don’t forget that this is only a recommendation, not an order.
4. The names shabang, shebang, hasbang, poundbang, and hashpling describe the digraph written as #!, used to instruct Unix-like OSs how the Python source file should be launched. This convention has no effect under MS Windows.
5. If you want convince Python that it should take into account a non-standard package’s directory, its name needs to be inserted/appended into/to the import directory list stored in the path variable contained in the sys module.
6. A Python file named __init__.py 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.
6. A repository (or repo for short) designed to collect and share free Python code exists and works under the name Python Package Index (PyPI) although it’s also likely that you come across a very niche name The Cheese Shop. The Shop’s website is available at https://pypi.org/.
7. To make use of The Cheese Shop the specialized tool has been created and its name is pip (pip installs packages while pip stands for… ok, don’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.
8. To check pip’s version one the following commands should be issued:
|
1 |
pip --version |
or
|
1 |
pip3 --version |
Check yourself which of these works for you in your OS’ environment.
9. List of main pip activities looks as follows:
pip help operation – shows brief pip’s description;
pip list – shows list of currently installed packages;
pip show package_name – shows package_name info including package’s dependencies;
pip search anystring – searches through PyPI directories in order to find packages which name contains anystring;
pip install name – installs name system-wide (expect problems when you don’t have administrative rights);
pip install--user name – install name for you only; no other your platform’s user will be able to use it;
pip install -U name – updates previously installed package;
pip uninstall name – uninstalls previously installed package;
Exercise 1
You want to prevent your module’s user from running your code as an ordinary script. How will you achieve such an effect?
|
1 2 3 4 5 |
import sys if __name__ == "__main__": print "Don't do that!" sys.exit() |
Exercise 2
Some additional and necessary packages are stored inside the D:\Python\Project\Modules directory. Write a code ensuring that the directory is traversed by Python in order to find all requested modules.
|
1 2 3 4 |
import sys # note the double backslashes! sys.path.append("D:\\Python\\Project\\Modules") |
Exercise 3
The directory mentioned in the previous exercise contains a sub-tree of the following structure:
|
1 2 3 |
abc |__ def |__ mymodule.py |
Assuming that D:\Python\Project\Modules has been successfully appended to the sys.path list, write an import directive letting you use all the mymodule entities.
|
1 |
import abc.def.mymodule |
Exercise 4
Where does the name “The Cheese Shop” come from?
It’s a reference to an old Monty Python’s sketch of the same name.
Exercise 5
Why should I ensure which one of pip and pip3 works for me?
When Python 2 and Python 3 coexist in your OS, it’s likely that pip identifies the instance of pip working with Python 2 packages only.
Exercise 6
How can I determine if my pip works with either Python 2 or Python 3?
|
1 |
pip --version |
will tell you that.
Exercise 7
Unfortunately, I don’t have administrative right. What should I do to install a package system-wide?
You have to ask your sysadmin – don’t try to hack your OS!

