How to call a Python script in the shell?
One way to call a Python script in a shell is by using the following command:
- Execute the Python script directly.
python script.py
The Python script file that needs to be executed is script.py.
- Execute the script using the Python interpreter.
python -c "print('Hello, World!')"
The -c parameter represents running Python code directly from the command line.
- To add executable permission in the script and specify the interpreter path:
Firstly, at the beginning of the script, add the following code to specify the Python interpreter path:
#!/usr/bin/env python
Next, grant the script execute permission.
chmod +x script.py
Finally, execute the script with the following command:
./script.py
script.py is the Python script file that needs to be executed.
- Specify the interpreter path using shebang:
Add the following code at the beginning of the script to specify the Python interpreter path:
#!/usr/bin/env python
Next, give the script executable permissions.
chmod +x script.py
Finally, run the script using the following command:
./script.py
The Python script file that needs to be executed is script.py.
In the above methods, you need to ensure that the Python interpreter is installed on the system and the correct path is configured in the environment variables.