Python sys.prefix Variable

Python sys.prefix Variable

  1. Syntax
  2. Parameters
  3. Returns
  4. Example Codes Demonstrating the Use of sys.prefix

Python sys.prefix is an efficient way of finding out the site-specific directory prefix where a system’s independent Python files and installation are made. Note that on the Unix platform, the default directory is /usr/local.

Syntax

sys.prefix

Parameters

no parameters It is a non-callable object.

Returns

The return type of sys.prefix is a string containing the site-specific directory prefix.

Example Codes Demonstrating the Use of sys.prefix

Use the sys.prefix on Windows OS

import sys
print (sys.prefix)

Output:

C:\Program Files\Python310

The above code may produce different directories based on the user’s system and the Python installations.

Use the sys.prefix on UNIX OS

import sys
print (sys.prefix)

Output:

/usr/local

Note that if a virtual environment is in use, the value will be changed in site.py to point to the specific virtual environment.

Use the sys.prefix With the sys.exec_prefix

import sys
print (sys.prefix)
print (sys.exec_prefix)

Output:

C:\Program Files\Python310
C:\Program Files\Python310

The sys.prefix and sys.exec_prefix are similar and produce the same desired results.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Sys

  • Python sys.stdin Attribute
  • Python sys.argv List
  • Python sys.maxsize() Method
  • Python sys.stdout Method
  • Python sys.exit() Method
  • Python sys.path Attribute