-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyTutorial-Importing_Modules.py
56 lines (40 loc) · 1.87 KB
/
PyTutorial-Importing_Modules.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
# https://www.youtube.com/watch?v=CqvZ3vGoGs0&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&index=10&t=0s
'''
There are several ways to import a module
------------Modules in the same directory--------------------
import my_module - this will import and run the whole module on load
my_module.find_index(courses, "Math")
import my_module as mm - this will import the module with an alias (for easier typing)
mm.find_index(courses, "Math")
from my_module import find_index - this will only import the function find_index from my_module.
You do NOT neet to call the module this way.
find_index()
from my_module import find_index, test - this will only import the objects specified from my_module.
You do NOT neet to call the module this way.
find_index()
test
from my_module import find_index as fi, test - aliases can also created using this method.
fi()
from my_module import * - this can be used but is frowned upon as it can make code confusing.
------------Modules in a different directory--------------------
BEST PRACTICE SAYS TO USE A PACKAGE but you can do this
import sys - insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import module
'''
from my_module import find_index as fi, test #import find_index with an alias and the variable test. Nothing else will be imported.
import random #imports random from the standard library
import datetime
import calendar
import os #gives access to operating system
import webbrowser
courses = ['History', 'Math', 'Physics', 'CompSci']
print(fi(courses, "Math"))
random_course = random.choice(courses)
print(random_course)
today = datetime.date.today()
print(today)
print(calendar.isleap(2020))
print(os.getcwd())
print(os.__file__) #prints out the location of the file
#webbrowser.open("https://www.mcccu.org")