**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.
There are two versions of Python; Python 2 and Python 3. We need the newer Python 3. Throughout this guide, you will see references to "''python'' and ''pip'' commands". On some systems, these might refer to Python 2 and you might need to substitute ''python'' and ''pip'' for ''python3'' and ''pip3'' respectively.
If you already know how to open a terminal window (command prompt), you can check that these are installed by running ''python %%--%%version'' and ''pip %%--%%version''. The former should output something like ''Python 3.8.6'' and the output of the latter should start with something like ''pip 20.1.1'' and end with ''(python 3.8)''. Make sure the Python versions match (in this case, both are Python 3.8). Also make sure that the Python version is at least 3.6.0. On Linux, this check looks like this:
{{:martyv2:programming:python:screenshot_from_2020-12-18_15-03-13.png}}
In windows you can open a terminal window (hit start and type terminal) and try the same commands
Remember to also try ''python3'' and ''pip3'' if ''python'' or ''pip'' don'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]]
If you have used Python before, feel free to use your favourite installation method.
==== Installing Python on Windows ====
Go to [[https://www.python.org/downloads/windows]] and download the latest release
Unless you are planning on running multiple versions of python, it is important to check the box to "Add Python to PATH" during the install
{{ :martyv2:userguides:python:python_install_path.png?nolink |}}
==== 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.
===== Step 2: 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
```
This will install the martypy Python library onto your computer and it should show something like this to indicate success:
```shell
Collecting martypy
Using cached martypy-2.0-py2.py3-none-any.whl (49 kB)
Collecting pyserial
Using cached pyserial-3.4-py2.py3-none-any.whl (193 kB)
Installing collected packages: pyserial, martypy
Successfully installed martypy-2.0 pyserial-3.4
```
==== 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
```
==== Step 2.5: Installing ffmpeg ====
ffmpeg is a tool required for the Marty speak feature. Some systems come with ffmpeg pre-installed, but if this is not the case for your machine, you can follow the next steps to install it.
=== Windows ===
- **Step 1:** Download the ffmpeg build from [[https://ffmpeg.org/download.html|https://ffmpeg.org/download.html]].
- **Step 2:** Unzip the downloaded file to a directory, e.g., ''C:\ffmpeg''.
- **Step 3:** Add ffmpeg to your System Path:
- Right-click on 'My Computer' and select 'Properties'.
- Click on 'Advanced system settings' and go to the 'System Properties' window.
- Click on the 'Environment Variables' button.
- In the 'System Variables' section, find and select the 'Path' variable, then click 'Edit'.
- Add the path to your ffmpeg bin directory, e.g., ''C:\ffmpeg\bin''.
- Click 'OK' to close all windows.
- **Step 4:** Verify the installation by opening Command Prompt and typing ''ffmpeg -version''.
=== macOS ===
- **Step 1:** Install Homebrew if it's not already installed (visit [[https://brew.sh/|https://brew.sh/]]) for instructions).
- **Step 2:** Open Terminal.
- **Step 3:** Install ffmpeg by typing ''brew install ffmpeg''.
- **Step 4:** Verify the installation by typing ''ffmpeg -version'' in the Terminal.
=== Ubuntu (Linux) ===
- **Step 1:** Open Terminal.
- **Step 2:** Update the package list by typing ''sudo apt-get update''.
- **Step 3:** Install ffmpeg by typing ''sudo apt-get install ffmpeg''.
- **Step 4:** Verify the installation by typing ''ffmpeg -version'' in Terminal.
=== Additional Resources ===
For more detailed instructions or troubleshooting, visit the official ffmpeg documentation at [[https://ffmpeg.org/documentation.html|https://ffmpeg.org/documentation.html]].
===== Step 3: 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 something like `'2.2.0'`. For example:
{{: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]]. 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. At the time of writing, Pylance is still just a _Preview_ version, meaning there might still be problems with it and you may want to hold off installing it untill it's fully ready.
Now you are ready to [[:martyv2:userguides:python:first_steps_with_python|start programming Marty]])