Python - Basics: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
Zeile 32: Zeile 32:
 
Ansonsten gilt als best practice ein projektspezifisches virtuelles environment zu machen und dort die benötigten Packages zu laden.
 
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).
+
Auf dem Mac virtuelles Environment generieren (Bei Windows ohne source Command.
  
 +
Terminal (direkt in VSC):
 +
<syntaxhighlight lang="bash">
 
  python3 -m venv .venv
 
  python3 -m venv .venv
 
  source .venv/bin/activate
 
  source .venv/bin/activate
 +
</syntaxhighlight>
  
 
Die neue Umgebung wird in VSC über Python: Select Interpreter aus der Command Palette ausgewählt.
 
Die neue Umgebung wird in VSC über Python: Select Interpreter aus der Command Palette ausgewählt.
Zeile 41: Zeile 44:
 
Jetzt kann man Packages Installieren:
 
Jetzt kann man Packages Installieren:
  
 +
Terminal:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
# Don't use with Anaconda distributions because they include matplotlib already.
 
# Don't use with Anaconda distributions because they include matplotlib already.
Zeile 54: Zeile 58:
 
python3 -m pip install matplotlib
 
python3 -m pip install matplotlib
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
== HTML Parsing und Download URL ==
 
== 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/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)
 
  https://www.thepythoncode.com/article/download-files-python (via Stream für lange Files)

Aktuelle Version vom 17. August 2022, 09:35 Uhr

Links[Bearbeiten]

Python

Shell[Bearbeiten]

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

Basic examples[Bearbeiten]

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

Visual Studio Code als IDE[Bearbeiten]

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

Packages / Code Libraries importieren[Bearbeiten]

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.

Terminal (direkt in VSC):

 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:

Terminal:

# 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[Bearbeiten]

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)