Different ways to use Python#
Where/how Python code can be executed
Tools to install Python and install Python libraries and programs (basics)
Python: a language and some interpreters#
Python is a programming language.
The most common way to execute Python code is to interpret it. Through misuse of language, one says that Python is an interpreted language (like Bash, Matlab, and in contrast with Fortran, C or C++). To interpret code, we need an interpreter, i.e. a program parsing Python code and running computer instructions.
Open a terminal. There are usually different python commands: python and python3.
How to get Python#
You first need to know that there are several ways to install Python packages, each tool works quite differently and is used for different tasks.
For different projects, you may need to use different versions of the same package. Or you may be facing a situation where a package you want to use needs a specific version of another package (an older version than the one you already have installed). To get around pottentially messy install, you should use virtual environments.
Definition: environment
An environment is a set of programs and libraries with particular versions. An
environment is defined by the software installed on your computer and by
environment variables, in particular the variable $PATH, which contains all
the paths where your shell looks for programs (you can read the output of echo $PATH).
One can create/use virtual environments. conda can creates conda virtual
environments (not specific to Python) and there are also Python virtual
environments which can be created with a command like python3 -m venv venv-for-something.
pipis the official tool to install Python packages provided by the Python Packaging Authority (PyPA). It is not designed to install non Python dependencies.pipinstalls by default from the Python Package Index (PyPI).condaandmambaare 2 open-source commandline tools to manage software installations and create “environments”.condahas been provided by a company called Anaconda Inc in their Python/R distribution Anaconda.
uvis a new Python package and project manager written in Rust. It is usually much faster thanconda, handles project management, and it installs and manages python versions and tools (environments).pixiis a fast, modern, and reproducible package management tool that supports multiple languages.
Install with conda/mamba#
To use conda/mamba, one has to install something like
miniforge/mambaforge (as we did in
About setting up a computer for scientific computing).
Install just Python#
On Linux, you should have the system Python (a command
python3corresponding to a file/usr/bin/python3).
Virtual environments
Some Linux distributions split Python in small bits, and it is possible to
install Python without some packages of the standard library. This is in
particular the case of the Python package venv, used to create virtual
environments, which is provided in Ubuntu with the Ubuntu package
python3-venv. Virtual environments are so important for the Python ecosystem
that one should get this package.
On Windows, Python can be installed in few clicks with the Microsoft Store.
On MacOS, one can use Homebrew or install with the official Python installer (https://www.python.org/downloads/)
Download the repository of this training#
Exercise 1
Download the repository of this training (you need mercurial). On Unix machines, use the commands:
hg clone https://foss.heptapod.net/py-edu-fr/py-edu-fr
cd py-edu-fr/books/init/book
ls
Execute a script with the command python3#
Exercise 2
Run a script with python3 (helloworld.py is just a text file):
python3 common/examples/helloworld.py
Note
cat is a Unix command that prints the content of a text file.
cat common/examples/helloworld.py
Work interactively with ipython#
The command ipython3 launches the program IPython, which is
used for interactive python.
Exercise 3
In IPython, you can execute a first interactive instruction:
2 + 2
4
3 / 4
0.75
Run the script from ipython.
run ../common/examples/helloworld.py
Hello world
My name is Pierre
In the file helloworld.py, a variable called name is defined.
print(name)
Pierre
Help on an object can be displayed with the question mark (try it):
name?
Try to type
name.and to press on tab… The tab key is used to tell you how what you typed can be completed.Try to use the top and bottom arrows…
Python in an IDE (Spyder)#
Launch the application Spyder, a Python IDE (Integrated Development Environment).
a good code editor with:
syntax coloring,
code analysis powered by pyflakes and pylint,
introspection capabilities such as code completion.
Ipython console
variable inspector
…
Important
It is very important to use a good editor to code in Python (for example Spyder, emacs or vi (with a good setup!), or Visual Studio Code).
Python in the browser (JupyterLab)#
The presentations of this python training are made with Jupyter (demonstration).
This is a very powerful tool to present results (see these examples).
jupyter-lab