# 第36章-创建模块和包¶

## 如何创建python模块¶

```def add(x, y):
return x + y

def division(x, y):
return x / y

def multiply(x, y):
return x * y

def subtract(x, y):
return x - y
```

```import arithmetic

print(arithmetic.subtract(10, 5))
print(arithmetic.division(2, 7))
print(arithmetic.multiply(12, 6))
```

```def add(x, y):
return x + y

def division(x, y):
return x / y

def multiply(x, y):
return x * y

def subtract(x, y):
return x - y

if __name__ == "__main__":
import sys
print(sys.argv)
v = sys.argv[1].lower()
valOne = int(sys.argv[2])
valTwo = int(sys.argv[3])
if v == "a":
elif v == "d":
print(division(valOne, valTwo))
elif v == "m":
print(multiply(valOne, valTwo))
elif v == "s":
print(subtract(valOne, valTwo))
else:
pass
```

## 如何创建python包¶

```mymath/
__init__.py
__init__.py
sqrt.py
subtract.py
multiply.py
divide.py
```

```# sqrt.py
import math

def squareroot(n):
return math.sqrt(n)
```

```# outer __init__.py
from . divide import division
from . multiply import multiply
from . subtract import subtract
from .adv.sqrt import squareroot
```

```import sys

# modify this path to match your environment
sys.path.append('C:\Users\mdriscoll\Documents')

import mymath

print(mymath.division(4, 2))
print(mymath.multiply(10, 5))
print(mymath.squareroot(48))
```

```#!/usr/bin/env python

from setuptools import setup

# This setup is suitable for "python setup.py develop".

setup(name='mymath',
version='0.1',
description='A silly math package',
author='Mike Driscoll',
author_email='mike@mymath.org',
url='http://www.mymath.org/',
)
```

```python setup.py develop
```