-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage-Management.py
94 lines (65 loc) · 2.68 KB
/
Package-Management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#* Package Management - Python
# In Python, a package is a way to organize and structure modules in a hierarchical directory. Packages
# allow modularity and code reuse. Here is a basic description how to manage packages in Python:
#? Structure of a Package:
# The basic structure of a package includes a directory that contains a special file called `__init__.py`.
# This file may be empty or may contain initialization code for the package.
'''
My Package/
|-- __init__.py
|-- module1.py
|-- module2.py
|-- subpackage/
| |-- __init__.py
| |-- module3.py
| |-- module4.py
'''
#? Creating a Package:
# To create a package, simply create a directory and place the `__init__.py` files and modules that you
# want to include in the package.
#? Importing Modules from a Package:
# You can import modules from a package in several ways:
# Import a specific module
from My_Module import module_0
# Import the entire package (and access modules with dot notation)
import My_Module
My_Module.module1.function()
# Import a specific module from a subpackage
from My_Module import module_3
#? Usage of `__init__.py`:
# The `__init__.py` file is executed when the package is imported. You can use it to initialize
# variables, configure the package, or perform other initialization tasks.
#? Relative Packages:
# You can use relative imports to reference modules within the same package. This is done
# using the dot (`.`) instead of the absolute package name.
#* Relative import example
from . My_Module import My_Module_1
from . My_Module import My_Module_2
#? Package Distribution:
# If you want to distribute your package for others to install, you can use tools like `setuptools`
# and create a `setup.py` file. Then, you can package your package and distribute it via Python
# Package Index (PyPI).
#? Example of `setup.py`:
from setuptools import setup, find_packages
setup(
name='my_package',
version='0.1',
packages=find_packages(),
install_requires=[
# List of package dependencies
],
)
#? Creating an Installable Package:
#* 1.
# Organize your package as a directory structure with a `__init__.py` at each level.
#* 2.
# Create a `setup.py` file in the root directory of your package.
#* 3.
# Run `python setup.py sdist` to create a distributable source file.
#* 4.
# Publish your package on PyPI or share the generated file.
#? Package Installation:
# After you package your package, others can install it using `pip`:
#* pip install my_package
# These are the basics of working with packages in Python. Packages are a way Effective at organizing and
# sharing code in larger projects.