Python Sys
## Python3 sys Module
The sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
### sys.argv
sys.argv is a list in Python, which contains the command-line arguments passed to the script.
With sys.argv, you can get the name of the script.
Example:
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument list:', sys.argv)
Output:
Number of arguments: 1 arguments.
Argument list: ['test.py']
If we execute the script using the following command:
python test.py arg1 arg2 arg3
The output will be:
Number of arguments: 4 arguments.
Argument list: ['test.py', 'arg1', 'arg2', 'arg3']
### sys.exit(n)
Exit the program, you can pass an exit status, 0 means normal exit.
### sys.path
When the Python interpreter imports a module, it will search for the module in the following locations:
1. The directory where the current script is located
2. The PYTHONPATH environment variable contains directories with standard installation scripts
3. Python's default installation path (depending on the installation)
You can modify sys.path to add the path to import modules.
Example:
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
print(sys.path)
Output:
['/root/PycharmProjects/test', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/lib/python3.4/site-packages']
### sys.version
Get the version information of the Python interpreter.
Example:
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
print(sys.version)
Output:
3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2]
### sys.platform
Returns the platform identifier of the system.
Example:
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
print(sys.platform)
Output:
linux
### sys.stdin
Standard input, usually keyboard input.
### sys.stdout
Standard output, usually output to screen.
### sys.stderr
Standard error.
### sys.modules
sys.modules is a dictionary that stores all loaded modules.
### sys.getrecursionlimit()
Get the maximum recursion depth.
### sys.setrecursionlimit()
Set the maximum recursion depth.
### sys.getdefaultencoding()
Get the default encoding.
### sys.setdefaultencoding()
Set the default encoding.
### sys.getfilesystemencoding()
Get the file system encoding.
### sys.stdout.write()
Write to standard output.
### sys.stdout.flush()
Flush standard output.
### sys.exc_info()
Get exception information.
### sys.exitfunc()
This function is called when the program exits.
### sys.settrace()
Set trace function.
### sys.last_traceback
The last traceback object.
### sys.executable
The path to the Python interpreter.
### sys.hexversion
The version of Python in hexadecimal.
### sys.maxsize
The maximum size of a list or other container.
### sys.byteorder
The byte order of the system.
### sys.copyright
Copyright information of Python.
### sys.api_version
The API version of Python.
### sys.version_info
Get detailed version information.
### sys.dont_write_bytecode
Whether to write .pyc files.
### sys.flags
Command-line flags.
### sys.warnoptions
Warning options.
### sys.builtin_module_names
List of built-in module names.
### sys.windows_version
Windows version information.
### sys.hash_info
Hash algorithm information.
### sys.implementation
Implementation information.
### sys.thread_info
Thread information.
### sys.float_info
Floating-point information.
### sys.abiflags
ABI flags.
### sys.base_exec_prefix
The base execution prefix.
### sys.base_prefix
The base prefix.
### sys.byteorder
The byte order.
### sys.callbacks
List of callbacks.
### sys.check_interval
Check interval.
### sys.console_title
Console title.
### sys.dllhandle
DLL handle.
### sys.dso_handle
DSO handle.
### sys.exc_clear
Clear exception.
### sys.exc_type
Exception type.
### sys.exc_value
Exception value.
### sys.exec_prefix
Execution prefix.
### sys.executable
Executable path.
### sys.flags
Flags.
### sys.float_repr_style
Float representation style.
### sys.getallocatedblocks()
Get allocated blocks.
### sys.getcheckinterval()
Get check interval.
### sys.getfilesystemencoding()
Get file system encoding.
### sys.getprofile()
Get profile function.
### sys.getrecursionlimit()
Get recursion limit.
### sys.getsizeof()
Get size of an object.
### sys.getswitchinterval()
Get switch interval.
### sys.gettrace()
Get trace function.
### sys.hexversion
Hex version.
### sys.int_info
Integer information.
### sys.long_info
Long integer information.
### sys.maxsize
Maximum size.
### sys.maxunicode
Maximum Unicode value.
### sys.path_hooks
Path hooks.
### sys.path_importer_cache
Path importer cache.
### sys.platform
Platform.
### sys.prefix
Prefix.
### sys.ps1
Primary prompt string.
### sys.ps2
Secondary prompt string.
### sys.setcheckinterval()
Set check interval.
### sys.setprofile()
Set profile function.
### sys.setrecursionlimit()
Set recursion limit.
### sys.setswitchinterval()
Set switch interval.
### sys.settrace()
Set trace function.
### sys.stderr
Standard error.
### sys.stdin
Standard input.
### sys.stdout
Standard output.
### sys.thread_info
Thread information.
### sys.version
Version.
### sys.version_info
Version information.
### sys.warnoptions
Warning options.
### sys.winver
Windows version.
## Related Links
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- [Python Number(Number)](#)
- (#)
- [Python Lists(List)](#)
- (#)
- [Python Dictionaries(Dictionary)](#)
- (#)
- (#)
- (#)
- [Python File I/O/O](#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- (#)
- [Python GUI programming(Tkinter)](#)
- [Python 2.x And 3.x Version Differences](#)
- (#)
- (#)
- (#)
## Comments
Please share your experience, questions, or suggestions to help others learn better.
Note: This is a translation placeholder. The original content likely had a comment section for readers to share their experiences and questions about the Python sys module.
YouTip