Installing Python
ASnake Documentation
Summary:
Python (usually) is an interpreter, which means it is a program which executes code.
There are different interpreters for Python, the most common being CPython, which is written in C. Others include PyPy (written in Python) and Pyston, which are often used for their performance improvements.
CPython
+ pro: greatest amount of working libraries
+ pro: most up-to-date
- con: slow
PyPy
+ pro: generally the fastest interpreter
- con: mostly only supports 'pure Python' libraries
Pyston
+ pro: fast, works with everything CPython does
- con: no Windows support
- con: stuck on version 3.8
- con: abandoned
MicroPython
+ pro: works on embedded due to low memory
- con: slow
- con: only useful with hardware
- con: generally incompatible with most Python libraries, including the standard library
If you are new, I reccomend using CPython. If you are experienced, use PyPy when you are not using libraries which require C ABI. Pyston can be used in niche cases, and should probably not be your choice going forward. MicroPython maybe if you are doing an embedded project.
ASnake (and Cython, except for MicroPython) supports all of the listed interpreters as a compilation target.
For those confused between Cython and CPython, Cython compiles your Python code into C (which makes it faster), and ASnake can help you automate that process. CPython is just a Python interpreter written in C.
If you are interesting in installing Cython for ASnake, follow this blog post guide.
CPython:
You will find no shortage of resources on how to install CPython for whatever operating system or distro you are using. If this guide does not suffice, use a search engine of your liking to assist.
Windows:
The install steps should be similar regardless of the Windows version you are using. I do not recommend Python from the Windows app store, but it is an option.
First, go to Python's official download page and download the latest (or preferred or ASnake supported) version for your computer architecture.
Once installed, execute the setup.exe
.
It is highly recommended you check Add python.exe to PATH
, as this will ensure you can access it from the command line. If you do not check it, you will have to manually add Python to PATH. That can be quite annoying.
You can Use admin privileges when installing py.exe
if you want, but you do not have to.
Unless you are feeling spunky, just click Install Now
, though it is likely you would do fine when customizing too. But for the sake of the guide we will assume you went the path of least resistance.
It will start installing, and once complete it will likely proclaim it was successful. If it was unsuccessful: freak out and start to panic, questioning if you will ever be a real coder.
Otherwise, it is time to bust out the scary hacker terminal. Type in terminal
into your Windows search bar thing, and click the Terminal app.
If you do not have the terminal app, command prompt will suffice. In this case type cmd
.
If you are using terminal, it is suggested you use Command Prompt
mode.
Type py --version
. If it outputs a version, congrats, you have Python installed. If not, you may need to start diagnosing where you went wrong. Make sure Python is in PATH.
You can use py
as the start of most of your Python commands. You can also use python
, but py
will default to the latest version you have installed.
If you want ASnake, enter py -m pip install ASnake
To check if ASnake works, do py -m ASnake -e "12"
If it outputs a 12, then you are ready to go!
Linux:
This will depend heavily on what distro you are using, but you are likely to already have Python installed!
You can check with: python3 --version
Or: python --version
If not, you can use apt (for Ubuntu, Debian, LinuxMint) to get it: sudo apt-get install python3
Or via pacman (for Arch, EndeavourOS, Manjaro): sudo pacman -S python
For Debian/Ubuntu based distros you should install pip: sudo apt-get install pip
For other distros, this is worth a shot: python -m ensurepip
In modern versions of Python for Linux, some distros have decided that using pip
is dumb, because of package conflicts with distro package-handlers.
There are solutions for this, such as disabling the warning (be careful, also replace with your version):
console
cd /usr/lib/python3.11
sudo rm EXTERNALLY-MANAGED
You can also try out pipx
. The pipx site has instructions on how to install it for your relevant distro.
PyPy:
PyPy is a Python interpreter written in Python. It's faster because it also has a Just-In-Time compiler, meaning as your code runs it will analyze it and convert it to machine code. That makes your code go faster.
Sounds great. So why not use it always? Mostly due to the lack of support for the CPython C API, which means that some libraries won't work, or will run slow. However, PyPy is still great and you should try it out if you ever have 'pure Python' (no C extensions) and speed is a concern.
You may wonder "Oh if PyPy speeds up my code why do I need ASnake?", but you fail to consider that ASnake also speeds up codes that run for PyPy! Meaning a script untouched by ASnake may run slower than a script compiled by ASnake for PyPy.
Here is how to install PyPy for your platform:
MacOS or Windows: download and install from official site.
Debain-based Linux:
sudo apt-get install pypy3
Arch-based Linux:
sudo pacman -Sy pypy3
For any of these, pypy3 -m ensurepip
may be necessary if pip is missing.
Pyston:
Pyston is an implementation of Python which, unlike PyPy, is fully compatible with CPython C API. This means its a fully drop-in replacement for Python which provides performance improvements. Just like PyPy, ASnake can target Pyston specifically to make Pyston scripts faster.
The catch though is twofold. Pyston is generally a tiny bit slower than PyPy (depends highly on the workload), and Pyston seems to be abandoned.
Pyston is stuck at Python 3.8, so if you need versions above or below that, you are out of luck.
Also, it is only available for Mac and Linux. Sorry Windows.
Download here: https://github.com/pyston/pyston/releases
Also Pyston is installable as a package on Arch-based Linux via the AUR.
MicroPython:
According to their website, "MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments."
There are MicroPython implementations for many popular microcontrollers, such as the RP2040 and ESP32 series.
When using MicroPython on such a device, a device firmware file is flashed to the microcontroller that has the MicroPython interpreter on it. When the device boots, it can either run MicroPython files or present a REPL for writing code. This section will cover the installation of MicroPython on the Raspberry Pi Pico but the process is similar for many other development boards.
There are a few ways to install MicroPython on your device. The most basic method is to flash a precompiled MicroPython firmware file to the device by mounting it as removable storage.
MicroPython firmware files can be found here.
Once you have the file (it should end in .uf2), press and hold the bootsel button on the Pico while plugging it into your PC.
The board should get mounted as a drive named RPI-RP2. Simply drag the .uf2 file over to the drive. Once the file is copied, the board will reset and appear as a serial device.
MicroPython can also be installed easily on Linux and Windows through the Thonny IDE. Once you have Thonny installed, choose Configure Interpreter from the Run menu. Select the board or microcontroller you are using from the first drop down menu. Click the link in the bottom right for Install or update MicroPython, then follow the instructions that pop up.
An example setup: