GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python package - organizing Python code in packages Python packagelast modified January 29, 2024 In this article we cover Python packages.
A Python package is a collection of modules which have a common purpose.
Package directories must have one special file called When we deal with large projects containing hundreds or thousands of modules, using packages is crucial. For example, we could put all database related modules in a database package and user interface code in ui package.
Built-in packages available in predefined directories; for instance,
Third-party packages are installed into predefined directories such as
Python package management
Python packages are managed with the Python package manager $ sudo pip3 install arrow For instance, the arrow library is installed with the above command. $ sudo pip3 uninstall arrow To uninstall arrow, we use the above command. Python package with empty __init__.pyIn the first example, we create a simple package in Python. $ tree . ├── mymath │ ├── __init__.py │ └── mfuns.py └── myprog.py
In our current working directory we have a
The __init__.py
The mfuns.py
def mycube(x): return x * x * x
In the myprog.py
#!/usr/bin/python # myprog.py from mymath.mfuns import mycube print(mycube(3))
In the Python importing function in __init__.py
In the next example, we have some code in the $ tree . ├── mymath │ ├── __init__.py │ └── mfuns.py └── myprog.py We have the same directory structure. __init__.py
from .mfuns import mycube
In the mfuns.py
def mycube(x): return x * x * x
In the myprog.py
#!/usr/bin/python # myprog.py from mymath import mycube print(mycube(3))
In the Python package without __init__.py
Since Python 3.3 it is possible to define package directories without using
the read.py
constants/
data.py
In our current working directory we have a data.py
colours = ('yellow', 'blue', 'red', 'orange', 'brown')
names = ('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
The read.py
#!/usr/bin/python # read.py from constants.data import colours import constants.data as mydata print(colours) print(mydata.names)
In the $ ./read.py
('yellow', 'blue', 'red', 'orange', 'brown')
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
Python arrow packageThe arrow is a third-party library for working with date and time in Python. $ ls /usr/local/lib/python3.5/dist-packages/arrow api.py arrow.py factory.py formatter.py __init__.py locales.py parser.py __pycache__ util.py
The library is installed in the arrow directory, under the Python subpackagesWe can also create subpackages. To access subpackages, we use the dot operator. $ tree . ├── constants │ ├── __init__.py │ ├── data.py │ └── numbers │ ├── __init__.py │ └── myintegers.py └── read.py constants/__init__.py
from .data import names
This is the constants/data.py
names = ('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
This is the numbers/__init__.py
from .myintegers import myintegers
The numbers/myintegers.py
myintegers = (2, 3, 45, 6, 7, 8, 9)
The integers module defines a tuple of seven integers. This tuple will
be accessed from the read.py
#!/usr/bin/python # read.py from constants import names from constants.numbers import myintegers print(names) print(myintegers)
This is the $ ./read.py
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
(2, 3, 45, 6, 7, 8, 9)
SourcePython packages - language reference In this article we have covered Python packages. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |