A Byte of Python - Modules Python


함수를 일단 정의함으로서 프로그램에서 코드를 재사용할 수 있음을 봤다. 만약 다른 프로그램의 여러함수를 전부 재사용하기를 원한다면 어떻게 할까? 이 것이 모듈이다.

모듈을 작성하는 방법은 많지만 가장 단순한 방법은 함수와 변수가 포함된 .py 파일을 작성하는 것이다.

다른 방법은 파이썬 인터프리터 자체로 네이티브 언어로 작성된 모듈을 작성하는 방법이다. 예를들어, C언어로 모듈을 작성하고 표준 파이썬 인터프리터로 파이썬 코드에서 사용하는 것이다.

모듈은 다른 프로그램에 의해 기능의 사용을 위해 임포트될 수 있다. 이는 파이썬 표준 라이브러리도 마찬가지인데, 먼저 어떻게 표준 라이브러리 모듈을 사용하는지 보여준다.

import sysprint('The command line arguments are:')for i in sys.argv:    print(i)print('\n\nThe PYTHONPATH is', sys.path, '\n')


$ python module_using_sys.py we are arguments    # each arg is separated by white spaceThe command line arguments are:module_using_sys.pyweareargumentsThe PYTHONPATH is ['/tmp/py',# many entries here, not shown here'/Library/Python/2.7/site-packages','/usr/local/lib/python2.7/site-packages']

How It Works

First, we import the sys module using the import statement. Basically, this translates to us telling Python that we want to use this module. The sys module contains functionality related to the Python interpreter and its environment i.e. the system.

When Python executes the import sys statement, it looks for the sys module. In this case, it is one of the built-in modules, and hence Python knows where to find it.

If it was not a compiled module i.e. a module written in Python, then the Python interpreter will search for it in the directories listed in its sys.path variable. If the module is found, then the statements in the body of that module are run and the module is made available for you to use. Note that the initialization is done only the first time that we import a module.

The argv variable in the sys module is accessed using the dotted notation i.e. sys.argv. It clearly indicates that this name is part of the sys module. Another advantage of this approach is that the name does not clash with any argv variable used in your program.

The sys.argv variable is a list of strings (lists are explained in detail in a later chapter). Specifically, the sys.argv contains the list of command line arguments i.e. the arguments passed to your program using the command line.

If you are using an IDE to write and run these programs, look for a way to specify command line arguments to the program in the menus.

Here, when we execute python module_using_sys.py we are arguments, we run the module module_using_sys.py with the python command and the other things that follow are arguments passed to the program. Python stores the command line arguments in the sys.argv variable for us to use.

Remember, the name of the script running is always the first element in the sys.argv list. So, in this case we will have 'module_using_sys.py' as sys.argv[0]'we' as sys.argv[1]'are' as sys.argv[2] and 'arguments' as sys.argv[3]. Notice that Python starts counting from 0 and not 1.

The sys.path contains the list of directory names where modules are imported from. Observe that the first string in sys.path is empty - this empty string indicates that the current directory is also part of the sys.path which is same as the PYTHONPATH environment variable. This means that you can directly import modules located in the current directory. Otherwise, you will have to place your module in one of the directories listed in sys.path.

Note that the current directory is the directory from which the program is launched. Run import os; print(os.getcwd()) to find out the current directory of your program.

Byte-compiled .pyc files

Importing a module is a relatively costly affair, so Python does some tricks to make it faster. One way is to create byte-compiled files with the extension .pyc which is an intermediate form that Python transforms the program into (remember the introduction section on how Python works?). This .pyc file is useful when you import the module the next time from a different program - it will be much faster since a portion of the processing required in importing a module is already done. Also, these byte-compiled files are platform-independent.

NOTE: These .pyc files are usually created in the same directory as the corresponding .py files. If Python does not have permission to write to files in that directory, then the .pyc files will not be created.

from .. import 문

If you want to directly import the argv variable into your program (to avoid typing the sys. everytime for it), then you can use the from sys import argv statement.

WARNING: In general, avoid using the from..import statement, use the import statement instead. This is because your program will avoid name clashes and will be more readable.


from math import sqrtprint("Square root of 16 is", sqrt(16))

모듈의 __name__

Every module has a name and statements in a module can find out the name of their module. This is handy for the particular purpose of figuring out whether the module is being run standalone or being imported. As mentioned previously, when a module is imported for the first time, the code it contains gets executed. We can use this to make the module behave in different ways depending on whether it is being used by itself or being imported from another module. This can be achieved using the __name__attribute of the module.

Example (save as module_using_name.py):

if __name__ == '__main__':    print('This program is being run by itself')else:    print('I am being imported from another module')


$ python module_using_name.pyThis program is being run by itself$ python>>> import module_using_nameI am being imported from another module>>>

How It Works

