# Installing Python

Using Python and Astra Image together can produce some powerful image processing solutions. Before using Python in Astra Image, please make sure to set up Python correctly.

**Download and Install Python**\
If you have Python installed on your system already, you can skip this step.

Go to python.org and download one of the 'Windows installer (64-bit)' files. Follow the prompts to install Python on your system.

**Start a Command Prompt**\
Hold down the Windows key and press R (Win+R). Then type <mark style="color:yellow;">cmd</mark> in the Run box. Another way to do this is to click on the Start menu, type <mark style="color:yellow;">cmd</mark>, and then click on 'Command Prompt'.

To test your Python installation, type:

<mark style="color:yellow;">py</mark>

and press Enter. You should see something like this:

<mark style="color:yellow;">Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) \[MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.</mark>

You can type:

<mark style="color:yellow;">quit()</mark>

end press Enter to get back to the command prompt.&#x20;

**Setup a Virtual Python Environment**\
&#x20;Virtual environments are a very useful feature of Python. They allow us to setup an instance of Python that is isolated from system Python. If you make a mistake or something gets broken, you can delete the virtual environment folder and start again (but always be careful when deleting files. Make a backup if you are not sure). Now, let's set up a virtual Python environment for Astra Image.

At the command prompt, type:

<mark style="color:yellow;">py -m venv C:\Users\UserName\PythonAstraImage</mark>

(if you already have Python installed, you might be able to use: <mark style="color:yellow;">python -m venv C:\Users\UserName\PythonAstraImage</mark>)

and press Enter.&#x20;

You can change the path ('C:\Users\UserName\PythonAstraImage') to another location. But please remember the path you selected for the virtual environment.

After that, go to the the 'Scripts' folder where the virtual environment is installed:

<mark style="color:yellow;">cd C:\Users\UserName\PythonAstraImage\Scripts</mark>

and type:

<mark style="color:yellow;">activate</mark>

then press Enter.

Your command prompt should look like:

<mark style="color:yellow;">(PythonAstraImage) C:\Users\UserName\PythonAstraImage\Scripts></mark>

Install Packages\
You are now ready to install Python packages. Two common packages that you will most likely need are numpy and OpenCV. To install them, run these four commands:

<mark style="color:yellow;">cd C:\Users\UserName\PythonAstraImage</mark>\ <mark style="color:yellow;">pip install numpy</mark>\ <mark style="color:yellow;">pip install opencv-python</mark>\ <mark style="color:yellow;">pip install opencv-contrib-python</mark>

You might also want to install other packages, like PyTorch or SciKit.

**Python in Astra Image** \
Start Astra Image and click on 'Python Script Editor...' in the Script menu. You will see an error message that says Python cannot be found. Click OK, then you will see the script window:

<div align="left"><figure><img src="/files/TZzUkxtDxDaaACkzbRu8" alt="The Python script editor window."><figcaption><p>The Python script editor window.</p></figcaption></figure></div>

Click the Settings button on the toolbar (the right-most icon). You will then be able to select the location of your Python virtual environment:

<div align="left"><figure><img src="/files/e2gSedv56j0kNRgtcPrj" alt="The Python settings dialog."><figcaption><p>The Python settings dialog.</p></figcaption></figure></div>

You can use the '...' button to open the Select Folder dialog. Select the folder where you created the Python virtual environment (in this example, the path is 'C:\Users\UserName\PythonAstraImage') . Then click 'OK'.

You are now ready to write Python scripts.

**Verification**\
To test your installation, you can run a small script. In the script editor, type:

<mark style="color:orange;">import AstraImage as ai</mark>\ <mark style="color:orange;">import sys</mark>\ <mark style="color:orange;">print('Python version: ', sys.version)</mark>

Then press the Run button. In the output section of the editor screen, you should see the Python version information:

<mark style="color:orange;">Python version: 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) \[MSC v.1940 64 bit (AMD64)]</mark>

<div align="left"><figure><img src="/files/RViuysuqTJDTJvlXNYDJ" alt="Success!"><figcaption><p>Success!</p></figcaption></figure></div>

**Next Steps**\
Congratulations! You can now integrate Python scripts into Astra Image. For more information, please see the next topic, 'Writing Scripts'.


---

# 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://astra-image.gitbook.io/astra-image-6.0/python-scripts/installing-python.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.
