Python Tutorial: Setting up your environment
Photo by Patrick on Unsplash
Installing Python
There are two main ways I reccommend to install python
- from the python site https://www.python.org/
- python basic package manager is
pip
you can use it from any command line of your operative system - when using base python I recommend to create independent virtual environments for each project, this will help you keep track of the packages you are using and also avoid issues with multiple package versions; the following commands refer to a linux bash command line, you can find windows equivalent in the virtual environment documentation
- install venv with the following command
pip install venv
- create a project directory and change your current directory into it
mkdir myproject cd myproject
- create a virtual environment in the project directory
python -m venv .
- activate it
source bin/activate
- install venv with the following command
- python basic package manager is
- using the miniconda from Continuum https://docs.conda.io/en/latest/miniconda.html
- this package and environment manager allows you to easily create independent projects, keeping separate package environments as well as using separate python interpreters
- when using Miniconda I also recommend to create separate environments for each project and avoid using the
base
environment which hosts the package manager itself- create a virtual environment with the python version of your choice
conda create -n myenv python=3.10
- create your project directory and move into it
- activate your conda environment
conda activate myenv
- create a virtual environment with the python version of your choice
Installing some useful packages for this tutorial
One of python slogans is to be a “Battery Included” language: this means that a lot of interesting functionalities are installed by default.
Most of this tutorial will make use of the default library; however I found some convenient libraries which are used for exercises and examples.
- pytest helps executing unit tests in a very convenient way
pytest-xdist
executes many tests in parallelpytest-cov
creates a coverage report which is very useful- python-language-server is a language server which will enable your programming editor to help you with development
- black is a formatter, useful for mantaining a readable code style which is also version control friendly
- pylint code linter, will help you find issues in your code witout running it
- jupyterlab a great multilanguage development environment in your browser; used by Google Cloud and many others
conda install pytest pytest-xdist pytest-cov \ python-language-server black \ pylint jupyterlab
Installing some useful software for programming in Python
Here is some other software which will help you developing in python
- Visual Studio Code free editor by Microsoft, one of the best around
- This tutorial will help you getting started with Visual Studio Code and python
- GIT one of the most used version control system, created by Linus Torvalds
Using the interactive command line (REPL)
Python provides a basic interactive command line to start with, I recommend to play with it and possibly with all of its extensions.
The command line reads the python expressions or statements, evaluates or executes them respectively and prints the result; these interactive command lines are called also Read, Eval, Print Loops (REPL) and were available since the LISP language.
to start the python REPL open your command line, move into your project, activate it and type
this will print some version information and the basic python prompt
python
>>>
You can type here python expressions, when you hit return python will evaluate them and print their value.
It is possible to type whole programs into a REPL (which is not the most convenient way); I love it because it gives me a fast feedback.
In time many extensions to the python CLI where developed which have some advantages, here is a list of some:
- ipython
- ptipython (which adds some CLI and editing functionalities)
- jupyter notebook (web powered extension)
- jupyter lab (even more powerful)