-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduleT.py
More file actions
163 lines (104 loc) · 3.42 KB
/
Copy pathmoduleT.py
File metadata and controls
163 lines (104 loc) · 3.42 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# ==========================================================
# IMPORT STATEMENTS IN PYTHON
# ==========================================================
# ------------------------------------------
# 1. import module
# ------------------------------------------
import math
print(math.sqrt(16))
# ------------------------------------------
# 2. import with alias
# ------------------------------------------
import math as m
print(m.pi)
# ------------------------------------------
# 3. from ... import
# ------------------------------------------
from math import sqrt, pi
print(sqrt(25))
print(pi)
# ------------------------------------------
# 4. from ... import *
# ------------------------------------------
# imports all public names (NOT recommended)
from math import *
print(sqrt(36))
# ==========================================================
# LOCATING MODULE
# ==========================================================
# Python searches modules in:
# 1. Current directory
# 2. PYTHONPATH (environment variable)
# 3. Standard library directories
import sys
print(sys.path) # shows search paths
# ==========================================================
# MODULE ATTRIBUTES
# ==========================================================
import math
# __file__ → file location (not available for built-in modules sometimes)
print(getattr(math, "__file__", "Built-in module"))
# __package__ → package name
print(math.__package__)
# __doc__ → documentation string
print(math.__doc__[:100]) # first 100 chars
# __dict__ → namespace dictionary
print(list(math.__dict__.keys())[:10])
# __name__ → module name
print(math.__name__)
# ==========================================================
# __name__ == "__main__"
# ==========================================================
# used to check if file is run directly
def main():
print("Running as main program")
if __name__ == "__main__":
main()
# ==========================================================
# PACKAGES IN PYTHON
# ==========================================================
# A package is a folder containing modules and __init__.py
# Example structure:
#
# mypackage/
# ├── __init__.py
# ├── module1.py
# └── module2.py
# ------------------------------------------
# IMPORTING FROM PACKAGE
# ------------------------------------------
# import entire module
# import mypackage.module1
# from package import module
# from mypackage import module1
# import specific function
# from mypackage.module1 import func
# ------------------------------------------
# USING __init__.py
# ------------------------------------------
# __init__.py can control package imports
# Example (__init__.py):
# from .module1 import func1
# from .module2 import func2
# then:
# from mypackage import func1
# ==========================================================
# RELATIVE IMPORTS (INSIDE PACKAGE)
# ==========================================================
# from . import module1 # same folder
# from .. import module2 # parent folder
# ==========================================================
# SUMMARY (IN COMMENTS)
# ==========================================================
# import:
# import module
# from import:
# from module import name
# from *:
# imports all (avoid)
# module attributes:
# __file__, __package__, __doc__, __dict__, __name__
# locating module:
# sys.path
# package:
# folder with __init__.py