**Where will your Python program run?**
* **On your computer:** use this page to install Python and MartyPy locally.
* **On a Raspberry Pi attached to Marty:** use [[martyv2:userguides:python:setting_up_python_on_raspberry_pi|Setting Up Python on Raspberry Pi]] instead. Your Chromebook, Windows PC or Mac only provides remote access; it does not need a local Python installation. Start with [[martyv2:userguides:raspberry_pi:primer|the Pi access guide]] if you cannot yet reach the Pi.
**Chromebook Linux is only needed for the local Chromebook route below, not for accessing and programming a Raspberry Pi remotely.**
===== Step 1: Installing Python =====
First you need to make sure you have the Python programming language and the ''pip'' command installed on your computer.
**martypy requires Python 3. We recommend using a recent version of Python to ensure optimal performance and compatibility.**
If you already have Python and pip installed, you can check the versions by opening a terminal and entering ''python %%--%%version'' and ''pip %%--%%version''.
In windows you can open a terminal window (hit start and type terminal) and try the same commands.
Remember to also try ''python3'' if ''python'' doesn't work.
If you are setting up Python on a Raspberry Pi, have a look here:
[[:martyv2:userguides:python:setting_up_python_on_raspberry_pi|{{:martyv2:programming:python:rpi-logo-reg-screen.png?50}}]] [[Setting Up Python on Raspberry Pi]]
==== Installing Python on Windows ====
Go to [[https://www.python.org/downloads/windows/]] and download the latest release.
Follow the installation instructions provided by Python. If you are given an option to "Add Python to PATH", we recommend enabling it. Once installation is complete, open a terminal and enter ''python %%--%%version'' to check that Python is installed correctly.
==== Installing Python on Chromebook (Linux Support) ====
**This section is only for running Python on the Chromebook itself.** If your program will run on a Raspberry Pi attached to Marty, skip these steps and use [[martyv2:userguides:raspberry_pi:primer|the Pi remote-access guide]] followed by [[martyv2:userguides:python:setting_up_python_on_raspberry_pi|Python setup on the Pi]]. Chromebook Linux is not required for that route.
School-managed Chromebooks may have Linux disabled by policy. Ask your administrator whether local Linux development is permitted; **do not enable Developer Mode or try to bypass school restrictions.** See [[https://support.google.com/chromebook/answer/9145439|Google's Chromebook Linux setup instructions]] for current settings and device support.
=== Enable Linux on Your Chromebook ===
If your administrator permits it, open ChromeOS Settings, find **Linux development environment** under **Developers**, and follow the setup instructions. Open the resulting Linux Terminal application. **The commands in this section run in Chromebook Linux**, unlike the separate Pi guide.
=== Install Python and create an environment ===
Run these commands one at a time:
sudo apt update
sudo apt install python3 python3-pip python3-venv
python3 --version
python3 -m venv ~/marty-env
source ~/marty-env/bin/activate
You should see a Python 3 version and then ''%%(marty-env)%%'' in the terminal prompt after activation. Continue to **Step 2** below from this same terminal. Use ''%%python -m pip%%'' inside the environment to install packages. Do not bypass an ''%%externally-managed-environment%%'' error with ''%%sudo pip%%'' or ''%%--break-system-packages%%''.
On a later session, run ''%%source ~/marty-env/bin/activate%%'' again before installing libraries or running your code. Alternatively, run your saved program with ''%%~/marty-env/bin/python PATH_TO_YOUR_SCRIPT.py%%'', replacing the script path.
=== Optional: choose an editor ===
A terminal editor or a permitted desktop editor can be used. For VS Code, follow [[https://code.visualstudio.com/docs/setup/linux|Microsoft's Linux installation instructions]]; a fresh Linux environment does not necessarily have the package repository needed for ''%%sudo apt install code%%''. Set the editor's interpreter to the Python inside ''%%~/marty-env%%''.
=== Check Python ===
With the environment active, run:
python -c "print('Hello, Python!')"
This verifies local Python only. It does not confirm that Marty is reachable through your school's network or the Chromebook Linux environment. Follow the connection instructions in [[martyv2:userguides:python:first_steps_with_python|First Steps With Python]] separately.
===== Installing the martypy Python Library =====
If you are working on a Raspberry Pi, use [[martyv2:userguides:python:setting_up_python_on_raspberry_pi|the Pi installation steps]] instead. For the Chromebook Linux route, keep the virtual environment from the previous section active. Open your terminal (command prompt) and type the following:
```shell
python -m pip install martypy
```
**MartyPy 3.8.1 and later install the speech audio components automatically through pip.** You do not need to install separate audio software or change PATH for speech. Speech synthesis and translation still require an internet connection.
This will install the martypy Python library and its dependencies onto your computer. The exact output varies depending on which packages are already installed, but it should include a success message like this:
```shell
Successfully installed martypy-3.8.1
```
==== Updating martypy: ====
__(No need to do this if you just installed martypy.)__
If something doesn't work, you can try updating martypy - maybe it has already been fixed. Updating is very similar to installing martypy. To get the newest version, run the following command:
```shell
python -m pip install --upgrade martypy
```
**Older versions:** MartyPy versions earlier than **3.8.1** require a separate [[https://ffmpeg.org/download.html|FFmpeg installation]] for speech. Upgrade using the command above to use the audio components installed automatically by pip instead.
===== Making sure it worked =====
{{:martyv2:programming:python:screenshot_2020-12-18_173141.png?300}}
Launch IDLE (usually installed along with Python) – this is an application that can be used for executing Python commands interactively.
If you don't have IDLE, don't worry! You can also just open up a terminal window (command prompt) and type the following:
```shell
python
```
(Or `python3`, on some systems.)
Now you have a _Python Shell_ in front of you. It is great for executing short commands and trying out a few things. For now, let's just verify that martypy was installed correctly.
Type `import martypy` and hit Enter. A new set of three arrows (`>>>`) should appear and now you can type `martypy.__version__`. The output should be `'3.8.1'` or a later version. The screenshot below is from an older release, so its version number will differ.
{{:martyv2:programming:python:screenshot_from_2020-12-18_18-30-22.png}}
To do something more exciting with your Python Shell, have a look at [[martyv2:userguides:python:first_steps_with_python|First Steps With Python]]. If you would like to write a bit more than just trivial Python programs, you will need a good text editor so you you can also carry on with the next section while you are setting things up.
If you started your Python Shell from a terminal (command prompt), you can go back there by typing `exit()`.
===== Setting up a Text Editor =====
Python programs are written in plain text so you will also need a text editor to write them.
For Windows, Mac and Linux one of the best choices (if you don’t have a personal preference already) is [[https://code.visualstudio.com|VisualStudio Code which is freely available]].
On the Raspberry Pi, raspberrypi.org recommend [[https://www.raspberrypi.org/documentation/usage/python|Thonny]] which should already be installed.
If you are installing VisualStudio Code and are presented with the following screen, we recommend selecting these options:
* _Add "Open with Code" action to Windows Explorer file context menu_
* This will make it easier to open your Python scripts by right-clicking them.
* _Add "Open with Code" action to Windows Explorer directory context menu_
* If you make a directory with all your MartyPy projects, this will let you navigate around it from within VS Code if you right-click the directory and "Open it with Code."
* _Add to PATH (requires shell restart)_
* This should be selected by default. It makes it easier to launch VS Code in some cases.
{{ :martyv2:userguides:python:vscode-install-config.png?600&direct }}
If you are using Visual Studio Code then there are extensions which make programming in Python very convenient - there is a [[https://code.visualstudio.com/docs/languages/python|nice tutorial on code.visualstudio.com]]. We recommend that you install the Microsoft Python extension for VS Code and select the Python interpreter where you installed martypy.
You can also try out the [[https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance|Pylance extension]] which makes Python programming even more convenient.
Now you are ready to [[:martyv2:userguides:python:first_steps_with_python|start programming Marty]])