# Python Py_compile Module

The `py_compile` module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.

## Importing the Module

```python
import py_compile
```

## Compiling a File

The `py_compile.compile()` function compiles a source file to byte-code and writes out the byte-code cache file.

### Basic Usage

```python
import py_compile

# Compile a file named 'myscript.py'
# This will create a .pyc file in the __pycache__ directory
py_compile.compile('myscript.py')
```

### Specifying Output File

You can specify the name of the byte-code file using the `cfile` argument.

```python
import py_compile

py_compile.compile('myscript.py', cfile='myscript.pyc')
```

### Error Handling

By default, `compile()` does not raise an exception if compilation fails (it prints to stderr). To raise an exception, set `doraise=True`.

```python
import py_compile

try:
    py_compile.compile('broken_script.py', doraise=True)
except py_compile.PyCompileError as e:
    print(f"Compilation failed: {e}")
```

## Command Line Usage

You can also use this module as a script to compile several source files.

```bash
python -m py_compile file1.py file2.py
```

[[programming/python/python]]