sys.
What It Does and Why It Matters
sys.executable contains the path of the active interpreter and sys.version_info exposes its numeric version components. They help launch child Python processes and check feature availability accurately.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
import sys
print(sys.executable)
print(sys.version_info.major == 3)
print(sys.version_info[:3])
How the Code Works
- The imports make these standard-library tools available: sys.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
/opt/pyvenv/bin/python
True
(3, 13, 5)
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Display interpreter location and check that the major version is Python 3.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Invoking an unrelated python executable from PATH instead of the interpreter running the current application.
- Checking only the easiest input and missing a relevant error or boundary case.
Key Takeaways
- sys.
- sys.executable contains the path of the active interpreter and sys.version_info exposes its numeric version components.
- Display interpreter location and check that the major version is Python 3.