Every Python module has its __name__ defined. If this is '__main__', that implies that the module is being run standalone by the user and we can take appropriate actions.

모듈 만들기

Creating your own modules is easy, you've been doing it all along! This is because every Python program is also a module. You just have to make sure it has a .py extension. The following example should make it clear.

Example (save as mymodule.py):

def say_hi():    print('Hi, this is mymodule speaking.')__version__ = '0.1'

The above was a sample module. As you can see, there is nothing particularly special about it compared to our usual Python program. We will next see how to use this module in our other Python programs.

Remember that the module should be placed either in the same directory as the program from which we import it, or in one of the directories listed in sys.path.

Another module (save as mymodule_demo.py):

import mymodulemymodule.say_hi()print('Version', mymodule.__version__)


$ python mymodule_demo.pyHi, this is mymodule speaking.Version 0.1

How It Works

Notice that we use the same dotted notation to access members of the module. Python makes good reuse of the same notation to give the distinctive 'Pythonic' feel to it so that we don't have to keep learning new ways to do things.

Here is a version utilising the from..import syntax (save as mymodule_demo2.py):

from mymodule import say_hi, __version__say_hi()print('Version', __version__)

The output of mymodule_demo2.py is same as the output of mymodule_demo.py.

Notice that if there was already a __version__ name declared in the module that imports mymodule, there would be a clash. This is also likely because it is common practice for each module to declare it's version number using this name. Hence, it is always recommended to prefer the import statement even though it might make your program a little longer.

You could also use:

from mymodule import *

This will import all public names such as say_hi but would not import __version__ because it starts with double underscores.

WARNING: Remember that you should avoid using import-star, i.e. from mymodule import *.

Zen of Python

One of Python's guiding principles is that "Explicit is better than Implicit". Run import this in Python to learn more.

dir 함수

빌트인 dir() 함수는 객체에의해 정의된 이름 리스트를 반환한다. 만약 객체가 모듈이면 이 리스트는 모듈내에 함수, 클래스와 변수들, 디파인드 이다.

이 함수는 매개변수도 받는데, 만약 매개변수가 모듈의 이름이면 지정된 모듈로부터 이름 리스트를 반환한다. 만약 매개변수가 없으면 함수는 현 모듈로부터 이름 리스트를 반환한다.

$ python>>> import sys# get names of attributes in sys module>>> dir(sys)['__displayhook__', '__doc__','argv', 'builtin_module_names','version', 'version_info']# only few entries shown here# get names of attributes for current module>>> dir()['__builtins__', '__doc__','__name__', '__package__', 'sys']# create a new variable 'a'>>> a = 5>>> dir()['__builtins__', '__doc__', '__name__', '__package__', 'sys', 'a']# delete/remove a name>>> del a>>> dir()['__builtins__', '__doc__', '__name__', '__package__', 'sys']

How It Works

First, we see the usage of dir on the imported sys module. We can see the huge list of attributes that it contains.

Next, we use the dir function without passing parameters to it. By default, it returns the list of attributes for the current module. Notice that the list of imported modules is also part of this list.

In order to observe dir in action, we define a new variable a and assign it a value and then check dir and we observe that there is an additional value in the list of the same name. We remove the variable/attribute of the current module using the del statement and the change is reflected again in the output of the dir function.

A note on del: This statement is used to delete a variable/name and after the statement has run, in this case del a, you can no longer access the variable a - it is as if it never existed before at all.

Note that the dir() function works on any object. For example, run dir(str) for the attributes of the str (string) class.

There is also a vars() function which can potentially give you the attributes and their values, but it will not work for all cases.


이제, 프로그램의 조직의 계층을 관찰하는 것으로 부터 시작해야 한다. 변수는 보통 함수내부로 간다. 함수와 전역 변수는 모듈로 들어간다. 모듈을 조직화하려면 어떻게 할 것인가? 이게바로 패키지이다.

패키지는 __init__.py 파일을 가진 모듈의 폴더로서 파이썬에게 이 폴더는 파이썬 모듈을 포함함을 나타낸다.

Let's say you want to create a package called 'world' with subpackages 'asia', 'africa', etc. and these subpackages in turn contain modules like 'india', 'madagascar', etc.

This is how you would structure the folders:

- <some folder present in the sys.path>/    - world/        - __init__.py        - asia/            - __init__.py            - india/                - __init__.py                - foo.py        - africa/            - __init__.py            - madagascar/                - __init__.py                - bar.py

Packages are just a convenience to organize modules hierarchically. You will see many instances of this in the standard library.


Just like functions are reusable parts of programs, modules are reusable programs. Packages are another hierarchy to organize modules. The standard library that comes with Python is an example of such a set of packages and modules.

We have seen how to use these modules and create our own modules.

Next, we will learn about some interesting concepts called data structures.


댓글 입력 영역