To run a Python script on Linux, follow these steps:
- Open a terminal in Linux by pressing Ctrl+Alt+T or searching for "Terminal" in the applications menu.
- Navigate to the directory where your Python script is saved. Use the cd command followed by the path to your script's directory. For example, if your script is located in the "/home/user/scripts" directory, you would run:
- Check that you have Python installed. Type the following command to check the Python version:
If Python is not installed, you can install it by running the appropriate command for your Linux distribution, such as sudo apt install python3 for Ubuntu-based systems.
- Run the Python script using the python command followed by the name of your script file. For example, if your script file is named "my_script.py", you would run:
If you have multiple versions of Python installed, you may need to use the specific version to run the script, such as python3 instead of python.
Make sure your script has executable permissions. You can set the executable permissions using the
chmod command, like this:
After setting executable permissions, you can run the script directly by typing
./my_script.py, assuming you are in the same directory as the script.
That's it! Your Python script should now execute and display any output or perform the intended tasks.