Howard Chen's Blog

CODE IS POETRY

Learn Python - Module

In the previous topic, it includes some really useful points of Python basics. In this page, we will try to use Modules in Python.

Custom module

There are two ways of using modules. Let’s first create a new Python file names module1.py.

1
2
def say_hello(): 
print('Hello Howard.')

In another Python file that wants to use the functions in the module1.py, we need the following ways to reference this module.

1
2
3
# method 1. note, no file extension here
import module1
module1.say_hello()

1
2
3
# method 2. note, we don't have the module name before the function name
from module1 import say_hello
say_hello()

Choose the two methods above wisely and it makes your code easier to read.

Use a build-in module

We are going to download an image from internet and store in disk. Here is the code.

1
2
3
4
5
from urllib import request

url = 'https://avatars2.githubusercontent.com/u/8789008?v=4&s=460'
filename = '1.jpg'
request.urlretrieve(url, filename)

In this code, we import an urllib module. You could use any module like this to support various requirements.

Proudly powered by Hexo and Theme by Hacker
© 2019 Howard Chen