Veröffentlicht · wird verbessert
Python-Anleitung · 1/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
This chapter gets a working Python 3 setup onto your machine. You will try code in the interactive interpreter, run a script from a file, give each project its own virtual environment, install packages with pip, and set up an editor that understands your code. By the end you will have a clean, repeatable way to start any new Python project.
Python 2 reached end of life years ago, so everything here assumes Python 3. How you install it depends on your platform:
brew install python).Then check that it works. On Windows, the py launcher is handy because it can pick between installed versions.
python3 --version
# on Windows
py --versionRun python3 (or py on Windows) with no arguments and you land in the REPL, a read-eval-print loop. Type an expression after the >>> prompt and the result appears immediately. It is the fastest way to check how a function behaves or to poke at a new library. Use help(len) for built-in documentation, and leave with exit() or Ctrl+D (Ctrl+Z then Enter on Windows).
2 ** 10 # 1024
"python".upper() # 'PYTHON'
len([1, 2, 3]) # 3
import math
math.sqrt(16) # 4.0Real programs live in .py files. Save the following as hello.py:
import sys
def main() -> None:
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!")
if __name__ == "__main__":
main()The if __name__ == "__main__": guard means main() runs only when the file is executed directly, not when another module imports it. That small habit keeps your files reusable.
python3 hello.py
python3 hello.py Alice
# -m runs a module by name, here a tiny web server from the standard library
python3 -m http.server 8000Different projects need different packages, often at conflicting versions. Installing everything into the system Python quickly turns into a mess. The standard venv module solves this by creating an isolated environment inside your project folder.
# create an environment in a folder called .venv
python3 -m venv .venv
# activate it on macOS or Linux
source .venv/bin/activate
# activate it in Windows PowerShell
.venv\Scripts\Activate.ps1
# leave it
deactivateOnce activated, python and pip point into the environment and your prompt shows (.venv). The folder can always be recreated, so keep it out of version control by listing it in .gitignore. If you are ever unsure which interpreter is running, python -c "import sys; print(sys.executable)" prints its path; when that path is inside .venv, the environment is active.
pip downloads packages from the Python Package Index (PyPI). Always install into an activated virtual environment, and prefer python -m pip so there is no doubt about which interpreter receives the package.
python -m pip install requests
python -m pip install --upgrade requests
python -m pip list
python -m pip uninstall requests| Command | What it does |
|---|---|
pip install name | Install a package |
pip install -r requirements.txt | Install everything listed in a file |
pip freeze | Print installed packages with exact versions |
pip show name | Show details about one package |
Any text editor will do, but a few features make a big difference:
.venv, so completion and error highlighting know about the packages you installed.python3 --version..py files for real programs, guarded by if __name__ == "__main__":.python3 -m venv .venv and activate it before working.python -m pip install, and make sure your editor uses the environment's interpreter.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.