Open tae898 opened 3 years ago
Hi there,
Exercise 2 of Chapter 13 is about creating two python files.
One is a module to be imported (i.e., my_utils.py
) and the other is a script that you’ll run directly (i.e., my_second_program.py
)
Yesterday during the lecture I created the two files as below:
my_utils.py
:
import os
import datetime
from random import randint
def get_current_directory():
"""Return the current working directory"""
to_return = os.getcwd()
return to_return
def get_current_time():
"""Return the current time."""
return datetime.datetime.now()
def get_random_int(low: int, high: int):
"""Sample a random integer from the uniform
distribution (low,high)."""
return randint(low, high)
foo = 'foo'
bar = [1,2,3,4]
my_second_program.py
:
import my_utils
print(my_utils.get_current_directory())
print()
print(my_utils.get_current_time())
from my_utils import foo, bar
print(foo, bar)
print()
from my_utils import get_random_int
print(get_random_int(-100, 100))
Make sure that both python files are in the same directory and your current directory in your terminal is also the directory where the two files are located.
Run python my_second_program.py
to see that this script successfully imports the module my_utils
,
and do stuff.
Original question: