---
title: Python Modules
description: "> ## Python Modules – Python Модулиуд > > > > ####   ### What is a Module? – Модуль гэж юу вэ? Consider a module to be the same as a code library. Модуль нь кодын сантай ижил ажилладаг. A…"
url: "https://apprentice.mn/docs/python-modules/"
updated: "2024-07-30"
category: Python
---

# Python Modules

> ## Python Modules – Python Модулиуд
>
>
>
> ####  

### What is a Module? – Модуль гэж юу вэ?

Consider a module to be the same as a code library.

Модуль нь кодын сантай ижил ажилладаг.

A file containing a set of functions you want to include in your application.

Өөрийн програмдаа оруулахыг хүсэж буй функцуудыг агуулах файлыг хэлнэ.

---

### Create a Module – Модуль үүсгэх

To create a module just save the code you want in a file with the file extension `.py`:

Модуль үүсгэхийн тулд хэрэгтэй файл кодоо `.py` өргөтгөлтэй хадгална:

#### Example – Жишээ

Save this code in a file named `mymodule.py`

Энэ кодыг `mymodule.py` нэртэй файлд хадгална уу:

`def greeting(name):  print("Hello, " + name)`

## Use a Module – Модуль ашиглах

Now we can use the module we just created, by using the `import` statement:

Одоо бид `импорт` хийх замаар үүсгэсэн модулиа ашиглах боломжтой:

#### Example – Жишээ

Import the module named mymodule, and call the greeting function:

Одоо бид импорт хийх замаар үүсгэсэн модулиа ашиглах боломжтой:

`import mymodulemymodule.greeting("Jonathan")`

**Note:** When using a function from a module, use the syntax: *module_name.function_name*.

**Тэмдэглэл:** Модулийн функцыг ашиглахдаа *module_name.function_name* синтаксыг ашиглана.

## Variables in Module – Модулийн хувьсагчууд

The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):

Модуль нь функцууд төдийгүй бүх төрлийн хувьсагчууд (массив, лавлах, объект г.м)-ийг агуулж болно:

#### Example – Жишээ

Save this code in the file `mymodule.py`

Энэ кодыг `mymodule.py` файлд хадгална уу:

`person1 = {  "name": "John",  "age": 36,  "country": "Norway"})`

##### Example – Жишээ

Import the module named mymodule, and access the person1 dictionary:

mymodule нэртэй модулийг импортлон, person1 лавлахад хандах:

`import mymodulea = mymodule.person1["age"]print(a)`

## Naming a Module – Модулийн нэр өгөх

You can name the module file whatever you like, but it must have the file extension `.py`

Модулийн файлын нэрийг хүссэнээрээ өгөх боломжтой, гэхдээ файлын өргөтгөл нь `.py` байх шаардлагатай.

## Re-naming a Module – Модулийг дахин нэрлэх

You can create an alias when you import a module, by using the `as` keyword:

Импорт хийх үед `as` түлхүүр үгийг ашиглан модулийг өөр нэрээр дуудаж болно:

#### Example – Жишээ

Create an alias for `mymodule` called `mx`:

`mymodule`-г `mx` гэж нэрлэнэ:

`import mymodule as mxa = mx.person1["age"]print(a)`

## Built-in Modules – Бэлэн модулиуд

There are several built-in modules in Python, which you can import whenever you like.

Python-д олон төрлийн бэлэн модуль байдаг бөгөөд хүссэн үедээ тэдгээрийг импортлон ашиглаж болно.

#### Example – Жишээ

Import and use the `platform` module:

`platform` модулийг импортлон ашиглах:

`import platformx = platform.system()print(x)`

## Using the dir() Function – dir() Функц ашиглах

There is a built-in function to list all the function names (or variable names) in a module. The `dir()` function:

Модульд байгаа бүх функцийн нэрийг (эсвэл хувьсагчийн нэр) жагсаах функц байдаг. Функц `dir()`:

#### Example – Жишээ

List all the defined names belonging to the platform module:

`platform` модулийн бүх тодорхойлсон нэрсийг жагсаах:

`import platformx = dir(platform)print(x)`

**Note:** The dir() function can be used on *all* modules, also the ones you create yourself.

**Тэмдэглэл:** dir() функцийг *бүх* модуль, мөн өөрийн үүсгэсэн модулиудад ашиглаж болно.

## Import From Module – Модулиас импортлох

You can choose to import only parts from a module, by using the `from` keyword.

`from` түлхүүр үгийг ашиглан модуль дотроос зөвхөн тодорхой хэсгийг импорт хийж болно.

##### Example – Жишээ

The module named `mymodule` has one function and one dictionary:

mymodule нэртэй модуль нэг функц болон нэг лавлах агуулагдаж байна:

`def greeting(name):  print("Hello, " + name)person1 = {  "name": "John",  "age": 36,  "country": "Norway"}`

#### Example – Жишээ

Import only the person1 dictionary from the module:

Модулиас зөвхөн person1 лавлахыг импортлох:

`from mymodule import person1
print (person1["age"])`

**Note:** When importing using the `from` keyword, do not use the module name when referring to elements in the module. Example: `person1["age"]`, **not** `mymodule.person1["age"]`

**Тэмдэглэл:** `from` түлхүүр үг ашиглаж импорт хийх үед модулийн нэрийг ашиглахгүйгээр элементүүдэд хандана. Жишээ нь: `person1["age"]` гэж бичих бөгөөд `mymodule.person1["age"]` гэж бичихгүй.

            [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-modules%2F&linkname=Python%C2%A0Modules)[https://www.addtoany.com/share](https://www.addtoany.com/share)
