Python Tutorial for Beginners - 8. Import Modules and Exploring The Standard Library

Table of Contents

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Run Code 3 in Python
  4. Run Code 4 in Python
  5. Run Code 5 in Python
  6. Run Code 6 in Python
  7. Run Code 7 in Python
  8. Run Code 8 in Python
  9. Run Code 9 in Python
  10. Linux/Mac
  11. Run Code 10 in Python
  12. Windows
  13. Run Code 11 in Python
  14. Run Code 12 in Python
  15. Run Code 13 in Python
  16. Run Code 14 in Python
  17. Run Code 15 in Python

Run Code 1 in Python

1
2
3
4
5
6
7
8
9
10
11
12
print('Imported my_module...')

test = 'Test String'


def find_index(to_search, target):
    '''Find the index of a value in a sequence'''
    for i, value in enumerate(to_search):
        if value == target:
            return i

    return -1

my_module.py

Run Code 2 in Python

1
2
3
4
5
6
import my_module

courses = ['History', 'Math', 'Physics', 'CompSci']

index = my_module.find_index(courses, 'Math')
print(index)

start.py

Run Code 3 in Python

1
2
3
4
5
6
import my_module as mm

courses = ['History', 'Math', 'Physics', 'CompSci']

index = mm.find_index(courses, 'Math')
print(index)

start.py

Run Code 4 in Python

1
2
3
4
5
6
from my_module import find_index

courses = ['History', 'Math', 'Physics', 'CompSci']

index = find_index(courses, 'Math')
print(index)

start.py

Run Code 5 in Python

1
2
3
4
5
6
7
from my_module import find_index, test

courses = ['History', 'Math', 'Physics', 'CompSci']

index = find_index(courses, 'Math')
print(index)
print(test)

start.py

Run Code 6 in Python

1
2
3
4
5
6
7
from my_module import find_index as fi, test

courses = ['History', 'Math', 'Physics', 'CompSci']

index = fi(courses, 'Math')
print(index)
print(test)

start.py

Run Code 7 in Python

1
2
3
4
5
6
7
from my_module import *

courses = ['History', 'Math', 'Physics', 'CompSci']

index = find_index(courses, 'Math')
print(index)
print(test)

start.py

Run Code 8 in Python

1
2
3
4
5
6
7
8
9
10
from my_module import find_index, test
import sys

courses = ['History', 'Math', 'Physics', 'CompSci']

index = find_index(courses, 'Math')
# print(index)
# print(test)

print(sys.path)

start.py

Run Code 9 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# move module file to desktop

import sys
sys.path.append('/Users/mt/Desktop/My-Modules') # move file to Desktop/My-Modules

from my_module import find_index, test


courses = ['History', 'Math', 'Physics', 'CompSci']

index = find_index(courses, 'Math')
# print(index)
# print(test)

print(sys.path)

start.py

Linux/Mac

1
2
3
4
5
6
7
8
# goto terminal
nano ~/.bash_profile

# insert
export PYTHONPATH="/Users/mt/Desktop/My-Modules"

ctrl+x Y
# restart terminal

start.py

Run Code 10 in Python

1
2
3
import my_module
import sys
sys.path

start.py

Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# start
# righ click on computer and click on propertis
# Advanced system seting
# Advandes
# Environment Variables
# Create - NEW 
#   Variable name: PYTHONPATH
#   Variable value: C:\Users\MT\Desktop\My-Modules
# OK OK

# Open Command promt

# python
import my_module
import sys
sys.path

start.py

Run Code 11 in Python

1
2
3
4
5
import random

courses = ['History', 'Math', 'Physics', 'CompSci']

random_course = random.choice(courses)

start.py

Run Code 12 in Python

1
2
3
4
5
6
import math

courses = ['History', 'Math', 'Physics', 'CompSci']

rads = math.radians(90)
print(math.sin(rads))

start.py

Run Code 13 in Python

1
2
3
4
5
6
7
import datetime
import calendar

courses = ['History', 'Math', 'Physics', 'CompSci']

today = datetime.date.today()
print(today)

start.py

Run Code 14 in Python

1
2
3
4
5
6
import os

courses = ['History', 'Math', 'Physics', 'CompSci']

print(os.getcwd())
print(os.__file__)

start.py

Run Code 15 in Python

1
import antigravity

start.py