Programming in Python on a Chromebook
This article will teach you how to run Python code on Chromebooks so you can do off-platform Python projects on your Chromebook.
Setting Up Your Python Coding Environment
If you were able to enable Linux on your computer, it should automatically come with Python 3 installed. If you haven’t set up your Linux environment yet, please read the Programming Locally on a Chromebook article before continuing.
Checking Your Python Installation
Let’s see which version of Python 3 you have! We can do this by typing the command
python3 -Vinto your terminal window. Make sure you type
python3 -Vand not
python -V! Typing
pythonwill check the Python 2 version which does not come preinstalled.[email protected]:~$ python3 -V
If this worked, it should display your Python 3 version.[email protected]:~$ python3 -V Python 3.7.3
If it didn’t work (or if you typed in
python3), you will see something like:[email protected]:~$ python3 -V -bash: python3: command not found
To download a new Python version on your machine, continue to the “Installing a New Python Version” section. If you already have the version of Python you want installed on your machine, skip “Installing a New Python Version”.
Installing a New Python Version
If you’re doing a Python 3 off-platform project, you probably don’t need to look at this section! This section is helpful for learners who are trying to code in Python 2 or a specific version of Python 3.
In this example, we have Python 3.7.3, and we’ll be downloading Python 3.8.8. Python 3.8.8 is the Python 3.8 Stable Release according to Python.org.
You can download the Python 3.8.8 source file by using the
wgetcommand in the terminal or from Python.org’s source page. We suggest using
wgetbut will show both approaches.
First, you’ll need to move into the
/usr/srcfolder in your terminal. Then, when using the
wgetcommand, you would change the Python version to match the one you are trying to download:
If you want to download the file from the Python.org website instead, you will navigate to Python.org’s source page. You will want to download from the Stable Release column and click the link to get the Gzipped source tarball type of file from the version you want.
You will need to move the downloaded file from your “Downloads” folder to your “Linux Files”. Then, you will need to move the file to
/usr/src/. You can do this with the
mvcommand.[email protected]:~$ sudo mv https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tgz /usr/src/
You will now need to unzip the file and check if it worked! We will use the
tarcommand to unzip it.[email protected]:/usr/src$ sudo tar xzf Python-3.8.8.tgz
Now, we’ll check if it worked by checking the version! We will need to use
python3since we downloaded Python 3.8.8 in this example.[email protected]:~$ python3.8 -V Python 3.8.8
Picking Your Coding Environment
VS Code is a great environment for lots of coding projects, but, depending on which off-platform project you’re doing or what you’re hoping to create, you may want a different coding environment. VS Code can be used to program in many different languages and is very versatile, and we will explain how to install VS Code in this article.
If your off-platform project requires Jupyter Notebook, you can learn how to install that in our Installing Jupyter Notebook on a Chromebook article.
Installing VS Code
You can follow the below instructions or find installation instructions from the VS Code website here.
- It’s recommended that, in addition to running
sudo apt-get update, we install the dependency
gnome-keyring. You can do this with the command
sudo apt-get install -y gnome-keyring.[email protected]:~$ sudo apt-get install -y gnome-keyring
- Next, to determine which installation file you should download, we need to figure out which CPU your Chromebook has. You can do this with the command
You will see either[email protected]:~$ dpkg --print-architecture
- Go to the VS Code Download page. The Linux installation section will be underneath the image of the penguin.
- If you saw
amd64when you ran the previous command, click on the button that says “64 bit” in the “.deb” row underneath the image of the penguin.
- If you saw
arm64when you ran the previous command, click the button that says “ARM 64” in the “.deb” row underneath the image of the penguin.
- If you saw
- Open your “Files” on your Chromebook and go to your “Downloads” section. Double-click on the installer to begin the installation.
5. A popup will let you know that you’re going to “Install app with Linux (Beta)”. Click “Install” to start the installation. You can press “Okay” to close the installation window. 6. Now, you can search for VS Code whenever you’re ready to start coding!
How Do I Access and Run a File from the Terminal?
You can use your Linux terminal to access files stored in the “Linux files” section of “My files”. To move files you’ve downloaded from Codecademy into this folder there are a few steps:
- Share your “Downloads” folder with your “Linux Files” folder by right-clicking on “Downloads” and then pressing “Share with Linux”.
- Now, you can move your file to “Linux files” and it will be accessible from your terminal!
- You can also view the files in “Linux files” by going to your terminal and using the
lscommand.[email protected]:~$ ls move_me.py my_first_program.py
- You can run your program using the
pythoncommand. You will write
python3 name_of_file.py(for Python 3) and
python name_of_file.pyfor Python2. If you’re using a different version of python, your command will look a little different.[email protected]:~$ python3 my_first_program.py Hello World!
While there are some limitations to programming locally on your Chromebook, you can still program in many languages.
The following articles will help you set up a specific language or tool on your Chromebook: