Python on raspberry

Written by pmd - - no comments

Begin

Installer et mettre à jour Python

Python 2 and Python 3 sont préinstallés sur Raspbian, mais au cas ou pour installer python3 :

sudo apt-get install python3

Ecrire un programme en python

Créer un fichier :

nano hello-world.py

Le remplir avec :

#!/usr/bin/python3

print("Hello, World!")

CTRL+X then Y.

Tous les programmes en Python doivent être avec l'extension ".py".

Executer un programme en Python

python3 hello-world.py

Rendre un programme python executable

Pour rendre un fichier ".py" executable :

chmod +x file-name.py

Puis pour l'executer :

./file-name.py

Source

Parse HTML : Beautiful Soup

Installing :

sudo apt-get install python-bs4 python3-bs4

Premier script

#!/usr/bin/python
from bs4 import BeautifulSoup, Comment

print("Hello, World!");

# save as local file 'webpage.html'
import urllib.request;
#urllib.request.urlretrieve ("https://www.xe.com/fr/currencytables/?from=EUR&date=2019-07-11", "webpage.html");

# read entire file and close immediately after block ends
with open('webpage.html', 'r') as f:
    html_doc = f.read()
#print(html_doc);

# on ouvre la page web téléchargée
soup = BeautifulSoup(html_doc, 'lxml')
# on vire tous les commentaires html
for element in soup(text=lambda text: isinstance(text, Comment)):
    element.extract()
# on vire tous les liens
for a in soup.findAll('a'):
    a.replaceWithChildren()

#print(soup.prettify())
all_rates = soup.tbody.find_all("tr")

# Loop over all elements of a list
for element_tr in all_rates:
    print(element_tr)

Installation de modules avec pip

Sous Windows pour installer des modules :

C:\Users\<username>\AppData\Local\Programs\Python\Python37\Scripts>pip3.7.exe install lxml

Sous linux, souvent il y a des paquets déjà prêts. Sinon :

sudo pip install pandas

Ou :

sudo python3 -m pip install lxml yfinance openpyxl selenium pyvirtualdisplay stockstats mplfinance python-telegram-bot xlrd scipy --upgrade

Pour quelques paquets il faut aussi faire ça sur le raspeberry pi :

sudo apt-get install xvfb chromium-chromedriver

Faire des choses différentes si script sous Windows ou sous Linux

import platform
if platform.system() == 'Windows':
    #Windows
    work_folder = Path('C:\Users\...')
elif platform.system() == 'Linux':
    #Linux
    work_folder = Path('/home/user/...')

Start script from Notepad++

Aller dans Execution > Executer et utiliser cette ligne :

cmd /k "python.exe $(FULL_CURRENT_PATH)"

Very usefull for yfinance

Fixes #192 - If no _institutial_holders is found by JeremyRitchie · Pull Request #196 · ranaroussi/yfinance · GitHub

*.py vers *.exe

Installer auto-py-to-exe :

pip install auto-py-to-exe

Lancer le programme depuis le bon dossier :

auto-py-to-exe

Source

Increase swap size

Source 1, 2

 

Rss feed of the tag