Posts Tagged - python

Python 101 (env, tools and Poetry)

TL;DR

Stack a usar:

  • python -> lenguaje e intérprete
  • py -> python launcher para windows
  • pipx -> instalar tools python globales
  • .env -> variables de entorno local; UAT y PROD usar config de donde se despliegue
  • pyenv -> gestor de versiones de Python
  • poetry -> gestor de proyecto. Abstrae pip y sustituye venv y requeriments.txt. Detalles de como usar poetry aquí <- TODO: linkear a post

No usar pero si conocer:

  • pip -> gestor de paquetes - incluye uso de requirements.txt (sustituir por poetry)
  • pip3 -> No usarlo. Leftover histórico de cuando python2 y python3 convivian hasta el 2020
  • venv -> entornos virtuales para aislamiento (sustituir por poetry)
  • conda -> package y environment manager para data science & ML

    Stack a usar

    Python (lenguaje/intérprete)

    Python como tal es el lenguaje y el intérprete. Es un lenguaje interpretado (no compilado) sobre una MV propia. Python por sí mismo no gestiona dependencias ni aislamiento.

Python. Solo. Ejecuta. Código.

(py) python en Windows

Python no es un único ejecutable, puedes tener varios intérpretes instalados a la vez. El caos normalmente viene a que cada uno es una versión distinta, con librerías distintas y sus propios paths.

python.exe
python3.exe
py.exe

Si usas lo que hace el sistema es buscar python en $PATH y usar el primero que encuentre

python main.py

En windows usar siempre py. Está diseñado para gestionar múltiples versiones de Python en windows.

py # lanza la version por defecto. Normalmente el último Python3 instalado
py -3.11
py -0p # muy útil para debuggear todas las instalaciones detectadas

No usar python y py indistintamente

No se puede usar indistintamente python y py ya que son comandos diferentes.

# DON'T DO THIS
python -m venv .venv
.venv/Scripts/activate
pip install fastapi
py main.py # puede ignorar el .venv activado y lanzar un python global distinto

Read More

(pipx) Install Python tool globally

# only the first time we install something 
pipx ensurepath
# close and open terminal again

# build, install and run to make sure it works
poetry build
poetry install
poetry run

# install localy with pipx
pipx install .

# now we're able to run it from anywhere
file-enlarger

in this case we invoke it as file-enlarger as the .toml declares it as such

[tool.poetry.scripts]
file-enlarger = "FileEnlarger:main"

Read More

Python's Poetry

Prerequisites

(!) TODO: review (!)

First of all install pip and use pip to install pipx. From then on, use only pipx

install pip tools

py -m pip install --user pip-tools

# upgrade pip
py -m pip install --upgrade pip

install pipx

py -m pip install --user pipx

# adds executables to global path so you can call them without py -m ...
py -m pipx ensurepath
# close and reopen console

install poetry through pipx

py -m pipx install --user poetry

Read More

Scrapy (Python web crawler)

Scrapy is a web-scrapper & crawler.

Concepts

spider: class that you define and scrapy uses to scrape information from a website (our a group of websites). They must define the initial requests to make, optionally how to follow links in the pages and how to parse the content to extract data

item pipeline: after an item has been crawled by a spider, it’s sent to the item pipeline which processes it through several components that are executed sequentially. You can use them, for example, to save items to a database

How to use

# create a new project
scrapy startproject your_project_name  

# after writing a spider, it starts the crawl
scrapy crawl quotes

Read More