16. Ek Bölüm

16.1. Etkileşimli Mod

There are two variants of the interactive REPL. The classic basic interpreter is supported on all platforms with minimal line control capabilities.

On Windows, or Unix-like systems with curses support, a new interactive shell is used by default. This one supports color, multiline editing, history browsing, and paste mode. To disable color, see Controlling color for details. Function keys provide some additional functionality. F1 enters the interactive help browser pydoc. F2 allows for browsing command-line history without output nor the >>> and prompts. F3 enters “paste mode”, which makes pasting larger blocks of code easier. Press F3 to return to the regular prompt.

When using the new interactive shell, exit the shell by typing exit or quit. Adding call parentheses after those commands is not required.

If the new interactive shell is not desired, it can be disabled via the PYTHON_BASIC_REPL environment variable.

16.1.1. Hata İşleme

When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack trace. (Exceptions handled by an except clause in a try statement are not errors in this context.) Some errors are unconditionally fatal and cause an exit with a nonzero exit status; this applies to internal inconsistencies and some cases of running out of memory. All error messages are written to the standard error stream; normal output from executed commands is written to standard output.

Yarıda kesme karakterinin (genellikle Control-C veya Delete) birincil veya ikincil istemine yazılması girişi iptal eder ve birincil istemi döndürür. [1] Bir komut yürütülürken bir yarıda kesme karakteri yazmak KeyboardInterrupt özel durumuna neden olur ve bu özel durum try deyimi tarafından işlenebilir.

16.1.2. Yürütülebilir Python Komut Dosyaları

BSD türü Unix sistemlerinde Python komut dosyaları, :: satırı koyarak kabuk komut dosyaları gibi doğrudan yürütülebilir hale getirilebilir:

#!/usr/bin/env python3

(yorumlayıcının kullanıcının PATH) komut dosyasının başında olduğunu ve dosyaya yürütülebilir bir mod verdiğini varsayarsak. #! dosyanın ilk iki karakteri olmalıdır. Bazı platformlarda, bu ilk satırın Windows ('\r\n') satır sonuyla değil, Unix stili bir satır sonuyla (\n') bitmesi gerekir. Python’da yorum başlatmak için karma veya pound karakteri olan '#' kullanıldığını unutmayın.

Komut dosyasına chmod komutu kullanılarak yürütülebilir mod veya izin verilebilir.

$ chmod +x myscript.py

Windows sistemlerinde, “yürütülebilir mod” kavramı yoktur. Python yükleyicisi otomatik olarak .py dosyalarını python.exe ile ilişkilendirir, böylece python dosyasına çift tıklama komut dosyası olarak çalıştırılır. Uzantı .pyw ‘de olabilir, bu durumda normalde görünen konsol penceresi bastırılır.

16.1.3. Etkileşimli Başlangıç Dosyası

Python’u etkileşimli olarak kullandığınızda, yorumlayıcı her başlatıldığında bazı standart komutların yürütülmesi genellikle kullanışlıdır. Bunu, başlatma komutlarınızı içeren bir dosyanın adına PYTHONSTARTUP adlı bir ortam değişkeni ayarlayarak yapabilirsiniz. Bu, Unix kabuklarının .profile özelliğine benzer.

Bu dosya Python komutları bir komut dosyasından okuduğunda değil, yalnızca etkileşimli oturumlarda okunur. Python komutları bir komut dosyasından okuduğunda, /dev/tty komutların açık kaynağı olarak verildiğinde değil (aksi takdirde etkileşimli bir oturum gibi davranır). Etkileşimli komutların yürütüldüğü aynı ad alanında yürütülür, böylece tanımladığı veya aldığı nesneler etkileşimli oturumda nitelik olmadan kullanılabilir. Bu dosyadaki sys.ps1 ve sys.ps2 istemlerini de değiştirebilirsiniz.

Geçerli dizinden ek bir başlangıç dosyası okumak istiyorsanız, bunu genel başlangıç dosyasında if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read() gibi bir kod kullanarak programlayabilirsiniz. Başlangıç dosyasını bir komut dosyasında kullanmak istiyorsanız, bunu komut dosyasında açıkça yapmalısınız:

import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
    with open(filename) as fobj:
        startup_file = fobj.read()
    exec(startup_file)

16.1.4. Özelliştirme Modülleri

Python provides two hooks to let you customize it: sitecustomize and usercustomize. To see how it works, you need first to find the location of your user site-packages directory. Start Python and run this code:

>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.x/site-packages'

Artık bu dizinde usercustomize.py adlı bir dosya oluşturabilir ve içine istediğiniz her şeyi koyabilirsiniz. Otomatik içe aktarmayı devre dışı bırakmak için -s seçeneğiyle başlatılmadıkça Python’un her çağrısını etkiler.

sitecustomize works in the same way, but is typically created by an administrator of the computer in the global site-packages directory, and is imported before usercustomize. See the documentation of the site module for more details.

Dipnotlar