View Single Post
Old 2019-11-03, 04:29   #3
Dylan14
 
Dylan14's Avatar
 
"Dylan"
Mar 2017

11128 Posts
Default How to get/use Python

Of course, before we can actually go about and learn how to use Python, we need to first acquire the Python interpreter/environment. There are several ways to do this:


1. Download Python from the official website, https://www.python.org/downloads/.
This is the easiest method to get up and running with Python on a standalone interpreter. It will give you the following by default (at least on a Windows install):
* documentation files
* pip (a useful tool to install additional packages)
* tcl/tk and IDLE (a dev environment)
* the python test suite
* the py launcher (makes it easier to run the interpreter)
There are multiple versions available; it is recommended to run the latest version (which at the time of post is 3.8.1)(*), as I will be using Python 3 here for this guide (**).


2. Use a pre-built distribution of Python, which includes a bunch of packages right off the bat. There are several of these available, although not all of them are compatible with every OS choice. For this I recommend the Anaconda distribution (available here, which installs a bunch of packages which will be useful later on (like numpy, matplotlib, etc.) and has pip which allows one to install more packages. Of course, you may pick a different one, see, for example, https://wiki.python.org/moin/PythonDistributions.


3. (Linux only) In several distros of Linux, Python is automatically installed. If for some reason, it is not, you can use the package manager included on your distro. To do this on Ubuntu, do the following:
* first run sudo apt-get update to update the repositories (inserting your root password if needed)

* then run sudo apt-get install python3 python3-pip and answer y when prompted. This will install the python interpreter, the pip package manager and any dependencies.
If you have a different distribution of Linux, then you may have to change apt-get to the appropriate name and parameters for your package manager (pacman, yum, etc.).


4. Online editors: there are several of them out there. The only ones that I have major experience with here are the ones on Google Colaboratory and Kaggle. Both of these use notebook style interfaces which are not too dissimilar to the Jupyter notebook that is used in the Anaconda distribution. For the Colab, you will need a Google account to proceed, and for Kaggle you will need to make an account. Once done, you can create notebooks. More information on these can be found in the following two threads.


Note: if you running the code locally, it will be useful to have a text editor, like emacs, gedit, or notepad++.


Running the interpreter:


Once you have the interpreter installed on your machine, then you can call the interpreter by invoking the following


Code:
python
or
Code:
python3
This assumes that python is in your path, if not, you will need to type the full path in to the interpreter, or add the path to the directory where python lives to your environment variables.
The end result should look like this:
Code:
C:\Users\Dylan>"C:\Users\Dylan\Anaconda\envs\Python 3\python.exe"
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>>
(ignore the warning here, this is a thing from conda).
If you are using Colab, once you login you will have a window which will show the available notebooks. Click on "New Python 3 notebook" and then a new notebook will open.
If you are using Kaggle, once you login, go to "Notebooks", then click on "New Notebook". Leave the settings as is, and then click on "Create". A new kernel will be created with a cell filled with an example piece of code, and an empty cell.
Once ready, we can move on to the next bit.



(*) The download page has pretty much every single minor version and bugfix version of Python, and there are even tarballs for version 1 of Python.
(**) Python 1 is very much out of date (1.6 was released in 2000). Python 2 reached end of life on January 1, 2020. It is of course still available in some Linux repos (for example, Debian 10 (Buster) still ships with it, but Ubuntu 20.04 LTS (Focal) will not have Python 2 in the main repository).

Last fiddled with by Dylan14 on 2020-01-09 at 00:01 Reason: update newest version and end of life notice on Python 2
Dylan14 is offline