module-found 0.2.1
Automatically create modules instead of failing with ModuleNotFound
Requires Python: <4.0,>=3.8

# module-found

module-found is a fun, experimental Python project that takes the magic of Python's import system to a whole new level. Ever tried to import a module that doesn't exist? No worries, module-found has your back. Using OpenAI, this package dynamically generates the module and its functions on-the-fly.

![Module Found Example](https://raw.githubusercontent.com/LiadOz/module-found/master/static/module_found_example.gif)

**In the gif above, all modules imported were generated by AI.**

# Disclaimer
module-found is purely for educational and entertainment purposes. It's not intended for production use. If you're here for serious business, you might want to look elsewhere. But if you're here for a ridiculous and creative example of what you can do with Python's import machinery, you're in the right place!

# Installation
You can install module-found directly from PyPI:

```bash
pip install module-found
```

# Environment Variables
To make the most out of module-found, you'll need to configure a couple of environment variables:
* `MODULE_FOUND_KEY`: Your OpenAI API key. This is required for generating modules and functions on-the-fly.
* `MODULE_FOUND_MODEL`: (Optional) The model to use for generating code. Defaults to 'gpt-4o-mini' if not specified.

Make sure to set these environment variables before using module-found. You can do this in your shell:

```bash
export MODULE_FOUND_KEY="your-openai-api-key"
export MODULE_FOUND_MODEL="gpt-4o"
```

Alternatively, you can pass these values directly to the setup() function in your code:
```python
from module_found import setup

setup(api_key="your-openai-api-key", model="gpt-4o")
```

# Automatic Setup
When you install module-found via pip, it automatically enables itself thanks to a module_found.pth file included in the package. This file ensures that `module_found.setup()` is called as soon as the package is installed, so you can start importing non-existent modules right away.
NOTE: Automatic setup does not work when installing in development mode.

# Usage
Once installed and configured, you can start importing any module, and module-found will handle the rest. Here's an example:

```python
>>> from cs_interview_questions import pascal_triangle
>>> pascal_triangle(10)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
```

Each module generated by module-found has an attribute called `code` which can be used to check out the code that was generated. Here's an example:
```python
>>> from emojifier import emojify
>>> print(emojify(msg='hello world'))
👋 🌍

>>> import emojifier
>>> print(emojifier.code)
def emojify(**kwargs):
    msg = kwargs.get('msg', '')
    emoji_dict = {
        'hello': '👋',
        'world': '🌍'
    }

    for word, emoji in emoji_dict.items():
        msg = msg.replace(word, emoji)

    return msg
```

# How It Works
module-found leverages Python's sys.meta_path to hook into the import system. When you try to import a module that doesn't exist, module-found steps in, generates a LazyModule object, then, when a function is called it generates its code using OpenAI's API taking into account the passed arguments and keywords arguments.

Here's a brief overview of the key components:
* AiFinder: This is the class responsible for catching failed imports and delegating the module creation to AiLoader.
* AiLoader: Handles the actual module creation using OpenAI and loads it into your Python environment.
* LazyModule and LazyFunction: These are responsible for dynamically generating constants and functions within the newly created modules.