How To Run a Python File in Jupyter Terminal

Jupyter Notebook is used by scientists, data scientists, and programmers to write and run Python code a lot. Did you know, though, that we can even run a Python script using the Jupyter Terminal itself? If you ever wondered how to run a Python File in Jupyter Terminal, you’ve come to the right place. In…

How To Run a Python File in Jupyter Terminal
Ashwini Avatar

Published by

Published on

Reading Time

4–6 minutes

Share this blog

Jupyter Notebook is used by scientists, data scientists, and programmers to write and run Python code a lot. Did you know, though, that we can even run a Python script using the Jupyter Terminal itself? If you ever wondered how to run a Python File in Jupyter Terminal, you’ve come to the right place. In this easy step-by-step guide, we’ll take you through it.

Running a Python script in Jupyter Terminal is useful for running complete scripts rather than running each cell separately. It offers effective script running, debugging, and batch processing. In this blog, we will understand the basics of running Python scripts in Jupyter Terminal and their benefits.

What is Jupyter Terminal?

Jupyter Notebook also has a built-in terminal, which allows you to execute command-line commands, just as you would on a Linux or Windows command prompt. The terminal can be used to install packages, switch directories, and execute Python scripts.

Why Run a Python File in Jupyter Terminal?

Allow us to look at a few reasons why you would wish to run Python file in Jupyter Terminal before we dive into the process:

  • Efficient Execution: It is more efficient to run a whole script at once rather than running code cell by cell in a Jupyter Notebook.
  • Batch Processing: One can run multiple Python scripts without needing to open and run them individually.
  • Better Debugging: Running scripts via the terminal allows you to debug in a step-by-step fashion.
  • Virtual Environment Support: One can activate virtual environments and run Python scripts from them.
  • Command-line Convenience: You can execute shell commands alongside Python scripts.

Now, let’s move forward with the steps to run a Python file using Jupyter Terminal.

Step-by-Step Guide: How to run a python file in Jupyter Terminal

Step 1: Open Jupyter Notebook

First, you need to open Jupyter Notebook on your system. If you haven’t installed Jupyter Notebook yet, you can install it using the following command in your command prompt or terminal:

Pip install notebook

After installation, start Jupyter Notebook using:

jupyter notebook

This will start Jupyter Notebook in your default web browser.

Step 2: Open the Jupyter Terminal

After you have opened Jupyter Notebook, use the following steps to reach the terminal:

  • Click on New at the top right.
  • Select Terminal from the dropdown menu.

This will open a command line where you can run shell commands as you would in a normal terminal.

Step 3: Move into the Directory Where Your Python File Is

Before you can run a Python script file, ensure that you are in the same directory where you saved the script file. Files in the current directory will be displayed if you use one of the following commands:

ls  # for Linux/macOS

dir  # for Windows

To switch directories, use cd:

Cd path/to/your/directory

If, for example, your Python script file is in a directory named scripts, you can do so as follows:

cd scripts

Once you have entered the directory, verify that your file is there by listing files again.

Step 4: Execute the Python File in Jupyter Terminal

Now that you are in the right directory, you can run your Python script with the following command:

python filename.py

For instance, if your Python script is script.py, execute it using:

python script.py

If you are using Python 3, you might have to use:

python3 script.py

Your script will now run, and its output will be printed right in the terminal.

Step 5: Running Python Scripts with Arguments (Optional)

Python scripts will sometimes need command-line arguments to be passed. You can pass arguments to your script when you run it from the Jupyter Terminal.

For example, if script.py must accept an argument (i.e., a file name), you can run:

python script.py input_file.txt

This comes in handy when you are dealing with scripts that need to process files, accept user input, or take parameters.

Additional Tips on Running Python Scripts in Jupyter Terminal

With Virtual Environments:

If you are working with a virtual environment, first activate it before running your Python script:

source venv/bin/activate  # Linux/macOS

venv\\Scripts\\activate  # Windows

python script.py

Running Scripts in the Background:

To execute a long script without tying up the terminal, use:

python script.py &

Checking Python Version:

Ensure you have the correct Python version by executing:

python– version

or

python3 –version

Redirecting Output to a File:

To redirect the output of the script to a file, use:

python script.py > output.txt

This will write the output to output.txt instead of displaying it to the terminal.

Common Issues and Troubleshooting

  • Command Not Found: If python script.py gives you a “command not found” error, ensure that Python is installed and added to your system’s PATH.
  • File Not Found: If you get “No such file or directory,” ensure you are in the right directory.
  • Permission Denied: If you get a permission error, run:

chmod +x script.py

python script.py

Conclusion

Operating a Python script using Jupyter Terminal is an excellent method of running scripts in a user-friendly manner without utilizing a notebook interface. Data analysis, automation, and computer program development can all be properly performed with such a method and its ability to offer flexibility and authority over the running of the script.

If you are interested in knowing more about Python programming, scripting, and also learning advanced Jupyter Notebook skills, Mynsha Learning is where you should begin. Mynsha Learning provides beginner and professional courses where you can learn Python and data science successfully. Enroll today and upgrade your skills with tutorial expert support!

Like what you read? Share with a friend

Still have questions?

What is Mynsha Learning?

Mynsha is a global online learning platform offering personalised, live, one-to-one classes for children and adults — across academic subjects, creative hobbies and teacher-training courses.

Are classes live or recorded?

Every class is live and one-to-one with your tutor, so it’s fully interactive. Classes are also recorded, so you can revisit them any time.

What age groups do you teach?

All ages. Many courses are for children, and several — like calligraphy, mandala art, yoga and our teacher-training courses — are designed for adults.

Can I choose my class time?

Yes. We teach across 15+ time zones and build classes around your schedule, so you can pick times that suit you.

Do you offer trial classes?

Yes — a free demo, with no card required and no obligation. It’s the best way to meet your tutor and see if Mynsha is right for you.

Ready to Start Your Learning Journey?

Follow your interests, build new skills, and grow with confidence. Let’s take the first step together.