How to Run Python Script in Terminal: A Step-by-Step Tutorial
- Method 1: Running Python Scripts Using the Python Command
-
Method 2: Running Python Scripts with
chmod
on Unix-based Systems - Method 3: Running Python Scripts in Virtual Environments
- Conclusion
- FAQ

Running Python scripts in the terminal is a fundamental skill for any aspiring programmer or data scientist. Whether you’re automating tasks, analyzing data, or developing applications, knowing how to execute your Python code directly from the terminal can significantly enhance your workflow. In this tutorial, we will explore various methods to run Python scripts in the terminal, providing clear, well-commented examples and detailed explanations for each approach. By the end of this article, you will be equipped with the knowledge to confidently run Python scripts, troubleshoot common issues, and optimize your coding experience. Let’s dive in!
Method 1: Running Python Scripts Using the Python Command
One of the most straightforward ways to run a Python script in the terminal is by using the Python command followed by the script’s filename. This method is applicable across various operating systems, including Windows, macOS, and Linux.
First, ensure that Python is installed on your system. You can check this by running the following command in your terminal:
python --version
If Python is installed, you will see the version number. To run a Python script, follow these steps:
- Open your terminal.
- Navigate to the directory where your Python script is located using the
cd
command. For example:
cd path/to/your/script
- Once in the correct directory, run the script using the Python command:
python your_script.py
Output:
Hello, World!
In this example, if your script your_script.py
contains a simple print statement like print("Hello, World!")
, executing the command will display “Hello, World!” in the terminal. This method is effective for running standalone scripts, and it’s essential to ensure that the script file has a .py
extension.
This approach is widely used because of its simplicity and directness. It allows you to execute scripts quickly without needing to configure additional settings. However, ensure that you are using the correct version of Python, as some systems may have both Python 2 and Python 3 installed. In such cases, you may need to use python3
instead of python
.
Method 2: Running Python Scripts with chmod
on Unix-based Systems
If you’re using a Unix-based operating system such as macOS or Linux, you can make your Python scripts executable by changing their permissions. This allows you to run the scripts directly without explicitly invoking the Python interpreter each time.
To do this, follow these steps:
- Open your terminal.
- Navigate to the directory where your Python script is located using the
cd
command. - Change the permissions of your script using the
chmod
command:
chmod +x your_script.py
- Now, you can run your script directly:
./your_script.py
Output:
Hello from Unix-based system!
In this example, if your script your_script.py
contains the line print("Hello from Unix-based system!")
, executing the command will display the message in the terminal.
Changing the script’s permissions with chmod +x
allows the operating system to recognize it as an executable file. This method can streamline your workflow, especially when you have multiple scripts to run frequently. Just remember to include the shebang line at the top of your Python script, which specifies the interpreter to be used. For example, the first line of your script should be:
#!/usr/bin/env python3
This line ensures that the correct Python interpreter is used when executing the script, making it more portable across different environments.
Method 3: Running Python Scripts in Virtual Environments
Using virtual environments is a best practice in Python development, as it helps manage dependencies and avoid conflicts between different projects. To run Python scripts within a virtual environment, you need to create and activate the environment first.
Follow these steps:
- Open your terminal.
- Navigate to your project directory.
- Create a virtual environment using the following command:
python -m venv myenv
- Activate the virtual environment:
- On Windows:
myenv\Scripts\activate
- On macOS and Linux:
source myenv/bin/activate
- Now, you can run your Python script as usual:
python your_script.py
Output:
Running in a virtual environment!
In this case, if your script your_script.py
contains print("Running in a virtual environment!")
, executing the command will display the message in the terminal.
Using virtual environments is beneficial because it isolates your project’s dependencies, ensuring that different projects can use different versions of libraries without conflict. Once you activate the virtual environment, any packages you install using pip
will be confined to that environment. This is particularly useful for maintaining the integrity of your projects and avoiding “dependency hell.”
To deactivate the virtual environment when you’re done, simply run the deactivate
command in the terminal. This will return you to your system’s global Python environment.
Conclusion
In this tutorial, we explored three effective methods for running Python scripts in the terminal: using the Python command, making scripts executable on Unix-based systems, and running scripts within virtual environments. Each method has its unique advantages, and understanding them will enhance your Python programming skills. Whether you are a beginner or an experienced developer, mastering these techniques will streamline your workflow and improve your productivity. Start experimenting with these methods today to see how they can benefit your Python projects!
FAQ
-
How do I check if Python is installed on my system?
You can check if Python is installed by running the commandpython --version
orpython3 --version
in your terminal. -
What should I do if my script doesn’t run?
Ensure that you are in the correct directory and that you have the right permissions. Check for syntax errors in your script as well. -
Can I run Python scripts without the
.py
extension?
While it’s possible to run scripts without the.py
extension, it’s best practice to include it for clarity and to avoid confusion. -
What is a virtual environment in Python?
A virtual environment is an isolated environment that allows you to manage dependencies for different Python projects separately. -
How do I install packages in a virtual environment?
After activating your virtual environment, you can install packages usingpip install package_name
, and they will only be available within that environment.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn