> For the complete documentation index, see [llms.txt](https://docs.rhinofcp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rhinofcp.com/rhino-sdk/creating-a-python-code-object-using-the-rhino-sdk.md).

# Creating a Python Code Object Using the Rhino SDK

This article explains how to use the Rhino SDK to create a Python code object.

Note: To learn how to create a Python Code Object using the Rhino FCP UI, see [Creating and Running a Python Code Object](/creating-and-running-code-objects/creating-and-running-a-python-code-object.md).

### Prerequisites

Before starting this process, you should have already:

1. Created a Project using the Rhino SDK or the User Interface (UI).
2. Created an Input Data Schema using the Rhino SDK or UI.
3. \[Optional] Created an Output Data Schema using the Rhino SDK or UI. If you choose not to create an output Data Schema, the system will auto-generate the Data Schema of the output for your Code.

### Import your Python Dependencies

```python
import rhino_health as rh
from rhino_health.lib.endpoints.code.code_objects_dataclass import (
    CodeObject,
    CodeObjectCreateInput,
    CodeTypes,
    CodeRunType
)
import getpass
```

Note: Remember to change all lines with CHANGE\_ME comments above them in all the blocks below.

### Log into the Rhino SDK using your FCP Credentials

Your username will be the email address you log into the Rhino FCP platform with.

```python
print("Logging In")
# CHANGE_ME: MY_USERNAME
my_username = "MY_USERNAME"

session = rh.login(username=my_username, password=getpass.getpass())
print("Logged In")
```

### Get Supporting FCP Information Needed to Create Your Code Object

At this point, you will need the name of your project, your *input Data Schema* name, optionally your *output Data Schema* name, and your Python code. You can also retrieve each object's UUID by following the instructions here: [Frequently Asked Questions (FAQs)](/frequently-asked-questions/frequently-asked-questions-faqs.md)

```python
# CHANGE_ME: YOUR_FCP_PROJECT_NAME
project = session.project.get_project_by_name('YOUR_FCP_PROJECT_NAME')

# CHANGE_ME: INPUT_SCHEMA_NAME & Possibly Version Number too
input_schema = session.project.get_data_schema_by_name('INPUT_SCHEMA_NAME', project_uid=project.uid, version=1)

# CHANGE_ME: OUTPUT_SCHEMA_NAME & Possibly Version Number too
output_schema = session.project.get_data_schema_by_name('OUTPUT_SCHEMA_NAME', project_uid=project.uid, version=1)

# CHANGE_ME: PYTHON_CODE
python_code = """PYTHON_CODE"""
```

### Create Your Python Code Object

During the creation of your `CodeObject`, you will provide it with various pieces of data similar to how a Code Object is created within the UI. You will give the `CodeObject` a name, description, `input_data_schema_uids`, `output_data_schema_uids`, `project_uid`, `code_object_type`, and the `config` where you will provide your Python code and other information dependent on your code run type.

{% tabs %}
{% tab title="Default Code Run Type" %}

```auto
code_object_params = CodeObjectCreateInput(
	# CHANGE_ME: CODE_OBJECT_NAME
	name = "CODE_OBJECT_NAME",

	# CHANGE_ME: CODE_OBJECT_DESCRIPTION
	description = "CODE_OBJECT_DESCRIPTION",

	input_data_schema_uids = [input_schema.uid],
        # output_schema.uid or None to Infer Output Data Schema
	output_data_schema_uids = [output_schema.uid],
	project_uid = project.uid,

	code_object_type = CodeTypes.PYTHON_CODE,

	config = {
		"code_run_type": CodeRunType.DEFAULT,
		"python_code": python_code
	}
)

code_object = session.code.create_code_object(code_object_params)
print(f"Got Code Object '{code_object.name}' with UID {code_object.uid}")
```

{% endtab %}

{% tab title="Auto-Container Snippet Code Run Type" %}

```auto
code_object_params = CodeObjectCreateInput(
	# CHANGE_ME: CODE_OBJECT_NAME
	name = "CODE_OBJECT_NAME",

	# CHANGE_ME: CODE_OBJECT_DESCRIPTION
	description = "CODE_OBJECT_DESCRIPTION",

	input_data_schema_uids = [input_schema.uid],
        # output_schema.uid or None to Infer Output Data Schema
	output_data_schema_uids = [output_schema.uid],
	project_uid = project.uid,

	code_object_type = CodeTypes.PYTHON_CODE,

	config = {
		"code_run_type": CodeRunType.AUTO_CONTAINER_SNIPPET,
		"python_code": python_code
	}
)

code_object = session.code.create_code_object(code_object_params)
print(f"Got Code Object '{code_object.name}' with UID {code_object.uid}")
```

{% endtab %}

{% tab title="Auto-Container File Code Run Type" %}

```auto
code_object_params = CodeObjectCreateInput(
	# CHANGE_ME: CODE_OBJECT_NAME
	name = "CODE_OBJECT_NAME",

	# CHANGE_ME: CODE_OBJECT_DESCRIPTION
	description = "CODE_OBJECT_DESCRIPTION",

	input_data_schema_uids = [input_schema.uid],
        # output_schema.uid or None to Infer Output Data Schema
	output_data_schema_uids = [output_schema.uid],
	project_uid = project.uid,

	code_object_type = CodeTypes.PYTHON_CODE,

	config = {
		"code_run_type": CodeRunType.AUTO_CONTAINER_FILE,
		"python_code": python_code
	}
)

code_object = session.code.create_code_object(code_object_params)
print(f"Got Code Object '{code_object.name}' with UID {code_object.uid}")
```

{% endtab %}
{% endtabs %}

**Getting Help**

If you have received an error or run into any issues throughout the process, please reach out to <support@rhinohealth.com> for more assistance.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rhinofcp.com/rhino-sdk/creating-a-python-code-object-using-the-rhino-sdk.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
