You're just one STEP away to hire a MentorPro
Technology we work in:
Services we provides:
One of the fundamental skills that every Python programmer should master is the ability to run Python scripts in a terminal. Whether you’re a beginner just getting started or an experienced developer looking to refine your workflow, understanding how to execute Python scripts from the command line is crucial.
In this comprehensive guide, we’ll delve into the various methods of running Python scripts in a terminal environment. We’ll explore the differences and nuances between running scripts on Windows and Linux systems, providing you with a detailed overview of the processes involved.
By the end of this guide, you’ll have a solid understanding of how to run Python scripts in different terminal environments, making you a more versatile and capable Python programmer. So, let’s dive in and learn more about how to effectively run Python scripts in a terminal!
Utilize the cd (change directory) command to navigate to the directory containing your Python script. For instance, if your script resides in a folder named “my_python_scripts,” you can navigate to it using the following command:
cd path/to/my_python_script
To execute a Python script, you’ll use the python command, followed by the name of your script. For example:
python my_script.py
Replace “my_script.py” with the actual name of your Python script.
python3 my_script.py
By invoking ‘’python3’’, you ensure that your script executes with Python 3, which is the more modern and widely adopted version. This approach is essential for running Python scripts in a terminal on Linux, where Python 2 might still be available but is generally being phased out in favor of Python 3.
If you have multiple versions of Python 3 installed (e.g., Python 3.8, Python 3.10, Python 3.11, Python 3.12), you can specify the version explicitly. For example, if you want to run your script using Python 3.10, you would use:
python3.10 my_script.py
This allows you to manage and test your scripts across different versions of Python without conflicts. By default, python3 in many Linux distributions, such as Ubuntu, points to a specific version of Python 3 (e.g., Python 3.8.12). You can install other versions and call them using their specific commands, like python3.10, python3.11, or python3.12.
Running a Python script in the Ubuntu terminal is quite straightforward. Here’s a step-by-step guide:
cd Documents
3: Run the Script: Once you’re in the correct directory, you can run the Python script using the python command followed by the name of your script. For example, if your script is named “my_script.py”, you would type:
python my_script.py
python3 my_script.py
python3.10 my_script.py
#!/usr/bin/env python3
#!/usr/bin/env python
chmod +x my_script.py
./my_script.py
nano my_script.sh
This will open the nano text editor with a new file named “my_script.sh”.
Write your script
#!/bin/bash# This is a simple shell script
echo "Hello, world!"
Save the file
chmod +x my_script.sh
Now, you can run the script by typing its name preceded by “./” to specify that it’s located in the current directory:
./my_script.sh
python my_script.py
python my_script.py
Running Python scripts in Visual Studio Code (VSCode) is convenient and efficient. Here’s how you can do it:
VSCode provides an integrated terminal that allows you to execute commands directly within the editor. You can open it by navigating to the “Terminal” menu and selecting “New Terminal”.
python your_script.py
import argparse def function_one(): print("Function One") def function_two(): print("Function Two") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run a specific function.") parser.add_argument("function", choices=["function_one", "function_two"]) args = parser.parse_args() if args.function == "function_one": function_one() elif args.function == "function_two": function_two()
By incorporating command line arguments into your Python script, you can enhance its flexibility and usability. This allows you to execute specific functions directly from the command line, streamlining your workflow and making your scripts more versatile.
python my_script.py function_one
import argparse
Create an argument parser object and define the arguments you want to accept. For example:
parser = argparse.ArgumentParser(description="Description of your script.") parser.add_argument("arg1", type=int, help="Description of argument 1") parser.add_argument("--arg2", type=float, help="Description of argument 2")
args = parser.parse_args()
print("arg1:", args.arg1) print("arg2:", args.arg2)
import my_module
my_module.my_function()
my_module.my_function(argument1, argument2)
To set up a virtual environment, use tools like venv or virtualenv. Once created, activate it in your terminal using source venv/bin/activate on Unix or .\venv\Scripts\activate on Windows. Now, run your Python scripts within this environment. This keeps your project dependencies contained and your system-wide Python installation clean.
In conclusion, running Python scripts from the terminal is a fundamental skill for Python programmers. Whether you’re on Linux, Windows, or using an integrated development environment like VSCode, the process is pretty straightforward. You can execute scripts directly, call specific functions with command line arguments, and handle different situations smoothly.
Ready to transform your business with top-tier custom software development solutions and It staff augmentation services? Partner with us, the premier custom software development company, where our elite team of AI and ML engineers and exceptional Python developers will bring your vision to life with precision and excellence. Let us drive your innovation and help you achieve your business goals. Contact us today!
For those who prefer not to install libraries globally, setting up a virtual environment is an excellent solution. Virtual environments allow you to create isolated environments for your projects, each with its own set of installed libraries and Python versions. This way, you can work on multiple projects with different dependencies without conflicts.
4.7/5
4.8/5
4.4/5
4.6/5
Pakistan
Punjab, Pakistan
28-E PIA, ECHS, Block E Pia Housing Scheme, Lahore, 54770
Phone : (+92) 300 2189222 (PK)
Australia
Perth, Western Australia
25 Mount Prospect Crescent, Maylands, Perth, 6051
Dubai
Albarsha , Dubai
Suhul Building No. 606, Albarsha 1, 47512
Phone : (+92) 300 2189222 (PK)