Python - Basics

Aus Wikizone
Wechseln zu: Navigation, Suche

Links

Python

Shell

conda list - Anaconda packages auflisten (nur wenn anaconda installiert ist)
python - Python Shell aufrufen

Basic examples

msg = "Hallo Python"
print(msg)
exit()
anaconda-navigatior - Navigator öffnen (grafischer Package Manager)

Visual Studio Code als IDE

https://code.visualstudio.com/docs/python/python-tutorial

Packages / Code Libraries importieren

Typischerweise holt man Packages von PyPi und importiert sie dann im Code.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point
plt.show()                   # Display the plot

Hier benötigt man das matplot package (die auch numpy enthält).

Wenn man Anaconda nutzt (Anaconda Interpreter in VSC auswählen) stehen die Packages automatisch zur Verfügung, die man im Anaconda-Navigator ausgewählt hat.

Ansonsten gilt als best practice ein projektspezifisches virtuelles environment zu machen und dort die benötigten Packages zu laden.

Auf dem Mac virtuelles Environment generieren (Bei Windows ohne source Command).

python3 -m venv .venv
source .venv/bin/activate

Die neue Umgebung wird in VSC über Python: Select Interpreter aus der Command Palette ausgewählt.

Jetzt kann man Packages Installieren:

# Don't use with Anaconda distributions because they include matplotlib already.

# macOS
python3 -m pip install matplotlib

# Windows (may require elevation)
python -m pip install matplotlib

# Linux (Debian)
apt-get install python3-tk
python3 -m pip install matplotlib


HTML Parsing und Download URL

https://www.thepythoncode.com/article/extract-web-page-script-and-css-files-in-python (via Get und Browser Simulation)
https://www.thepythoncode.com/article/download-files-python (via Stream für lange Files)