Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตัวแปรสภาพแวดล้อม Python (Environment Variables)


ตัวแปรสภาพแวดล้อมสำคัญที่ Python รับรู้:

ลำดับตัวแปรและคำอธิบาย
1 PYTHONPATH
คล้าย PATH บอก interpreter หาโมดูลที่ import ได้ ควรมี Python source library และ directories ที่มี source code
2 PYTHONSTARTUP
พาธของไฟล์ initialization ที่มี source code Python รันทุกครั้งที่เริ่ม interpreter ชื่อ .pythonrc.py ใน Unix
3 PYTHONCASEOK
ใช้ใน Windows บังคับให้ Python หาค่าที่ match แบบ case-insensitive ใน import statement
4 PYTHONHOME
เส้นทางค้นหาโมดูลทางเลือก ปกติ embed ใน PYTHONSTARTUP หรือ PYTHONPATH

การตั้งค่าในระบบปฏิบัติการต่างๆ

Linux/macOS (bash)

export PYTHONPATH=/path/to/modules:$PYTHONPATH
export PYTHONSTARTUP=~/.pythonrc.py

Windows (Command Prompt)

set PYTHONPATH=C:\path\to\modules;%PYTHONPATH%

Windows (PowerShell)

$env:PYTHONPATH = "C:\path\to\modules;$env:PYTHONPATH"