# Python Scripting

All the **Python** programming features provided by [PyCharm IDE](https://www.jetbrains.com/pycharm/) are included with Robpod Studio.

**Python scripting** can be useful to develop client applications that control the robot or exchange data externally. In this scenario Robpod Studio can be used as an integrated platform to develop both native robot programs and external Python scripts, and test them in the same environment.

To create a new Python file:

1. Right click on **project folder ->  New -> Python File.**

   <figure><img src="/files/VqF46piaaCdDjKNCw9T1" alt=""><figcaption></figcaption></figure>
2. Enter the name of your script file.

You can work now with both robot native program and external Python scripts. If you want you could also display the two scripts side by side and work on them at the same time. To display a script in side by side mode: &#x20;

Right click on script name, then select **Open in Right Split.**

<figure><img src="/files/6J7tvdvkj7CGgLliDj2P" alt=""><figcaption></figcaption></figure>

To test the scripts you can run both of them at the same time:

1. To [execute the robot script](/robot/robot-toolbar/run-a-script.md), click on the robot program name and select![](/files/JuwVDf0ClZ6gTL7QGVpL)**Run On Robot**
2. To execute the Python script: click on the python script name and select ![](/files/2yfJnidh4cNLGaWN1rUt) **Run**.

{% embed url="<https://youtu.be/MV8yei3aHsc>" %}

### Example: UR external motion control with a Python Script&#x20;

In this example TCP socket comunication is used to share motion data between the robot program and a client Python script. In this way is possible to control the robot form an external application.

**Robot Program**

```python
# Universal Robots script for controlling the robot with external python script
while True:
    if socket_open("192.168.1.67", 8888):
        while True:
            values = socket_read_ascii_float(8)
            joints = [values[1], values[2], values[3], values[4], values[5], values[6]]
            if values[0] > 6:
                textmsg("Moving to: ", joints)
                movej(joints, v=values[7], a=values[8])
            else:
                socket_close()
                break
            end
            socket_send_string("target_reached")
        end

    else:
        textmsg("Connecting...")
        sleep(1)
    end
end
```

**External Client Script**

```python
import socket
# connect to robot
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 8888))
server.listen()
connection, address = server.accept()


# function to send move commands
def movej(pose, v=1, a=1):
    print("Moving to: {}".format(pose))
    connection.send("({}, {}, {}, {}, {}, {}, {}, {})\n".format(pose[0], pose[1], pose[2], pose[3], pose[4], pose[5], v, a).encode())
    buf = connection.recv(128)
    if len(buf) > 0:
        print(buf.decode())


# movements
movej([0.00, -1.57, -1.57, 0.00, 1.57, 0.00])
movej([0.00, -1.57, 0, 0.00, 1.57, 0.00])
movej([0.00, -1.57, -1.57, 0.00, 1.57, 0.00])
```


---

# Agent Instructions: 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:

```
GET https://docs.robpod.cloud/advanced-scripting/python-scripting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
