/*
print('Imported my_module...')
test = 'Test String'
def find_index(to_search, target):
"""Find the index of a target value in a sequence"""
for i, value in enumerate(to_search):
if value == target:
return i
return -1
import module_name
Put the module in the same folder as the current python file. And use import module_name
to import the module_name.py
file.
import my_module
courses = ['History', 'Math', 'Physics', 'CompSci']
index = my_module.find_index(courses, 'Math')
print(index)
Output:
Imported my_module...
1
To make the module name shorter, one can define a shorter name by import module_name as module_shorter
. When call the module, one can use module_shorter
instead.
import my_module as mm
courses = ['History', 'Math', 'Physics', 'CompSci']
index = mm.find_index(courses, 'Math')
print(index)
Output:
Imported my_module...
1
from my_module import find_index
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(index)
Output:
Imported my_module...
1
One thing to note about this approach, it only gives access to the find_index()
function not everything else in the module.
as
still works to make a shortcut for a function by from module_name import func_name as func_shorter
. But this will reduce the readability of the code, so it’s not recommended to do very often.
from my_module import find_index as fi, test
courses = ['History', 'Math', 'Physics', 'CompSci']
index = fi(courses, 'Math')
print(index)
print(test)
Output:
Imported my_module...
1
Test String
To import every thing, one can use from module_name import *
. This is not used very often, because it’s hard to track the original module
of a function
.
import
searchesWhen import
a module or the standard library, the system is gonna check a multiple locations. And the locations that are checked, is within a list called sys.path
.
import sys
print(sys.path)
Output:
['d:\\Documents\\Code\\python\\tutorial\\9_import modules-library', 'D:\\Program Files\\Python\\Python310\\python310.zip', 'D:\\Program Files\\Python\\Python310\\DLLs', 'D:\\Program Files\\Python\\Python310\\lib', 'D:\\Program Files\\Python\\Python310', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32\\lib', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\Pythonwin']
It contains the current directory
of the script that is running, Python path environment variable
, standard library
directory, site-packages
directory for 3rd party packages.
If the imported module is not in the folder, there’s a ModuleNotFoundError
appears.
from my_module import find_index, test
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(index)
print(test)
Output:
Traceback (most recent call last):
File "d:\Documents\Code\python\tutorial\9_import modules-library\9_import modules-standard library.py", line 40, in
from my_module import find_index, test
ModuleNotFoundError: No module named 'my_module'
sys.path.append('folder_address')
# Append Path
import sys
sys.path.append('D:\Documents\Code\python\HG_modules')
from my_module import find_index
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print("The index of 'Math' in courses is {}".format(index))
# print(f"The index of 'Math' in courses is {index}")
print(sys.path)
Output:
Imported my_module...
The index of 'Math' in courses is 1
['d:\\Documents\\Code\\python\\tutorial\\9_import modules-library', 'D:\\Program Files\\Python\\Python310\\python310.zip', 'D:\\Program Files\\Python\\Python310\\DLLs', 'D:\\Program Files\\Python\\Python310\\lib', 'D:\\Program Files\\Python\\Python310', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32\\lib', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\Pythonwin', 'D:\\Documents\\Code\\python\\HG_modules']
The above is not the best way to add the address to python, because it append
before other import
. It is better to change the environment variable, and doesn’t need to add the sys.path.append()
at each time modules from certain location is called.
In the terminal, use nano
to change the .bash_profile
file. ~/
means working in the home directory
.
nano ~/.dash_profile
Scroll to the end of the file and set the python path.
export PYTHONPATH="/Users/your_user_name/file_location"
Here, your_user_name
is your logged in user name, and file_location
is where the module is located. Note that, no space in between the equal and the path.
Hit ctrl + x
, then y
to save the change, and enter
to keep the same file name. Finally, to restart the terminal.
Right click Computer 此电脑
→ \rightarrow → Properties 属性
→ \rightarrow → Advanced system settings 高级系统设置
→ \rightarrow → Environment Variable 环境变量
User variables 用户变量
, click New 新建
, set Variable name 变量名
as PYTHONPATH
, and Variable value
as the folder address of the self-defined modules. Finally click ok
to save the change.import sys
print(sys.path)
Output:
['d:\\Documents\\Code\\python\\tutorial\\9_import modules-library', 'D:\\Documents\\Code\\python\\HG_modules', 'D:\\Program Files\\Python\\Python310\\python310.zip', 'D:\\Program Files\\Python\\Python310\\DLLs', 'D:\\Program Files\\Python\\Python310\\lib', 'D:\\Program Files\\Python\\Python310', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\win32\\lib', 'D:\\Program Files\\Python\\Python310\\Lib\\site-packages\\Pythonwin']
The 1st address is the current working directory. It’s clear that the 2nd folder is added to the python environment successfully.
random
modulerandom.choice()
picks up a random element in a sequence.import random
courses = ['History', 'Math', 'Physics', 'CompSci']
random_course = random.choice(courses)
print(random_course)
Output:
Math
math
modulemath
is for common mathematical operations.
import math
rads = math.radians(90)
print(rads)
print(math.sin(rads))
Output:
1.5707963267948966
1.0
datetime
and calendar
modulesimport datetime
import calendar
today = datetime.date.today()
print(today)
print(calendar.isleap(2023))
Output:
2023-03-17
False
os
moduleos
gives access to the underlying operating system.
import os
print(os.getcwd())
Output:
d:\Documents\Code\python\tutorial\9_import modules-library
os
module has tons of other functionalities. It gives the ability to scan
the file system, and create
files, delete
files.
The standard library modules are simply python files. To view the location of a module, just print out its os.__file__
.
import os
import numpy as np
print(os.__file__)
print(np.__file__)
Output:
D:\Program Files\Python\Python310\lib\os.py
D:\Program Files\Python\Python310\Lib\site-packages\numpy\__init__.py
And D:\Program Files\Python\Python310\lib
is the standard library directory and it’s all the standard modules that are located.