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

  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

# file: 'app1.py'
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

# file: 'app2.py'
import my_module

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

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

start.py

Run Code 3 in Python

# file: 'app3.py'
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

# file: 'app4.py'
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

# file: 'app5.py'
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

# file: 'app6.py'
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

# file: 'app7.py'
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

# file: 'app8.py'
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

# file: 'app9.py'
# 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

# file: 'terminal'
# 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

# file: 'app10.py'
import my_module
import sys
sys.path

start.py

Windows

# file: 'terminal'

# 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

# file: 'app11.py'
import random

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

random_course = random.choice(courses)

start.py

Run Code 12 in Python

# file: 'app12.py'
import math

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

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

start.py

Run Code 13 in Python

# file: 'app13.py'
import datetime
import calendar

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

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

start.py

Run Code 14 in Python

# file: 'app14.py'
import os

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

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

start.py

Run Code 15 in Python

# file: 'app15.py'
import antigravity

start.py

Comments 💬